13

I think multiple sockets can be associated with same TCP port.

But can the same thing work for UDP?

Cœur
  • 32,421
  • 21
  • 173
  • 232
Terminal User
  • 803
  • 3
  • 12
  • 21

2 Answers2

12

The only way to associate multiple sockets with a port in TCP is by listening and then accepting.

The purpose in that case is to give every incoming client a unique socket so as to keep their byte streams separate.

You don't need that in the case of UDP because there are no byte streams. You can write an entire UDP server using a single UDP socket. You just read, despatch to a handler for that client, the handler writes the response back via the same socket.

user207421
  • 289,834
  • 37
  • 266
  • 440
  • 1
    The question implies either: 1) packets from different source ip addresses, or 2) packets from different source ports on the same source ip. Either way, those *are* different sockets, by definition. I don't quite see your point when you say "you don't need that (sockets?) in the case of UDP" – Mike Pennington May 27 '11 at 11:26
  • @Mike Pennington The server only needs one listening port. Every request datagram comes in via that port, and contains its own reply address. Every reply datagram can go out via that port, to the reply address of the request datagram. So, only one datagram socket. Your (1) and (2) don't make any difference to that: they are just distinct reply addresses. – user207421 May 28 '11 at 08:10
  • @ejp I am talking abut the client ip and port. we both agree that the server port is the same regardless. – Mike Pennington May 28 '11 at 08:50
  • @Mike Pennington I've answered that. In Java it's as easy as putting the response into the same DatagramPacket the request arrived in, and sending it back via the single DatagramSocket. Tell me why you think > 1 is required. – user207421 May 28 '11 at 09:09
  • 2
    @EJP, I was mistaken. DATAGRAM SOCKETS are identified by a two-tuple, while STREAM SOCKETS are identified by a four-tuple. +1 for your answer – Mike Pennington May 28 '11 at 09:46
  • Is that possible to start two TCP or UDP socket on the same port at the same time? – Terminal User May 29 '11 at 04:59
  • @JIpeng Tan having read my answer why do you still think it is necessary? – user207421 May 29 '11 at 09:45
  • @TerminalUser (a) TCP: not unless they are all bound to different interfaces none of which is `0.0.0.0.` (b) UDP: yes, provided all of them were created with `SO_REUSEADDR` or `SO_REUSEPORT`. There is an exception to (a) under Windows which I don't begin to understand the point of, as it doesn't mean that both ports can accept connections. – user207421 Aug 08 '13 at 12:25
7

Yes, it is also possible to have multiple sockets using a single UDP port.

caf
  • 216,678
  • 34
  • 284
  • 434
  • 6
    With the caveat that only broadcast & multicast packets are going to be multiplexed, unicast packets will only be delivered to the first socket. – Steve-o Jun 17 '11 at 14:29
  • 1
    @caf how can it be? see http://www.cs.cmu.edu/~srini/15-441/F01.full/www/assignments/P2/htmlsim_split/node18.html If a second socket attempts to Bind to port 8000 on interface 1, the Bind will fail since the first socket already ``owns'' that port/interface – onmyway133 Jul 21 '13 at 03:23
  • 2
    @entropy: That's the default behaviour, but many OSes allow programs to request that behaviour be disabled. See [this answer](http://stackoverflow.com/a/14388707/134633) for some more information on this. – caf Jul 21 '13 at 13:04