-1

Researching and asking of course here I found out something that I did not know/realize before.
That a process can bind a specific port for two different protocols.
E.g. the same port X for TCP and UDP. (Any other well-known examples?)
But how is this possible?
I mean if I do new DatagramSocket(6789); and new ServerSocket(6789);
I assume I can accept both TCP and UDP in my program and just delegate to different classes. Right?
But how does this actually work? Does java understand if the client is using TCP or UDP and passes the socket to the appropriate class?

user207421
  • 305,947
  • 44
  • 307
  • 483
Jim
  • 18,826
  • 34
  • 135
  • 254
  • 1
    Nothing to do with Java; your OS knows the difference between TCP and UDP. – Brian Roach Jul 21 '13 at 15:41
  • 1
    Related: http://stackoverflow.com/questions/12176474/how-to-run-tcp-and-udp-on-a-single-port-at-same-time for java, and there's a number of other similar Q's in general – Brian Roach Jul 21 '13 at 15:42
  • @BrianRoach:So somehow the implementation of the 2 classes register to the OS for their protocols and OS knows where to pass what connection? – Jim Jul 21 '13 at 15:42
  • I don't really understand what you're asking. TCP and UDP are two completely different things. You'd have a `ServerSocket` for TCP and a `DatagramSocket` for UDP and handle each of them in the appropriate manner. – Brian Roach Jul 21 '13 at 15:43
  • @BrianRoach: But the clients connect to the same port right?How does a udp client is send to the proper class? I don't understand this – Jim Jul 21 '13 at 16:04
  • 1
    Again ... TCP and UDP are completely different things. Using the same port number is irrelevant. UDP:6789 has absolutely nothing to do with TCP:6789 If a client sends a UDP packet ... it's a UDP packet. The server's network stack receives it as a UDP packet, and it's delivered to your process as a UDP packet. – Brian Roach Jul 21 '13 at 16:58

1 Answers1

2

Ports exist within the namespace of the protocol. It isn't the same port, only the same port number.

Java has nothing todo with it either.

user207421
  • 305,947
  • 44
  • 307
  • 483