How to close port after using server sockets

16,428

Solution 1

Have you tried calling setReuseAddress(true) on the server socket? It's normal for the TCP state machine to enter TIME_WAIT state. For detailed explanation look here.

Solution 2

It is because there is a cooldown period implemented on some O.S. If you wait a little (a couple of minutes for example), then you should be able to access the port again.

That being said, it is not a good idea to open and close serversockets on a port. It is better to open it and keep it open as long as necessary. Then close it when you are done. Don't open/close/open/close/open/close...

Share:
16,428
dan
Author by

dan

Updated on July 04, 2022

Comments

  • dan
    dan almost 2 years

    In my application I create a ServerSocket instance with some port. After I am finished, I close the socket, but when I try to make a new ServerSocket on the same port, it throws:

    "java.net.BindException: Address already in use"
    

    If I create the ServerSocket with a different port, then it works.

    ServerSocket.isClosed also returns true

    What is the problem?

    public void run() {
        try {
            BufferedInputStream bufferedinputstream = new BufferedInputStream(
                                                            new FileInputStream(fileReq));
            BufferedOutputStream outStream = new BufferedOutputStream(
                                                            cs.getOutputStream());
            byte buffer[] = new byte[1024];
    
            int read;
            System.out.println(cs);
            while ((read = bufferedinputstream.read(buffer)) != -1)
            {
                outStream.write(buffer, 0, read);
                outStream.flush();
            }
            System.out.println("File transfered");
            outStream.close();
            bufferedinputstream.close();
    
            try {
                this.finalize();
            } catch (Throwable e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }
    } catch (Exception e) {
        System.out.println("Exce....");
        System.out.println(e.getMessage());
    }
    finally
    {
        if ( cs != null)
            try {
                int usedPort=cs.getLocalPort();
                System.out.println("Closing "+cs);
                cs.close();
                System.out.println(cs+" Closed");
                System.out.println("asd"+cs.isClosed());
                portManager.getInstance().mp.put(usedPort,true);
            } catch (IOException e) {
                System.err.println("in sendToClient can't close sockt");
                System.err.println(e.getMessage());
            }
    }
    
  • dan
    dan almost 13 years
    well this what i'm doing(like you can see in my code)... but it's not helping!!!! i try evrey thing to free this port..
  • dan
    dan almost 13 years
    i wait for 10 min but still it's not let mt use this port agien until i close my server program...
  • dan
    dan almost 13 years
    first thanks for the help all of you. and yes i tried this (i need you use it when the socket is start right?) any way it's not work the deam port allways in in use "Address already in use: JVM_Bind" grrr how its can possible that i can't use that port all over the program run until i close the java program.. must be a some way
  • Code Painters
    Code Painters almost 13 years
    What platform is it? If it's Linux, try netstat -pan | grep 1234 and see the state. On windows you can try netstat -an as well.
  • user207421
    user207421 over 6 years
    BuuferedInputStreams are never equal to Strings, and == is not the correct way to test that even if they were.