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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:34:23+00:00 2026-05-24T04:34:23+00:00

In a application that I made, server accepts socket connection from clients and do

  • 0

In a application that I made, server accepts socket connection from clients and do various work. List of clients is stored in clients which has a LinkedList type.

I am using while statement with conditional statement based on size of the clients queue.

I want to know if there is a way to solve problems described below… so that server will not hang forever.

while(clients.size()>0){ //few clients the queue is alive at the moment
    //Suddenly, all clients in the queue shut down at this point in the while loop,
    //Another thread, which checks clients knows that clients are dead
    //so clients.size() becomes 0, but there is no way to check that at this point
    //and server is expecting for connection clients forever

    Socket socket= server.accept();
    socket.close();
}
//printout results

edit

The clients list is not just existing clients. It is a list of alive clients. This list is gets updated by another thread.

I have while statement above, because I want to do some job after there is no alive clients left. (either finished their job, or just died)

I want to solve this problem without using timeout exception, because clients will response in random time. (again, liveness of clients is checked by another Thread with heartbeat technique)

  • 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-24T04:34:23+00:00Added an answer on May 24, 2026 at 4:34 am

    You can set the timeout on the ServerSocket and catch the SocketTimeoutException:

    server.setSoTimeout(1000);
    while (clients.size() > 0) {
        try {
            server.accept();
        }
        catch (SocketTimeoutException e) {
            // Ignore
        }
    }
    

    This way you will check the status of the client list every second or so.

    Don’t forget to synchronize access to clients if you have other threads accessing it. Either wrap it using Collections.synchronizedList or put appropriate synchronization in the parts of your code that access the variable.

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

Sidebar

Related Questions

I have made a java application that stores data from a .csv file to
I have an application that needs to determine whether a user has made a
I have a server application that handles clients requests in different manner. I want
I have this application that has a form with a drop-down list and a
I have an application that is made up of the following: A central database
I have made an application that gives the user the option to open up
I've made an application that makes full use of ajax, and what I need
I made a SmartDevice application that runs on startup and I want it to
I have made a web application that uses master page for Login & Logout
I made some changes to an existing application that was previously just straight HTML

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.