|
关于rip2 查询的问题, 我发送了查询消息,怎么没有应答啊, 代码如下
[复制链接]
#include
#include
#include
#include
#include
#include
#include
#include
#include
void MTimeOuts(int msec)
{
struct timeval timeout;
memset(&timeout, 0, sizeof(struct timeval));
if(msec >= 1000)
{
timeout.tv_sec = msec/1000;
timeout.tv_usec = 0;
}
else
{
timeout.tv_sec = 0;
timeout.tv_usec = msec;
}
select(0, NULL, NULL, NULL, &timeout);
}
typedef enum{
STATIC,
DHCP,
PPPoE
} WAY;
int main(int argc, char *argv[])
{
int sock;
WAY way = DHCP;
way = 1;
extern int errno;
struct sockaddr_in toAddr;
sock = socket(AF_INET,SOCK_DGRAM,0);
if(sock < 0)
{
exit(1);
}
struct sockaddr_in bAddr;
struct sockaddr_in cli;
memset(&bAddr, 0, sizeof(bAddr));
bAddr.sin_family = AF_INET;
bAddr.sin_addr.s_addr = htonl(INADDR_ANY);//inet_addr("192.168.0.55");
bAddr.sin_port = htons(520);
int ret = bind(sock, (struct sockaddr *)&bAddr, sizeof(bAddr));
if(ret != 0)
{
char * mesg = strerror(errno);
printf("Mesg:%s\n",mesg);
}
struct ip_mreq mreq;
if (inet_aton("224.0.0.9", &mreq.imr_multiaddr) < 0)
{
perror("inet_aton");
return -1;
}
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
ret = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,&mreq,sizeof(mreq));
if(ret != 0)
{
printf("setsock error code=%d\n", errno);
char * mesg = strerror(errno);
printf("Mesg:%s\n",mesg);
return -1;
}
char buff[128];
memset(buff, 0x00, sizeof(buff));
memset(&toAddr,0,sizeof(toAddr));
toAddr.sin_family=AF_INET;
//toAddr.sin_addr.s_addr=(inet_addr("224.0.0.9"));
//toAddr.sin_addr.s_addr=(inet_addr("210.82.95.233"));
toAddr.sin_port = htons(520);
buff[0] = 0x01;
buff[1]= 0x02;
buff[23] = 0x10;
time_t now = time(NULL);
time_t last =time(NULL);
int count = 0;
char buf[128];
int r = 0;
if(1)
{
ret = 24;
if(sendto(sock,buff, 24,0,(struct sockaddr*)&toAddr,sizeof(toAddr)) != ret)
{
printf("Sending incomplete...\n");
}
int i = 0;
count = 0;
if ((r = recvfrom(sock, buf, 128, 0, (struct sockaddr *)&cli, &count)) < 0)
{
printf("recv no data");
}else
{
for(i= 0; i< r; i++)
{
printf("%02x ", (unsigned char)buf);
}
printf("\ngreat\n");
}
MTimeOuts(1000);
}
close(sock);
return 1;
}
|
|