在使用C的unix上,我的客户端正在使用超级用户模式侦听端口68.在发送DHCP发现消息之后,当我尝试接收时,它在recvfrom中阻塞意味着没有收到消息,或者它是否像系统有一个进程(DHCP客户端)在同一端口68上侦听它接收消息并且我的进程不能接收消息.问题是什么?
我已设置套接字选项SO_REUSEADDR和SO_BROADCAST.我要送到67号港口.
struct dhcpmessage { uint8_t op; uint8_t htype; uint8_t hlen; uint8_t hops; uint32_t xid; uint16_t secs; uint16_t flags; uint32_t ciaddr; uint32_t yiaddr; uint32_t siaddr; uint32_t giaddr; char chaddr[16]; char sname[64]; char file[128]; char magic[4]; char opt[3]; } __attribute__((__packed__)); #include#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include"defs.h" int main() { int sockfd,listenfd,connfd; const int on=1; struct sockaddr_in servaddr,cliaddr,rservaddr; if((sockfd=socket(AF_INET,SOCK_DGRAM,0)) < 0) die("socket"); if(setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)) < 0) die("setsockopt"); if(setsockopt(sockfd,SOL_SOCKET,SO_BROADCAST,&on,sizeof(on)) < 0) die("setsockopt"); bzero(&servaddr,sizeof(servaddr)); bzero(&cliaddr,sizeof(cliaddr)); cliaddr.sin_port = htons(68); cliaddr.sin_family = AF_INET; cliaddr.sin_addr.s_addr = htonl(INADDR_ANY); if(bind(sockfd,(struct sockaddr*)&cliaddr,sizeof(cliaddr)) < 0) die("bind"); servaddr.sin_port = htons(67); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = inet_addr("255.255.255.255"); struct dhcpmessage dhcpmsg; bzero(&dhcpmsg,sizeof(dhcpmsg)); dhcpmsg.op = 1; dhcpmsg.htype = 1; dhcpmsg.hlen = 6; dhcpmsg.hops = 0; dhcpmsg.xid = htonl(1000); dhcpmsg.secs = htons(0); dhcpmsg.flags = htons(0x8000); dhcpmsg.chaddr[0] = 0x00; dhcpmsg.chaddr[1] = 0x1A; dhcpmsg.chaddr[2] = 0x80; dhcpmsg.chaddr[3] = 0x80; dhcpmsg.chaddr[4] = 0x2C; dhcpmsg.chaddr[5] = 0xC3; dhcpmsg.magic[0]=99; dhcpmsg.magic[1]=130; dhcpmsg.magic[2]=83; dhcpmsg.magic[3]=99; dhcpmsg.opt[0]=53; dhcpmsg.opt[1]=1; dhcpmsg.opt[2]=1; if(sendto(sockfd,&dhcpmsg,sizeof(dhcpmsg),0,(struct sockaddr*)&servaddr,sizeof(servaddr)) < 0) die("sendto"); struct dhcpmessage recvdhcpmsg; socklen_t rservlen = sizeof(rservaddr); if(recvfrom(sockfd,&recvdhcpmsg,sizeof(recvdhcpmsg),0,(struct sockaddr*)&rservaddr,&rservlen) < 0) die("recvfrom"); char *str = (char*)&recvdhcpmsg; int i; for(i=0;i
nos.. 5
需要注意的是,您没有IP地址,您想获得一个IP地址. - 大多数操作系统不允许您在没有IP地址的NIC上绑定/发送UDP消息.dhcp客户端通常使用原始套接字/并且应该将src地址设置为0.0.0.0)
这样的原始套接字将获取所有数据包,如果系统dhcp客户端正在运行,则您的应用程序将不会
1> nos..:需要注意的是,您没有IP地址,您想获得一个IP地址. - 大多数操作系统不允许您在没有IP地址的NIC上绑定/发送UDP消息.dhcp客户端通常使用原始套接字/并且应该将src地址设置为0.0.0.0)
这样的原始套接字将获取所有数据包,如果系统dhcp客户端正在运行,则您的应用程序将不会