Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Sockets: How can server detect that client is no longer connected?

843790Mar 22 2007 — edited Mar 22 2007
Hi,

I really need help and advice with the following problem:

I have a Client - Server socket program.
The server listens on port 30000 using a server socket on one machine
The client connects to localhost on port 20000, previously creating an ssh port forward connection using the Jsch package from www.jcraft.com with
"session.setPortForwardingL(20000, addr, 30000);"
Then the client sends Strings to the server using a PrintWriter.
Both are connected to each other through the internet and the server uses a dynamic dns service.

This all works well until the IP address of the Server changes, The client successfully reconnects to the server using the dynamic dns domain name, but the server keeps listening on the old socket from the previous connection, while opening a new one for the new client connection. The server doesn't seem to notice that Client has disconnected because of this IP address change.

looks like the server is stuck inside the while loop. If i cut the connection manually on the client side, the server seems to notice that the client has disconnected, and jumps out of the while look (see code below)

this is the code I'm using for the server:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.logging.Logger ;

public class SocketHandler extends Thread {

    static Logger logger = Logger.getLogger("Server.SocketHandler");

    private Socket clientSocket = null;

    private BufferedReader in = null;

    private InputStreamReader inReader = null;

    public SocketHandler(Socket clientSocket) throws IOException {
        this.clientSocket = clientSocket;
        inReader = new InputStreamReader(clientSocket.getInputStream ());
        in = new BufferedReader(inReader);
    }

    public void run() {

        try {
            String clientMessage = null;
            while ((clientMessage = in.readLine()) != null) {
                logger.info("client says: " + clientMessage);
            }
           
        } catch (IOException e) {
            logger.severe(e.getMessage());
            e.printStackTrace();
        } finally {

            try {
                logger.info("closing client Socket: " + clientSocket);
                clientSocket.close();
                in.close();
                ServerRunner.list.remove(clientSocket);
                logger.info("currently "+ServerRunner.list.size()+" clients connected");
            } catch (IOException e) {
                logger.severe (e.getMessage());
                e.printStackTrace();
            }
        }

    }
}
I've tried making the server create some artificial traffing by writing some byte every few seconds into the clients OutputStream. However I get no exceptions when the IP address changes. The server doesn't detect a disconnected socket connection.
I'd really appreciate help and advice
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 19 2007
Added on Mar 22 2007
4 comments
1,056 views