简体   繁体   English

没有bind()的UDP客户端不接收数据

[英]UDP client does not receive data without bind()

I refereed to the UDP client program from binarytides and I was able to send a UDP packet frompy PC to the UDP server which is My embedded device and this device echoes back a UDP message. 我从二进制文件中查看了UDP客户端程序,我能够将一个UDP数据包发送到UDP服务器,这是我的嵌入式设备,该设备回送UDP消息。

In this PC-UDP client code it is expected to get the echoed message ,but I'm not getting any echoes back.So I ran a UDP server in my PC which listens for the incoming data and prints it , I was able to see the echoed message from my Embedded device. 在此PC-UDP客户端代码中,预期会收到回显的消息,但我没有得到任何回显。因此,我在PC上运行了一个UDP服务器,该服务器侦听传入的数据并打印出来,我能够看到来自我的嵌入式设备的回显消息。

When I added these lines just before the while(1) loop in the code,and now I'm able to see the Echoed back message. 当我在代码中的while(1)循环之前添加这些行时,现在我能够看到Echoed返回消息。

 //setup address structure
memset((char *) &si_server, 0, sizeof(si_server));
si_server.sin_family = AF_INET;
si_server.sin_port = htons(PORT);
si_server.sin_addr.S_un.S_addr = INADDR_ANY;

if( bind(s ,(struct sockaddr *)&si_server , sizeof(si_server)) == SOCKET_ERROR)
{
    printf("Bind failed with error code : %d" , WSAGetLastError());
    exit(EXIT_FAILURE);
}
puts("Bind done");

Any thoughts on what might be causing the issue? 对什么可能导致此问题有任何想法?

Hi Finally I found the answer from EJP answer 嗨最后我找到了EJP 答案的答案

It is only necessary to bind() a server, because the clients need a fixed port number to send to. 只需要绑定()服务器,因为客户端需要一个固定的端口号来发送。 A client needn't bind() at all: an automatic bind() will take place on the first send()/sendto()/recv()/recvfrom() using a system-assigned local port number. 客户端根本不需要bind():使用系统分配的本地端口号在第一个send()/ sendto()/ recv()/ recvfrom()上发生自动bind()。

With the help of wireshark I was able to see My PC was sending data from Port 53701 and on first sendto() this port got automatically bind'ed , so had to do a explicit binding. 在wireshark的帮助下,我能够看到我的PC正在从端口53701发送数据,并且在第一个sendto()这个端口被自动绑定,因此必须进行显式绑定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM