Kamran Hosseini
Kamran Hosseini

Reputation: 536

destination port in UDP protocol

My question is that how the destination port address in UDP is chosen/given?

I mean what matters to set a destination port in a UDP packet? Because when we send a packet, just the destination address(ip) is important and we want to send data to our destination. It has nothing to do with the port! Do we assign a random port?

Upvotes: 0

Views: 2120

Answers (2)

Ron Maupin
Ron Maupin

Reputation: 6452

Generally, the sending port is chose randomly for the ephemeral ports available.

The destination port is the port to which the destination application is listening. To facilitate this, IANA maintains the Service Name and Transport Protocol Port Number Registry for standard applications and protocols.

If you create your own application or protocol, there is a range for you to use, but you should always check the registry to make sure you will not step on some other application or protocol.

When you design your listening application or protocol, you choose a port on which it listens, and the sending application will need to send to that port.

Upvotes: 0

David Schwartz
David Schwartz

Reputation: 182753

Typically, whatever documentation tells you what to put in the UDP datagram you're sending should also tell you what port to send it to.

For example, if you're trying to talk to an NTP server, RFC5905 tells you what to put in the UDP datagrams you send. It also tells you, on page 16, to send it to port 123.

If you're writing a DNS resolver, RFC1035 is one place you might look for the information needed to know what to put in your UDP datagrams. It also tells you, in section 4.2, to send the datagrams to port 53.

So however you're figuring out what to put in the UDP datagrams you're going to send, that's typically what tells you either what port to send them to or, in some cases, how to determine what port to send them to.

For example, a media streaming protocol might start with the information about the stream being delivered by a web server. In that case, the information delivered by the web server to the client might include the destination port to send datagrams to.

Generally, there's either a well-known port that at least one side listens for datagrams on or there's some external method using a different protocol that tells whichever end sends the first datagram what port to send it to. The other end then just replies, sending its response datagrams to whatever port that first datagram was sent from.

Upvotes: 1

Related Questions