Can TCP and UDP sockets use the same port?
Asked Answered
C

3

140

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?

Calces answered 22/6, 2011 at 9:1 Comment(0)
L
162

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.

Lurette answered 22/6, 2011 at 9:5 Comment(13)
another example NetBIOS with UDP and TCP on 137-139Typhus
@Eric Fortis RFC 1700 is full of examples, let's not list them all shall we? ;-)Eggert
the official repository of port numbers is at iana.org/assignments/port-numbers these days, but many of the dual udp/tcp entries are merely reservations, and don't indicate that the protocol actually uses both. For example there's no implementation of HTTP over UDP, since HTTP requires a reliable transport. Both are registered to avoid confusion and prevent an unrelated protocol appearing to be port 80.Lurette
@EJP The ietf is full of RFCs, let's not list them all shall we? BTW just pointing out some ports everybody should know about.Typhus
@Eric I could list the two I've written... ;-)Lurette
@Eric Fortist why exactly should 'everybody know about' the NetBIOS port numbers?Eggert
Please post it as a question so you can grant me the answerTyphus
Something this answer does not explain: the 'port' semantic is specific to each protocol (but some might not have this semantic) of the transport level (OSI model level 4). So TCP has its own ports, which are interpreted by the TCP stack; UDP has its own ports, which are interpreted by the UDP stack. So to say, ports are not shared between UDP and TCP; it just happens that both protocols have the same definition of "ports" and that in order to simplify, we use the same port value for multiple connections of different types to the same service.Tunicle
@Tunicle yes, that's right - I might expand the answer slightly.Lurette
@Tunicle can I access same port (e: 53) ,in same time via UDP and TCP protocol ?Polypeptide
@Polypeptide yes, you can (if supported by the server). That's the entire point of this question.Lurette
As the network families seem to have independent port allocation, is there no need to use the port reuse option for single udp and single tcp binding?Reformation
@SamGinrich the port reuse option is not required in that circumstance.Lurette
I
14

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).

Incorrect answered 1/9, 2014 at 3:7 Comment(2)
The reason you state is often given but it is really meaningless. There is no such thing as a UDP connection, and no context in which connections are considered regardless of the associated protocol. The fact is that ports are artefacts of TCP and UDP separately, and there is therefore no possibility of ever confusing them.Eggert
Thank you for pointing out my fault.It's right that there is no connection using UDP.Incorrect
S
1

Quick nc experiment showing that you can use the same port for TCP/UDP simultaneously

Tested on Ubuntu 23.10, nc from the netcat-openbsd package, based on my other answer: https://mcmap.net/q/45836/-http-test-server-accepting-get-post-requests we can launch a simple server that listens forever on TCP port 8000 with:

nc -kdl 8000

From man nc we know that the -u flag uses UDP instead of TCP, let's try and launch another listener at the same time with -u from another shell:

nc -kdlu 8000

And now, we can send messages to either TCP or UDP, and the correct listener will always get the message, for TCP:

echo asdf | nc localhost 8000

and for UDP:

echo asdf | nc -u localhost 8000

and the corresponding listener then prints asdf to stdout.

Another way to check that both are actually listening on the same port number but on different domains is:

sudo netstat -tupan | grep 8000

which gives:

tcp        0      0 0.0.0.0:8000       0.0.0.0:*           LISTEN      214312/nc
udp        0      0 0.0.0.0:8000       0.0.0.0:*                       227612/nc

There is however a complicating factor that I only learned about when writing this answer, if we launch another two listeners with and without -u:

nc -kdl 8000
nc -kdlu 8000

then this version of nc does not complain. This is because as we can see with strace, bind is done with SO_REUSEPORT, related threads:

So now that we have four nc running, netstat gives:

tcp        0      0 0.0.0.0:8000          0.0.0.0:*       LISTEN      237988/nc
tcp        0      0 0.0.0.0:8000          0.0.0.0:*       LISTEN      214312/nc
udp        0      0 0.0.0.0:8000          0.0.0.0:*                   237673/nc
udp        0      0 0.0.0.0:8000          0.0.0.0:*                   227612/nc

and when we send the requests, the TCP and the UDP requests are randomly routed to either one of the two of the listeners of the corresponding type.

Strander answered 30/1 at 23:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.