Question

I have a simple UDP Server and Client.

Client creates a DGRAM socket, binds it (explicitly) (even through it is not required), prints out the port it got bound to using getsockname() (non-zero) and then sends a message (via sendto()) which is successfully delivered.

Server receives the message using recvfrom(). The message is received correctly but the port number in sender is zero FOR THE FIRST MESSAGE RECEIVED (I am using ntohs()). From the second message, the port number displays correctly (i.e. the same one which client shows it's socket got bound to).

Any idea?

UPDATE: Problem solved by me. See answer below.

Was it helpful?

Solution

I solved this problem. Suppose last argument of recvfrom() is

socklen_t from_len;
then you must set
from_len = sizeof(struct sockaddr_in);
(for IPv4) before the recvfrom() call. Trivial, I know it, but sometimes it can just skip your mind :-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top