Receiving a response via UDP

I saw how applications send a packet via UDP to an IP address on some port and get a response. Which port is the answer to? (By the way, how does the router know the answer is for my PC if there are no ports redirected to my computer?)

Cheers, Neo_b

+4
source share
3 answers

Which port the response is assigned to depends on the application. UDP is completely stateless, so after you release the packet, the only way the application can wait for a response is that it knows that the other end will send it. Depending on the UDP application, I would expect the response to be executed on the same port for simplicity - this is not the case for protocols such as TCP, which have an intentionally random (and high) source port.

To answer your second question, many routers, even low-cost home routers, perform stateful packet inspection (SPI). Something like this is likely to happen, but I should be fixed if I leave:

[Set stage with client, router, internet, server.]

  • The client issues a UDP packet.
  • The router sends a UDP packet to the Internet.
  • The router remembers that the client sent the UDP packet to the server and set the mapping in its memory.
  • The server sends a UDP packet, possibly to the same port.
  • The router receives the packet and checks the mapping to find a client recently connected to the server.
  • The router sends the packet to the client.

How it is implemented is specific to the router, I think, but this is my understanding of how it works.

+7
source

If I send a message to the UDP port on another computer, whichever port I send the message from, no matter how it is selected, appears in the UDP datagram. I would think that the remote end would send any response to this datagram to this source port.

I suppose the same applies even if the ports are changed by a firewall or NAT device, the remote end sees a datagram from a specific port and sends a response back, the firewall / NAT device then translates that port into the source source port.

+2
source

When you create a UDP socket, you must bind it to the port number. If you do not, the operating system will assign an ephemeral port.

An application, on the other hand, should know about this port. When replies are sent back, your router may not know how to route. There are 2 ways to solve this problem.

  • You can explicitly configure the route to your computer on a specific computer port.
  • You can configure the router to monitor the UDP connection by automatically opening the route to your computer when a particular packet is sent. UPNP protocol based on this concept.
+1
source

All Articles