开发者

With java multi-thread Server/Client, How to tell if client thread closed?

开发者 https://www.devze.com 2023-04-11 11:58 出处:网络
I am making a Server/Client program in Java. The server waits for a connection then makes a new thread for that connection. The server then goes back and listens for connections on other ports.

I am making a Server/Client program in Java. The server waits for a connection then makes a new thread for that connection. The server then goes back and listens for connections on other ports.

sck = srvrSckt.accept();
server.numConnections++;
System.out.print("Connection was made on " + server.port[i] + ".\n");
Connections conn = new Connections(sck);
server.threads[i] = new Thread(conn);
server.threads[i].start();

After a connection has been made, I want to go back and check if any previous connections have closed. Then I will mark these ports as available for futu开发者_高级运维re connections to be made on. Is there a way to do this?

I have read that the best way to communicate between threads is with a shared variable. However, I cannot find a good example of this. Also, I am not sure if a shared variable would work because I not only want to check for connections that were successfully closed, but also for connections that were disconnected abruptly.

Any help you can offer will be greatly appreciated. Thank-you.

EDIT:

Here is a larger portion of the code. That may make my question more clear.

if (server.isAvailable[i] == true)
{
    availablePort = server.allPorts[i];
    server.isAvailable[i] = false;
    ServerSocket srvrSckt = new ServerSocket(availablePort);
    System.out.println("Waiting for connection on port " + availablePort + "...");
    srvrSckt.setSoTimeout(5000);
    try {
        sck = srvrSckt.accept();
        server.numConnections++;
        System.out.print("Connection was made on " + server.port[i] + ".\n");
        Connections conn = new Connections(sck);
        server.threads[i] = new Thread(conn);
        server.threads[i].start();
    } catch (Exception e) {
    server.isAvailable[i] = true; // no connection was made
    srvrSckt.close();
}


To my understanding, you have absolutely no control over assigning ports to incoming remote TCP connections. That's the job of the operating system. A socket is closed when Socket.isClosed returns true or when Socket.getInputStream.read returns -1 indicating end of stream.


After a connection has been made, I want to go back and check if any previous connections have closed

Normally , the best way to detect on the server the end of a session with a client is to set up of the "goodbye-protocol" between client and server. A fixed token is posted by the client to server and server acknowledges by closing the channel. If the client terminates abruptly , then the only way on server to know about this is to setup a heart-beat protocol, every certain time of inactivity , the server will ping client and the client must respond by pingback. If there is no response , server must terminate the client session.

As for sharing a variable between threads , you may refer : http://download.oracle.com/javase/tutorial/essential/concurrency/sync.html

0

精彩评论

暂无评论...
验证码 换一张
取 消