- 论坛徽章:
- 0
|
socket编程,发送UDP广播数据,抓包检查时,发现目的MAC地址不是全0xff,而是网关的MAC地址,这是为什么?
程序如下:
int socket_fd;
struct sockaddr_in my_addr,user_addr;
int so_broadcast=1;
my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(SERVPORT);
my_addr.sin_addr.s_addr=htonl(INADDR_ANY);
bzero(&(my_addr.sin_zero), 8 );
user_addr.sin_family=AF_INET;
user_addr.sin_port=htons(CLIENTPORT);
user_addr.sin_addr.s_addr=inet_addr("255.255.255.255");
bzero(&(user_addr.sin_zero), 8 );
if((socket_fd=(socket(AF_INET,SOCK_DGRAM,0)))==-1)
{
printf("socket:%s:%d\n", strerror( errno ), errno );
exit(1);
}
setsockopt(socket_fd,SOL_SOCKET,SO_BROADCAST,&so_broadcast,sizeof(so_broadcast));
if((bind(socket_fd,(struct sockaddr *)&my_addr, sizeof(struct sockaddr)))==-1)
{
printf("bind:%s:%d\n", strerror( errno ), errno );
exit(1);
}
char buf[8]={3, 0, 4, 192, 168, 1, 101, 0};
while( 1 )
{
//sleep 1 second
sleep( 1 );
//send broadcast message to inform client the address of server
if( sendto(socket_fd,buf, 7 ,0,(struct sockaddr *)&user_addr,sizeof(user_addr) ) == -1)
{
printf("sendto():%s:%d\n", strerror( errno ), errno );
}
}
[ 本帖最后由 zzbwang 于 2008-12-19 13:37 编辑 ] |
|