How to run tcp and udp on a single port at same time?

谁说我不能喝 提交于 2019-12-12 13:29:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!