Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7985173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:29:14+00:00 2026-06-04T11:29:14+00:00

I want to implement a handler in my code to allow a client to

  • 0

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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T11:29:15+00:00Added an answer on June 4, 2026 at 11:29 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

given this class definition: public class Frame { IFrameStream CapturedFrom; } I want implement
I want to implement dynamic, client side file generation in javascript. Is it possible?
I want to implement Craig Andera's custom XML configuration handler in a slightly different
I want to implement drag&drop possibilities in my application. Here is my source code:
I am writing a particle transport code. In this code physical objects implement an
I want implement in my software solution an VBA editor but in c# 3.0.
Microsoft has announce that WindowsLiveID become a OpenID provider . I want implement it
I want to implement an autocomplete feature with images in my website. I would
I want to implement file uploading with behaviour which is commonly seen in various
I want to implement a function in the base class but I also want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.