2

I have written a simple code of server side programming that is waiting for the response from client, this runs successfully on the cmd.exe but while executing it on the eclipse it is throwing exception, the details of exception are as follows;

Exception in thread "main" java.net.BindException: Address already in use: JVM_Bind

this is happening in eclipse only could anyone please tell me how it is working in eclipse & how in cmd.exe

the code of server side program is :

package networking;

import java.io.*;
import java.net.*;

public class Server1 {
    public static void main(String args[]) throws Exception {
        ServerSocket sock = new ServerSocket(5000);
        Socket s1 = sock.accept();
        BufferedReader br = new BufferedReader(new InputStreamReader(
                s1.getInputStream()));
        String msg = br.readLine();
    }

}
Make
  • 422
  • 2
  • 6
  • 17
  • 1
    Is the program still running in Eclipse when you try to run it on the command line? The error you're getting is because two programs are trying to `accept` on the same port. – Xymostech May 08 '13 at 03:45
  • No It is not running,I am running it in eclipse now not in cmd.exe, I am running it only one place on one time. – Make May 08 '13 at 03:46
  • What if you add `sock.setReuseAddress(true)` before you call `sock.accept()`? – Xymostech May 08 '13 at 03:50
  • Again throwing same exception – Make May 08 '13 at 03:52
  • 2
    [Reference](http://stackoverflow.com/questions/3714957/address-already-in-use-jvm-bind) – thar45 May 08 '13 at 03:59
  • FYI, I rolled back your question because you deleted the error you were getting. The error is very important for helping users find your question (and answer it). – thegrinner May 09 '13 at 13:49

2 Answers2

1

The socket is open when 'Server1' is started.

This can happen if:

  • Server1 is already running. ( you didn't kill it, seems to be the likely case...)
  • Another process is listening on port 5000.
  • The port isn't closed down quite yet, but is stuck in a "waiting" state for a while. (It can happen if a server is killed when sockets are connected.)

But! Instead of guessing, You should simply check what's going on: Download Microsoft's TCPView and have a look:

http://technet.microsoft.com/en-us/sysinternals/bb897437

(You could also use netstat -a in a cmd window.)

Some notes:

The port number

You should probably not use port 5000, as it seems to be used by uPnP - you may find that the port is always open on some machines... Check here:

http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers.

Try a 5-digit number :-)

Time_wait

Check this question about Java network server and TIME_WAIT:

The should-be-accepted answer gives the following options to reduce the window,

setSoLinger(true,0)

setReuseAddress(true)

(With the risk of losing data when killing the server as the client may not become aware of that the send failed. In the old days, data could get mixed up too, but that is no longer the case.)

Community
  • 1
  • 1
KarlP
  • 5,149
  • 2
  • 28
  • 41
0

This used to happen to me all the time in eclipse. Make sure nothing else with respect to your server is running. In eclipse, make sure you click the red box to terminate all instances. To double check, if in windows, go to task manager and make sure no processes related to your server are running. That should do the trick.

Steve P.
  • 14,489
  • 8
  • 42
  • 72