简体   繁体   中英

Receiving UDP data in Java

I'm trying to receive UDP data broadcast by PlayCap to network address 192.168.103.255 port 3000 in Java, but I'm having trouble setting things up. Here's what I have:

DatagramSocket socket = new DatagramSocket();
InetSocketAddress address = new InetSocketAddress("192.168.103.255", 3000);
socket.bind(address);

I'm getting "java.net.SocketException: already bound" from the bind call. I'm pretty inexperienced with networking, so I may be doing something way wrong here. Any help is appreciated.

Here is the stacktrace:

java.net.SocketException: already bound
    at java.net.DatagramSocket.bind(Unknown Source)
    at runner.main(runner.java:16)

I dont want to revive and old thread but i don't think the answer to this question is correct. I faced the same issue when i used the similar code to create a DatagramSocket.

DatagramSocket socket = new DatagramSocket();
socket.setReuseAddress(true);
socket.bind(new InetSocketAddress(InetAddress.getByName("localhost"), 5566));

This results in a SocketException

Exception in thread "main" java.net.SocketException: already bound
at java.net.DatagramSocket.bind(DatagramSocket.java:376)
at testapplication.TestApplication.main(TestApplication.java:25)

Java Result: 1

Not because there is another process occupying the same port but i have created an already BOUND datagram socket when i use the default constructor.

new DatagramSocket()

According to javadoc :

DatagramSocket() Constructs a datagram socket and binds it to any available port on the local host machine.

So the reason for the exception is you are trying to bind an already bound socket. To make it work you need to create an unbond socket with below constructor

DatagramSocket socket = new DatagramSocket(null);
InetSocketAddress address = new InetSocketAddress("192.168.103.255", 3000);
socket.bind(address);

Hope this helps...

Do
netstat -a -o -n
and from this you can find that either this port is already bind or not(even from this you can get all the bound ports).If yes , then try any other port :)

Most probably your application is running twice . Or you might be executing the same code twice. Even the same application may fail when binding twice.

Happens a lot for beginners that they didn't shut down their previous attempt (happened to me, too), and then their port is already in use. Make sure to add proper exception handling, eg by popping up a message "Port already in use."

Note that for listening you usually will bind a port only , without an explicit address (you might need to use "0.0.0.0" for this). Then you can receive both broadcast and unicast.

The code I use for listening to broadcasts is simply:

DatagramSocket s = new DatagramSocket();
s.bind(new InetSocketAddress(port))

Note that I'm not binding to a particular address, but only to a port.

Check the port 3000 it may be already used by another application. Try using a different port.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM