问题
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?
回答1:
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
来源:https://stackoverflow.com/questions/12176474/how-to-run-tcp-and-udp-on-a-single-port-at-same-time