DHCP:无法接收来自服务器的回复

发布于 2024-08-12 04:17:16 字数 912 浏览 5 评论 0原文

我正在使用 Ubuntu 9.04。我在 VMware 工作站上运行它。这是我的 C 代码:

int sockfd,cnt,addrlen;
const int on = 1;
struct sockaddr_in servaddr,cliaddr;
char reply[512];

sockfd = socket(AF_INET, SOCK_DGRAM, 0);

if (sockfd < 0) {
   perror("socket");
   exit(1);
}

setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR, &on,sizeof(on));
bzero(&cliaddr, sizeof(cliaddr));
cliaddr.sin_family = AF_INET;
cliaddr.sin_addr.s_addr = htonl(INADDR_ANY);
cliaddr.sin_port = htons(68);
addrlen = sizeof(servaddr);

if (bind(sockfd, (struct sockaddr *) &cliaddr, sizeof(cliaddr)) < 0) {        
  perror("bind");
  exit(1);
} 

while(1)
{       
   cnt = recvfrom(sockfd, reply, sizeof(reply), 0,(struct sockaddr *) &servaddr, &addrlen);

   if (cnt < 0) {
     perror("recvfrom");
     exit(1);
   } 

   printf("\nReply Received\n");
}

我在一个终端中运行该程序,并在另一个终端上运行“dhclient”。我没有收到任何数据报。我做错了什么?

I am working on Ubuntu 9.04. I am running this on VMware workstation. Here is my C code:

int sockfd,cnt,addrlen;
const int on = 1;
struct sockaddr_in servaddr,cliaddr;
char reply[512];

sockfd = socket(AF_INET, SOCK_DGRAM, 0);

if (sockfd < 0) {
   perror("socket");
   exit(1);
}

setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR, &on,sizeof(on));
bzero(&cliaddr, sizeof(cliaddr));
cliaddr.sin_family = AF_INET;
cliaddr.sin_addr.s_addr = htonl(INADDR_ANY);
cliaddr.sin_port = htons(68);
addrlen = sizeof(servaddr);

if (bind(sockfd, (struct sockaddr *) &cliaddr, sizeof(cliaddr)) < 0) {        
  perror("bind");
  exit(1);
} 

while(1)
{       
   cnt = recvfrom(sockfd, reply, sizeof(reply), 0,(struct sockaddr *) &servaddr, &addrlen);

   if (cnt < 0) {
     perror("recvfrom");
     exit(1);
   } 

   printf("\nReply Received\n");
}

I run this program in one terminal and run 'dhclient' on another. I receive no datagrams. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

傲影 2024-08-19 04:17:16

看起来您正在 UDP 端口 68 上侦听来自客户端的广播消息?如果我正确读取 DHCP,客户端将从 UDP 端口 68 发送其广播“发现”请求,但发送到服务器上的 UDP 端口 67,因此您需要侦听端口 67 才能接收它。

在尝试使用 dhclient 之前测试您的代码的一个简单的“第一个”测试是尝试使用 netcat 与您的服务器通信。像这样的命令行

echo "Foo" | netcat -u localhost 68

应该会导致您当前的代码接收数据包。

另一个很好的调试工具是wireshark,它可以让您准确地看到dhclient 发送的UDP 数据包以及它们包含的内容。

Looks like you're listening on UDP port 68 for a broadcasted message from the client? If I'm reading DHCP correctly, the client will send its broadcase 'discover' request FROM UDP port 68, but TO UDP port 67 on the server, so you would need to be listening on port 67 to receive it.

An easy 'first' test to test you're code before trying it with dhclient would be to try talking to your server with netcat. a command line like

echo "Foo" | netcat -u localhost 68

Should cause a packet to be received by your current code.

Another good debugging tool is wireshark which will let you see exactly what UDP packets are being sent by dhclient and what they contain.

一个人的旅程 2024-08-19 04:17:16

我不确定你做错了什么,但如果我是你,我会编写自己的客户端,这非常简单,看看它是否可以与上面的服务器代码对话(谁知道 dhclient 除了联系你的代码之外还会做什么)。我还暂时将端口号更改为不为人知的端口号。这样我就不会干扰任何其他程序和界面。

I'm not sure what you're doing wrong but if I were you I'd write my own client which is very simple and see if it can talk to your server code above (who knows what dhclient might do outside of contact your code). I'd also temporarily change the port number to something not well-known. This way I wouldn't be interfering with any other programs and interfaces.

调妓 2024-08-19 04:17:16

我推荐这个教程。另外,你是以 root 身份运行的吗?否则你无法获得那个低编号的端口。

I recommend this tutorial. Also, are you running as root? You can't get that low-numbered port otherwise.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文