Question : UDP sockets question in C over Linux

Dear Expert:

I'm trying to make a simple example client/server program using UDP. There will be one server and different clients executing the same program.

The server executes the socket, bind and readfrom instructions, to read messages arriving from any client. When a message arrive, it answers to the client using sendto.

The client executes the socket, bind (with 0 on port and INADDR_ANY on s_addr field), and sends the messge using sendto. Then get the answer from server using recvfrom.

The problem is server is sending a response, but clients receives its own message, that is, client is receiving the same message he has sent to the server.

I don't understand why, as server has received the message so client message should be out of the socket.

Could be caused because I'm executing client and server on the same machine?

Thank you in advance.

Answer : UDP sockets question in C over Linux

>> The client executes the socket, bind (with 0 on port and INADDR_ANY on s_addr field)

The client isn't supposed to do a bind. Just set up the socket, and call sendto and recvfrom.

For an example, take a look at this sample code from Beej's excellent guide to network programming :

        http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html#datagram
Random Solutions  
 
programming4us programming4us