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 7173501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:50:13+00:00 2026-05-28T15:50:13+00:00

My client application needs to be able to recover in several situations. When the

  • 0

My client application needs to be able to recover in several situations.
When the power is turned on the client reads the address and port from a file and attempts to connect to the server.
If this is the first time this client has been used on this host a default IP address is tried but this may be incorrect.
The user then needs to enter the correct IP address. The port is hard coded.
The way it works now is a one second swing.Timer attempts to get the status of the server…

statusTimer = new Timer(ONE_SECOND, new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        final byte GET_STATUS = 20;
        SendCommand(GET_STATUS, null);
    }
});
statusTimer.start();

If there is no connection the user is asked to connect…

private void SendCommand(byte command, byte[] parameters) {
    if ((null == socket) || (null == outputStream) || socket.isClosed())
        if (JOptionPane.YES_OPTION == 
            JOptionPane.showConfirmDialog(null, "Connect?")) 
            Connect();
            ...

private void Connect() {
    String s = (String) 
        JOptionPane.showInputDialog(null, "Enter IP Address", ipAddress);
    if (validateIp(s)) {
        ipAddress = s;
        connectTask = new ConnectTask();
        connectTask.start();
        ...

The socket is opened, the io streams are set, and an RxThread is started.

class ConnectTask extends Thread {
    @Override public void run() {
        try {
            if (null == socket) socket = new Socket(ipAddress, TCP_PORT);
            if (null == outputStream) outputStream = socket.getOutputStream();
            if (null == inputStream) inputStream = socket.getInputStream();
            if (null == rxThread) rxThread = new RxThread();
            ...

There are a few bad things that could happen at this point, for example:
1. The server could be off when the client is turned on. What is the best way for the client to automatically open the socket, set the io streams, and continue when the server is powered on?
2. In the same way, the server might see a power cycle during a read, write, or neither. Is there a good way to automatically recover after power is restored?
3. The IP address may need to be corrected as mentioned above.
It would be nice to handle these and other situations without requiring the user to close and open the client application or power cycle the system.
What kinds of approaches are needed for a bullet-proof java client socket?
This and this are similar but deal with a server sockets

  • 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-05-28T15:50:14+00:00Added an answer on May 28, 2026 at 3:50 pm

    You need to be able to distinguish between first-time usage (when the server details may be wrong, and the user should be prompted) and later usage (when the server may just be down). You could save a file when the user first enters the IP, for example.

    After that, you probably want something like: an inner loop where you attempt to connect to the server and read data, with a timeout. If the socket is closed due to a network error then this can be caught with a try-catch block. Put all this into an outer loop so that you try again when disconnected.

    Depending on the details, and assuming the amount of data is small, it may well be easiest for your client to open a connection every time rather than attempting to maintain a connection, then sleep in between connections.

    If the server is unresponsive, your current code (which creates a new ConnectTask thread each second using a Timer) may end up creating multiple ConnectTasks, one created per second, each trying to connect in parallel, though it’s hard to be certain from the partial code you posted.

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

Sidebar

Related Questions

I'm writing a client/server application that requires the server needs to be able to
I need to be able to send an email from a silverlight client-side application.
We have a client application that needs to send messages to a server for
I have a .NET client application that needs to communicate with a server using
I have a client server based windows forms application that needs an administrator only
My client needs a Pocket PC application to remotely configure an industrial sensor via
I have a Silverlight application that needs to retrieve some data from my database.
I'm building an application that needs to use a web server like a file
My application needs to be able to post to https and preserve the session
We are writing an application which needs a dummy user as a socket.io client

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.