2

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.

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.

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.

 //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?

Sorcrer
  • 1,634
  • 1
  • 14
  • 27
  • 4
    An unbound UDP socket will be able to receive from all interfaces on the system. A bound UDP socket will only be able to receive from the bound interface. So if you don't care where the UDP packets are coming from or on what interface they are arriving at, then you don't need to bind the socket. – Some programmer dude Jan 27 '15 at 06:37
  • 1
    See http://stackoverflow.com/a/3057797/4323 – John Zwinck Jan 27 '15 at 06:42
  • @JoachimPileborg So what might the posssible reason I'm not able to get the echo back ? – Sorcrer Jan 27 '15 at 07:07
  • Maybe because the packets are dropped? UDP is an *unreliable* protocol, and *will* have packet loss and reordering. Or maybe there's a router/firewall between your systems that only allows UDP packets one-way? Or maybe there's something wrong with your code? Without more information it's impossible to do more than guess. – Some programmer dude Jan 27 '15 at 07:10
  • hi @JoachimPileborg please see my updated question , I can now see the echoed back message . but can't understand what's the issue? – Sorcrer Jan 27 '15 at 08:58
  • @JoachimPileborg: you have to `bind()` a UDP receiving socket in order to establish the specific port that it can receive on. You just don't have to bind it to a specific interface. There is a difference between an unbound socket and a socket that is bound to 1-or-all interfaces. – Remy Lebeau Jan 27 '15 at 21:26
  • @JoachimPileborg the receiver still needs to bind to a port number, unless it does a prior `send()`. – user207421 Jan 30 '15 at 08:39

1 Answers1

12

Hi Finally I found the answer from EJP answer

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.

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.

Community
  • 1
  • 1
Sorcrer
  • 1,634
  • 1
  • 14
  • 27