Ghosh
Ghosh

Reputation: 191

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

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?

Upvotes: 3

Views: 3461

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533820

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

Upvotes: 8

Related Questions