136

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?

Nicholas Wilson
  • 9,435
  • 1
  • 41
  • 80
user800799
  • 2,883
  • 7
  • 31
  • 36

2 Answers2

156

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.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • 1
    another example NetBIOS with UDP and TCP on 137-139 – Eric Fortis Jun 22 '11 at 09:11
  • 2
    @Eric Fortis RFC 1700 is full of examples, let's not list them all shall we? ;-) – user207421 Jun 22 '11 at 10:40
  • 1
    the official repository of port numbers is at http://www.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. – Alnitak Jun 22 '11 at 10:42
  • 1
    @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. – Eric Fortis Jun 24 '11 at 22:29
  • @Eric I could list the two I've written... ;-) – Alnitak Jun 24 '11 at 22:35
  • 2
    @Eric Fortist why exactly should 'everybody know about' the NetBIOS port numbers? – user207421 Jun 25 '11 at 10:00
  • 4
    Please post it as a question so you can grant me the answer – Eric Fortis Jun 25 '11 at 18:30
  • 16
    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. – moala Aug 08 '14 at 08:54
  • @moala yes, that's right - I might expand the answer slightly. – Alnitak Aug 08 '14 at 09:07
  • 1
    @moala can I access same port (e: 53) ,in same time via UDP and TCP protocol ? – uma Oct 17 '18 at 01:52
  • 1
    @uma yes, you can (if supported by the server). That's the entire point of this question. – Alnitak Oct 17 '18 at 07:53
13

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

aMooly
  • 551
  • 5
  • 13
  • 4
    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. – user207421 Sep 01 '14 at 03:45
  • 1
    Thank you for pointing out my fault.It's right that there is no connection using UDP. – aMooly Sep 01 '14 at 09:49