How to determine that the current client socket has closed and connect to another server?

I want to implement a handler in my code to allow a client to automatically connect to a second instance of a server on the same network if the server connected to this client is not responding after 10 seconds. What am I doing wrong??

private static void connectToServer(String serverHostname, int port) throws UnknownHostException, IOException
    {
        try {
            echoSocket = new Socket(serverHostname, 10118);
            out = new PrintWriter(echoSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(echoSocket
                    .getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Unknown host: " + serverHostname);
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Not connected to server: "
                    + serverHostname);
            System.exit(1);
        }
    }





 public static void main(String[] args) throws IOException {
            listServer.add("10.196.113.31");
            listServer.add("10.196.113.27");
            BufferedReader stdIn = new BufferedReader(new InputStreamReader(
                    System.in));
            try {


            if (args.length > 0)
                serverHostname = args[0];
            System.out.println("Trying to connect to host " + serverHostname
                    + " at port 10118.");


            connectToServer(getServer(), 10118);
            echoSocket.setSoTimeout(10000);

            String userInput;
            System.out.print("Input: ");

                while ((userInput = stdIn.readLine()) != null) {
                    out.println(userInput);

                    if (!echoSocket.isConnected()) connectToServer(serverHostname, 10118);

                    System.out.println("echo: " + in.readLine());
                    System.out.print("Input: ");
                }
            } catch (SocketTimeoutException e) {
                NextServer();
                connectToServer(getServer(), 10118);
                System.out.println("Timeout");
                // TODO: handle exception
            }


            out.close();
            in.close();
            stdIn.close();
            echoSocket.close();
        }

        private static void NextServer(){
            idServer ++;        
        }

        private static String getServer(){
            return listServer.get(idServer);
        }

EDIT

When I run the server, then the client, everything is fine. Then I start the second server on another machine on the same network, and try to send text from the same client, the client should automatically detect that the first server is disconnected, connect to the second instance of the server and send the text message. Here is the exception i am getting:

Exception in thread "main" java.net.SocketException: Connection reset at java.net.SocketInputStream.read(Unknown Source) at sun.nio.cs.StreamDecoder.readBytes(Unknown Source) at sun.nio.cs.StreamDecoder.implRead(Unknown Source) at sun.nio.cs.StreamDecoder.read(Unknown Source) at java.io.InputStreamReader.read(Unknown Source) at java.io.BufferedReader.fill(Unknown Source) at java.io.BufferedReader.readLine(Unknown Source) at java.io.BufferedReader.readLine(Unknown Source) at Q3.Client.main(Client.java:62)

Upvotes: 0

Views: 330

Answers (2)

Marko Topolnik
Marko Topolnik

Reputation: 200138

If the server is not responding to connect, your setSoTimeout will not cover that case, so make sure you have a timeout in the connection establishment code. Write this:

SocketAddress a = new InetSocketAddress(serverHostname, 10118);
echoSocket = new Socket();
echoSocket.connect(a, 10000);

Upvotes: 1

matt b
matt b

Reputation: 139921

If you want your client to do something when the server disconnects the connection, you need to do more than just catch SocketTimeoutException. As the stacktrace shows, a dropped connection will result in a generic SocketException being thrown. You should catch this also.

In other words, it looks like you are only handling the case where the initial connection to the server from the client fails; and you are not handling the case where an existing condition is dropped.

Upvotes: 3

Related Questions