I have a situation like I have to run UDP and TCP both on a single port at a time. This is because in my application at any time anyone can call for any protocol. So I need to continously check the incoming request and serve the request. Can anyone pls help me to get rid of this situation in java?
Asked
Active
Viewed 3,403 times
3
-
Same port can receive both requests, but I do't think you can differentiate them - http://stackoverflow.com/questions/6437383/tcp-and-udp-sockets-on-same-port – Manimaran Selvan Aug 29 '12 at 11:10
-
3@ManimaranSelvan No. All UDP ports are different from all TCP ports regardless of the number. They occupy different namespaces. OP has to use two sockets, a TCP and a UDP. There is no such thing as 'differentiate' the request because they can never get confused in the first place. – user207421 Aug 29 '12 at 12:27
1 Answers
7
You can't check whether a request is TCP or UDP. Instead you add a listener which is TCP and a listener which is UDP. IMHO UDP is more useful if you use a broadcast or multi-cast address.
e.g.
ServerSocket ss = new ServerSocket(12345);
DatagramSocket ds = new DatagramSocket(12345);
or
ServerSocket ss = new ServerSocket(12345);
DatagramSocket ds = new MulticastSocket(new InetSocketAddress("224.224.1.1", 12345));
In both cases, tcp connections go to the ServerSocket
and udp packets go to the DatagramSocket

Peter Lawrey
- 525,659
- 79
- 751
- 1,130