Can TCP and UDP sockets use the same port?

SocketsNetworkingTcpUdp

Sockets Problem Overview


First of all, is there any problem with using both UDP and TCP on the same server?

Secondly, can I use the same port number?

Sockets Solutions


Solution 1 - Sockets

Yes, you can use the same port number for both TCP and UDP. Many protocols already do this, for example DNS works on udp/53 and tcp/53.

Technically the port pools for each protocol are completely independent, but for higher level protocols that can use either TCP or UDP it's convention that they default to the same port number.

When writing your server, bear in mind that the sequence of events for a TCP socket is much harder than for a UDP socket, since as well as the normal socket and bind calls you also have to listen and accept.

Furthermore that accept call will return a new socket and it's that socket that you'll then have to also poll for receive events. Your server should be prepared to continue accepting connections on the original socket whilst simultaneously servicing multiple clients each of which will be triggering receive events on their own sockets.

Solution 2 - Sockets

Firstly,there is no problem using both tcp and udp on the server.

Secondly,we can have both UDP and TCP requests on same port ,because each request is identified by a quintuple contained by source IP ,Destination IP, Source Port, Destination Port, PROTOCOL(as protocol can be TCP or UDP).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser800799View Question on Stackoverflow
Solution 1 - SocketsAlnitakView Answer on Stackoverflow
Solution 2 - SocketsaMoolyView Answer on Stackoverflow