简体   繁体   中英

How to kill open server sockets under windows7?

I wrote a simple server client socket program and when I recompile the server I get:

java.net.BindException: Address already in use: JVM_Bind
    at java.net.DualStackPlainSocketImpl.bind0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketBind(Unknown Source)
    at java.net.AbstractPlainSocketImpl.bind(Unknown Source)
    at java.net.PlainSocketImpl.bind(Unknown Source)
    at java.net.ServerSocket.bind(Unknown Source)
    at java.net.ServerSocket.<init>(Unknown Source)
    at java.net.ServerSocket.<init>(Unknown Source)

Therefore my question is how to kill the socket under windows 7? Is there a possible solution to kill it in eclipse?

I appreciate your answer!!

Kill the jvm this fixed the issue when I ran into it. Are you closing the socket in your code before you stop your simple server?

Like RGdev I assume that you still have a javaw process running in the background which keeps the connection open. But it could also be a different server program on your machine which hogs the port you want to use.

You can find out which processes are listening to which port with the netstat command in the cmd shell. The following parameters list (a) all connections including servers, (b) shows the executable which opened the connection and (n) suppresses the substitution of port numbers with service names for well-known ports.

netstat -abn

Here is my code for server side:

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

public class ServerSide {


    public static void main(String[] args) {

        try
        {
            ServerSocket myServerSocket = new ServerSocket(9999);
            System.out.println("Server is waiting on host" + InetAddress.getLocalHost().getCanonicalHostName() + "port= "+ myServerSocket.getLocalPort());
            Socket skt = myServerSocket.accept();

            BufferedReader myInput = new BufferedReader(new InputStreamReader(skt.getInputStream()));
            PrintStream myOutput = new PrintStream(skt.getOutputStream());

            String buf = myInput.readLine();
            System.out.println("Server readLine");
            if(buf!=null)
            {
                System.out.println("Buf = " + buf);
                myOutput.print("Got it?");
            }
            else
            {
                System.out.println("Nothing returned in server sidex`x  ");
            }
            skt.close();
            System.out.println("Server shutdown");
        }
        catch(IOException e)
        {
            e.printStackTrace();
            System.out.println("Whooops");
        }
    }

}

As you can see for clean-up I've written:

        skt.close();

But maybe this is not your problem, Maybe your problem is which I had 1 hour ago ;) I used to run a program but the result is not what I expected so I modify it and run it again but the port was busy or already in use! What I do on eclipse? Under the Console where you get the error, on the right side of the window there is red colour rectangle button! It say "Terminate". If you click on that your port will be free. By the way don't forget to check the console for both(Server/Client) sides.

You can also get this error message, when the process already terminated. TCP has a time wait state. This state is used to ensure that no TCP packets from an old connection can be delivered to a new process listening at the same port. Normally you should use the ServerSocket.setRuseAddress(true) to avoid this issue.

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