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

  • Home
  • SEARCH
  • 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 159259

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:49:37+00:00 2026-05-11T10:49:37+00:00

This is a follow up to: this question Basically, I have a server loop

  • 0

This is a follow up to:

this question

Basically, I have a server loop that manages a connection to one solitary client. At one point in the loop, if a ClientSocket exists it attempts a read to check if the client is still connected:

if (bufferedReader.read() == -1) {     logger.info("CONNECTION TERMINATED!");     clientSocket.close();     setUpSocket(); // sets up the server to reconnect to the client } else {     sendHeartBeat(); // Send a heartbeat to the client } 

The problem is, that once a socket has been created the application will hang on the read, I assume waiting for data that will never come, since the client never sends to the server. Before this was OK, because this correctly handled disconnects (the read would eventually fail when the client disconnected) and the loop would attempt reestablish the connection. However, I now have added the above sendHeartBeat() method, which periodically lets the client know the server is still up. If the read is holding the thread then the heartbeats never happen!

So, I assume I am testing if the connection is still up incorrectly. I could, as a quick hack, run the bufferedReader.read() in a seperate thread, but then I’ll have all sorts of concurrency issues that I really don’t want to deal with.

So the question is a few fold:

  1. Am I checking for a client disconnect correctly?
  2. If not, how should I do it?
  3. If I am doing it correctly how I do I get the read to not hold the process hostage? Or is threading the only way?
  • 0 0 Answers
  • 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. 2026-05-11T10:49:38+00:00Added an answer on May 11, 2026 at 10:49 am

    When you create your socket, first set a timeout:

    private int timeout    = 10000; private int maxTimeout = 25000;  clientSocket.setSoTimeout(timeout); 

    With this, if a read times out you’ll get java.net.SocketTimeoutException (which you have to catch). Thus, you could do something like this, assuming you’ve previously set the SO_TIMEOUT as shown above, and assuming that the heartbeat will always get a response from the remote system:

    volatile long lastReadTime;  try {     bufferedReader.read();     lastReadTime = System.currentTimeMillis(); } catch (SocketTimeoutException e) {     if (!isConnectionAlive()) {         logger.info('CONNECTION TERMINATED!');         clientSocket.close();          setUpSocket(); //sets up the server to reconnect to the client     } else {         sendHeartBeat(); //Send a heartbeat to the client     } }  public boolean isConnectionAlive() {     return System.currentTimeMillis() - lastReadTime < maxTimeout; } 

    A common way of handling this is setting the timeout to some number (say 10 seconds) and then keeping track of the last time you successfully read from the socket. If 2.5 times your timeout have elapsed, then give up on the client and close the socket (thus sending a FIN packet to the other side, just in case).

    If the heartbeat will not get any response from the remote system, but is just a way of ultimately generating an IOException earlier when the connection has fallen down, then you could do this (assuming that the sendHeartBeat itself will not throw an IOException):

    try {     if (bufferedReader.read() == -1) {         logger.info('CONNECTION TERMINATED with EOF!');         resetConnection();     } } catch (SocketTimeoutException e) {     // This just means our read timed out ... the socket is still good     sendHeartBeat(); //Send a heartbeat to the client } catch (IOException e) {     logger.info('CONNECTION TERMINATED with Exception ' + e.getMessage());     resetConnection(); }  ....  private void resetConnection() {     clientSocket.close();      setUpSocket(); //sets up the server to reconnect to the client } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 100k
  • Answers 100k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer break breaks out of one loop, but you can add… May 11, 2026 at 7:55 pm
  • Editorial Team
    Editorial Team added an answer You can join on the Employee table to find a… May 11, 2026 at 7:55 pm
  • Editorial Team
    Editorial Team added an answer Rails does this to a (IMHO) ridiculous extent. May 11, 2026 at 7:55 pm

Related Questions

This is a follow-up to two questions I asked a week or so back.
The question is basically a follow up to this thread: Using a 64 bit
Just wanted opinions on a design question. If you have a C++ class than
I'm writing a little desktop app that should be able to encrypt a data

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.