Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion over how UDP server sends the response back to UDP client

Tags:

tcp

sockets

udp

I am writing a UDP based client server and have got pretty much the code, but I am confused about how UDP server sends the response back to UDP client, this is my understanding till now:

  • Suppose a UDP client wants to communicate with a UDP server, so it will send a request to the UDP server (using the UDP socket opened at client's end), now this will reach the UDP module at the UDP server, where UDP module will identify the UDP service using the port number and will send that request to that UDP service/server.
  • Now, since UDP is a connection-less protocol so unlike TCP, UDP server will not send response over some connection, instead, UDP server will extract the source IP address and source port from the request and send the response back to client.

My confusion is that at server side, there is a socket which is bound to a UDP port and "continuously" listening for any UDP client request, but this is not true at client side, UDP client will open a socket to send the request to UDP server and then that's it, I think it cannot keep that port hanging for UDP server to respond, and if that port closes then how client will receive the response back.

I mean ofcourse, UDP server's response will reach back the UDP client because IP address is there, but once that response has reached UDP module of the client, even though there will be a port but how UDP module can send it to the client who originally sent the request because it would have closed the socket bound to that port? Or it will not?

I am looking for answer which clearly describes the UDP communication (I am not interested in contrasting it with TCP or explaining TCP since I have already fair understanding of TCP), especially how the response will reach back the UDP client.

like image 662
pjj Avatar asked Dec 18 '16 21:12

pjj


People also ask

How does UDP respond to a service request to a listening port?

Now, since UDP is a connection-less protocol so unlike TCP, UDP server will not send response over some connection, instead, UDP server will extract the source IP address and source port from the request and send the response back to client.

Does UDP send a response packet?

UDP is completely stateless, so after firing off a packet the only way an application can expect a response is if it knows the other end is going to send one.

Does UDP have a response?

Many traditional UDP protocols, like DNS, are request-response based. Since there is no state associated with a higher level "connection", the server can restart, to upgrade or change configuration, without any problems.

How does UDP send traffic?

Use nc command for sending and receiving UDP packets through network. Send some human readable sentences through nc command. Capture the UDP packet sent by nc command. Check network packet in Wireshark.


2 Answers

My confusion is that at server side, there is a socket which is bound to a UDP port and "continuously" listening for any UDP client request, but this is not true at client side, UDP client will open a socket to send the request to UDP server and then that's it, I think it cannot keep that port hanging for UDP server to respond, and if that port closes then how client will receive the response back.

I agree. This is your confusion. Why do you think can't keep the socket open and do a receive on it? It can.

I mean ofcourse, UDP server's response will reach back the UDP client because IP address is there, but once that response has reached UDP module of the client, even though there will be a port but how UDP module can send it to the client who originally sent the request because it would have closed the socket bound to that port?

Why?

Or it will not?

Not.

The client:

  • creates a socket
  • sends a datagram
  • calls recvfrom() or friends to receive the response.

Of course if the client isn't interested in the response it can close the socket, but this is not the normal case.

I am looking for answer which clearly describes the UDP communication (I am not interested in contrasting it with TCP or explaining TCP since I have already fair understanding of TCP), especially how the response will reach back the UDP client.

So don't tag your question with the tcp tag.

like image 156
user207421 Avatar answered Oct 11 '22 10:10

user207421


Yes, UDP server can send back to client. Here is an example: https://www.cs.rutgers.edu/~pxk/417/notes/sockets/udp.html and code demo https://www.cs.rutgers.edu/~pxk/417/notes/sockets/demo-udp-04.html

We now have a client sending a message to a server. What if the server wants to send a message back to that client? There is no connection so the server cannot just write the response back. Fortunately, the recvfrom call gave us the address of the server. It was placed in remaddr:

recvlen = recvfrom(s, buf, BUFSIZE, 0, (struct sockaddr *)&remaddr, &addrlen);

The server can use that address in sendto and send a message back to the recipient's address.

sendto(s, buf, strlen(buf), 0, (struct sockaddr *)&remaddr, addrlen)

like image 22
Y00 Avatar answered Oct 11 '22 09:10

Y00