当前位置: 技术问答>linux和unix
如何获得client的MAC地址?
来源: 互联网 发布时间:2016-01-27
本文导语: 我写了一个模拟程序,一个client,一个server,通过upd传数据。 我可以用inet_ntoa获得client端的IP地址,但是我不知道怎么从server端获得client的MAC地址。谢谢! client.c -------------------- #include #include #include #include #incl...
我写了一个模拟程序,一个client,一个server,通过upd传数据。
我可以用inet_ntoa获得client端的IP地址,但是我不知道怎么从server端获得client的MAC地址。谢谢!
client.c
--------------------
#include
#include
#include
#include
#include
#include
#include
int port = 6789;
int main(int argc, char** argv)
{
int sdf;
struct sockaddr_in addr;
char buf[1024];
int ret;
memset(&addr, 0, sizeof(addr));
sdf = socket(AF_INET, SOCK_DGRAM, 0);
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("127.0.0.100");
addr.sin_port = htons(port);
do {
memset(buf, 0, 1024);
printf("Please input the next message: ");
scanf("%s", buf);
ret = sendto(sdf, buf, strlen(buf), 0, (struct sockaddr *)&addr, sizeof(addr));
printf("ret: %dn", ret);
if (!strcmp(buf, "exit") || !strcmp(buf, "quit")) break;
} while(1);
}
=========================
server.c
-------------------------
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int port = 6789;
int main(int argc, char** argv)
{
int sdf;
struct sockaddr_in sin;
struct hostent* server_host_name;
char buf[2048];
int sin_len;
int ret;
bzero(&sin, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_port = htons(port);
sdf = socket(PF_INET, SOCK_DGRAM, 0);
bind(sdf, (struct sockaddr *)&sin, sizeof(sin));
while(1) {
bzero(buf, 2048);
sin_len = sizeof(sin);
ret = recvfrom(sdf, buf, 2047, 0, (struct sockaddr *)&sin, (socklen_t*)&sin_len);
printf("ret: %dn", ret);
printf("Received message: %sn", buf);
printf("Client IP address: %sn", inet_ntoa(sin.sin_addr));
if (ioctl(sdf, SIOCGIFNETMASK, &sin)
我可以用inet_ntoa获得client端的IP地址,但是我不知道怎么从server端获得client的MAC地址。谢谢!
client.c
--------------------
#include
#include
#include
#include
#include
#include
#include
int port = 6789;
int main(int argc, char** argv)
{
int sdf;
struct sockaddr_in addr;
char buf[1024];
int ret;
memset(&addr, 0, sizeof(addr));
sdf = socket(AF_INET, SOCK_DGRAM, 0);
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("127.0.0.100");
addr.sin_port = htons(port);
do {
memset(buf, 0, 1024);
printf("Please input the next message: ");
scanf("%s", buf);
ret = sendto(sdf, buf, strlen(buf), 0, (struct sockaddr *)&addr, sizeof(addr));
printf("ret: %dn", ret);
if (!strcmp(buf, "exit") || !strcmp(buf, "quit")) break;
} while(1);
}
=========================
server.c
-------------------------
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int port = 6789;
int main(int argc, char** argv)
{
int sdf;
struct sockaddr_in sin;
struct hostent* server_host_name;
char buf[2048];
int sin_len;
int ret;
bzero(&sin, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_port = htons(port);
sdf = socket(PF_INET, SOCK_DGRAM, 0);
bind(sdf, (struct sockaddr *)&sin, sizeof(sin));
while(1) {
bzero(buf, 2048);
sin_len = sizeof(sin);
ret = recvfrom(sdf, buf, 2047, 0, (struct sockaddr *)&sin, (socklen_t*)&sin_len);
printf("ret: %dn", ret);
printf("Received message: %sn", buf);
printf("Client IP address: %sn", inet_ntoa(sin.sin_addr));
if (ioctl(sdf, SIOCGIFNETMASK, &sin)