I am following the UDP tutorials at http://docs.oracle.com/javase/tutorial/networking/datagrams/broadcasting.html ,I have copied all the code and compiled it, now If I compile the client first and then the server, the server prints this out in 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 is
socket = new DatagramSocket(12345);
MulticastServerThread line 10 is
public MulticastServerThread() throws IOException {
super("MulticastServerThread"); // line 10
}
MulticastServer line 3 is
public class MulticastServer {
public static void main(String[] args) throws java.io.IOException {
new MulticastServerThread().start(); // line 3
}
}
If I start the server first, then the client, the client prints out this in 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)
MulticastClient line 9 is
MulticastSocket socket = new MulticastSocket(12345);
Looking at the errors, it seems to me that it is something to do with listening to ports, how can I go about fixing this?
Canvas
You can do the following:
Use netstat coomand to view which application is using that port. Then use tasklist and taskkill to kill the application at that port.
This is likely because you are already running an instance of the server. Only one server can listen on a given port at a time. Check to see if you are already running an instance (if you are using Eclipse, you should see this in the command window) and terminate it before running another instance.
Though it is also possible that a stream was not shut down properly. If you are running an IDE, restarting the IDE should fix the problem, though occasionally I have had to restart my computer. Probably a better solution to fix this, but that is what has worked for me.