Is the UDP address already in use?

I follow the UDP tutorials at http://docs.oracle.com/javase/tutorial/networking/datagrams/broadcasting.html , I copied all the code and compiled it, now if I compile the client first and then the server, the server will print it's on the console

Exception in thread "main" java.net.BindException: Address already in use: Cannot bind at java.net.PlainDatagramSocketImpl.bind0(Native Method) at java.net.PlainDatagramSocketImpl.bind(Unknown Source) at java.net.DatagramSocket.bind(Unknown Source) at java.net.DatagramSocket.<init>(Unknown Source) at java.net.DatagramSocket.<init>(Unknown Source) at java.net.DatagramSocket.<init>(Unknown Source) at QuoteServerThread.<init>(QuoteServerThread.java:19) at MulticastServerThread.<init>(MulticastServerThread.java:10) at MulticastServer.main(MulticastServer.java:3) 

QuoteServerThread line 19

 socket = new DatagramSocket(12345); 

Line 10 MulticastServerThread

 public MulticastServerThread() throws IOException { super("MulticastServerThread"); // line 10 } 

MulticastServer 3 String -

 public class MulticastServer { public static void main(String[] args) throws java.io.IOException { new MulticastServerThread().start(); // line 3 } } 

If I first started the server and then the client, the client prints it to the console

 Exception in thread "main" java.net.BindException: Address already in use: Cannot bind at java.net.PlainDatagramSocketImpl.bind0(Native Method) at java.net.PlainDatagramSocketImpl.bind(Unknown Source) at java.net.DatagramSocket.bind(Unknown Source) at java.net.MulticastSocket.<init>(Unknown Source) at java.net.MulticastSocket.<init>(Unknown Source) at MulticastClient.main(MulticastClient.java:9) 

Line 9 multicast

 MulticastSocket socket = new MulticastSocket(12345); 

Looking at the errors, it seems to me that this is due to listening on ports, how can I fix this?

Canvas

+4
source share
2 answers

This is most likely because you are already using a server instance. Only one server can listen on a given port at a time. Make sure you are already using an instance (if you are using Eclipse, you should see this in the command window) and end it before starting another instance.

Although it is also possible that the thread was not closed properly. If you are using the IDE, restarting the IDE should fix the problem, although sometimes I had to restart the computer. This is probably the best solution to fix this, but this is what worked for me.

+3
source

You can do the following:

Use netstat coomand to see which application uses this port. Then use tasklist and taskkill to kill the application on this port.

+1
source

All Articles