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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:52:18+00:00 2026-06-13T12:52:18+00:00

I am currently trying to write a very simple chat application to introduce myself

  • 0

I am currently trying to write a very simple chat application to introduce myself to java socket programming and multithreading. It consists of 2 modules, a psuedo-server and a psuedo-client, however my design has lead me to believe that I’m trying to implement an impossible concept.

The Server

The server waits on localhost port 4000 for a connection, and when it receives one, it starts 2 threads, a listener thread and a speaker thread. The speaker thread constantly waits for user input to the console, and sends it to the client when it receives said input. The listener thread blocks to the ObjectInputStream of the socket for any messages sent by the client, and then prints the message to the console.

The Client

The client connects the user to the server on port 4000, and then starts 2 threads, a listener and s speaker. These threads have the same functionality as the server’s threads, but, for obvious reasons, handle input/output in the opposite way.

The First Problem

The problem I am running into is that in order to end the chat, a user must type “Bye”. Now, since my threads have been looped to block for input:

    while(connected()){

        //block for input
        //do something with this input
        //determine if the connection still exists (was the message "Bye"?)
    }

Then it becomes a really interesting scenario when trying to exit the application. If the client types “Bye”, then it returns the sending thread and the thread that listened for the “Bye” on the server also returns. This leaves us with the problem that the client-side listener and the server-side speaker do not know that “Bye” has been typed, and thus continue execution.

I resolved this issue by creating a class Synchronizer that holds a boolean variable that both threads access in a synchronized manner:

public class Synchronizer {

    boolean chatting;

    public Synchronizer(){

        chatting = true;
        onChatStatusChanged();
    }

    synchronized void stopChatting(){

        chatting = false;
        onChatStatusChanged();
    }

    synchronized boolean chatting(){

        return chatting;
    }

    public void onChatStatusChanged(){

     System.out.println("Chat status changed!: " + chatting);
    }
}

I then passed the same instance of this class into the thread as it was created. There was still one issue though.

The Second Problem

This is where I deduced that what I am trying to do is impossible using the methods I am currently employing. Given that one user has to type “Bye” to exit the chat, the other 2 threads that aren’t being utilized still go on to pass the check for a connection and begin blocking for I/O. While they are blocking, the original 2 threads realize that the connection has been terminated, but even though they change the boolean value, the other 2 threads have already passed the check, and are already blocking for I/O.

This means that even though you will terminate the thread on the next iteration of the loop, you will still be trying to receive input from the other threads that have been properly terminated. This lead me to my final conclusion and question.

My Question

Is it possible to asynchronously receive and send data in the manner which I am trying to do? (2 threads per client/server that both block for I/O) Or must I send a heartbeat every few milliseconds back and forth between the server and client that requests for any new data and use this heartbeat to determine a disconnect?

The problem seems to reside in the fact that my threads are blocking for I/O before they realize that the partner thread has disconnected. This leads to the main issue, how would you then asynchronously stop a thread blocking for I/O?

I feel as though this is something that should be able to be done as the behavior is seen throughout social media.

Any clarification or advice would be greatly appreciated!

  • 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-13T12:52:19+00:00Added an answer on June 13, 2026 at 12:52 pm

    Just to clarify for anyone who might stumble upon this post in the future, I ended up solving this problem by tweaking the syntax of my threads a bit. First of all, I had to remove my old threads, and replace them with AsyncSender and AsyncReader, respectively. These threads constantly send and receive regardless of user input. When there is no user input, it simply sends/receives a blank string and only prints it to the console if it is anything but a blank string.

    The Workaround

    try{        
        if((obj = in.readObject()) != null){
    
        if(obj instanceof String)
            output = (String) obj;
    
        if(output.equalsIgnoreCase("Bye"))
        s.stop();
        }
    }
    catch(ClassNotFoundException e){
    
        e.printStackTrace();
    }
    catch(IOException e){
    
        e.printStackTrace();
    }
    

    In this iteration of the receiver thread, it does not block for input, but rather tests if the object read was null (no object was in the stream). The same is done in the sender thread.

    This successfully bypasses the problem of having to stop a thread that is blocking for I/O.

    Note that there are still other ways to work around this issue, such as using the InterruptableChannel.

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

Sidebar

Related Questions

I am trying to write a very simple Android application that checks the signal
I am currently trying to write a very simple app that sends an object
Currently am trying to read and write mp3 tags without using any external modules
This is a very simple game I'm trying to write but I have a
Currently I am trying something very simple. I am looking through an XML document
I am trying to write a somewhat simple todo application using knockout.js and jquery
I'm trying to write a file system iterator as a simple Windows Application. I
I am trying to write a very simple rails backend backed up with a
I'm trying to write a very simple/minimal custom video player in Flash CS3 I
I'm trying to write very simple program which will imitate simple DeadLock, where Thread

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.