Should I close outlets at both ends?

I have the following problem. My client program controls server availability on the local network (using Bonjour, but this is not a mater rally). Once the client has "noticed" the client application, the client tries to create socket: Socket(serverIP,serverPort);.

At some point, the client may lose the server (Bonjour says that the server is no longer displayed on the network). Thus, the client decides the closesocket because it is no longer valid.

At some point, the server appears again. Thus, the client tries to create a new socket associated with this server. But! The server may refuse to create this socket because it (the server) already has a socket associated with the client IP address and client port. This is because the socket was closed by the client, not the server. Could this happen? And if so, how can this problem be solved?

Well, I understand that it is unlikely that the client will try to connect to the server from the same port (client port), as the client selects its ports randomly. But it can still happen (by accident). Correctly?

+5
source share
6 answers

Yes, close the socket as soon as you detect a failure.

"" "close_wait", . , time_wait .

, , , .

( , IP-, , IP- .)

+2

/ , ( , ):

public class Server{

public static void main(String[] args) throws Exception {
    new Thread(){
        java.net.ServerSocket server = new java.net.ServerSocket(12345);
        java.util.ArrayList<java.net.Socket> l = new java.util.ArrayList<java.net.Socket>();
        public void run() {
            try{
            while(true){
                 java.net.Socket client = server.accept();
                System.out.println("Connection Accepted: S: "+client.getLocalPort()+", C: "+client.getPort());
                l.add(client);
            }
            }catch(Exception e){e.printStackTrace();}
        }
    }.start();
}

( - ):

import java.net.InetAddress;
import java.net.Socket;


public class SocketTest {
    public static void main(String[] args) throws Exception {
        InetAddress server = InetAddress.getByName("192.168.0.256");
        InetAddress localhost = InetAddress.getLocalHost();
        Socket s = new Socket(server, 12345, localhost, 54321);
        System.out.println("Client created socket");
        s.close();
        s = null;
        System.gc();
        System.gc();
        Thread.sleep(1000);
        s = new Socket(server, 12345, localhost, 54321);
        System.out.println("Client created second socket");
        s.close();
        System.exit(55);
    }
}

, , , "java.net.BindException: Address, : connect"

+2

: , .

, , , - ACK/NACK -.

ACK , ACK, .

-, . , , -. , , - java.nio-, .

, , , . , -, .

- / .

, - , , .

+1

. , . TIME_WAIT, .

.

, ping/pong, , .

+1

, ( Bonjour), .

- - , . IP-/ #, , , ( )

, , , Bonjour , . , , , TCP- ( , - IP-). , . , (, HTTP), . (, SSH), .

+1
source

If you close the server socket only if the socket is blocked, the client socket will be closed, but not vice versa.

otherwise, a socket at both ends would be better. Because a socket is a heavy weight for your system. It will use the local port and remote port of your system forever.

Thanks Sunil Kumar Sahoo

-1
source

All Articles