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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:28:34+00:00 2026-06-08T12:28:34+00:00

I’m trying to build a chat server-client program in Java. But the problem is

  • 0

I’m trying to build a chat server-client program in Java. But the problem is that after a point in the code, it stops execution and for the life of me, I cannot figure out why. I’m attaching the code here. I’m new to multithreading and socket programming, so it’s possible the error is quite obvious but I’m completely missing it.

public class ChatClient implements Runnable
{  private Socket socket              = null;
private Thread thread1              = null;
private ObjectOutputStream streamOut = null;
private ChatClientThread client    = null;
private Message sendMsg = null;
private String username = null;
private DataInputStream console = null;
private Scanner s = new Scanner(System.in);
String text;

public ChatClient(String serverName, int serverPort)
{  System.out.println("Establishing connection. Please wait ...");
   try
     {  socket = new Socket(serverName, serverPort);
     System.out.println("Connected: " + socket);
     System.out.println("Enter your username:");
     username = s.nextLine();
     start();
  }
  catch(UnknownHostException uhe)
  {  System.out.println("Host unknown: " + uhe.getMessage()); }
  catch(IOException ioe)
  {  System.out.println("Unexpected exception: " + ioe.getMessage()); }
}

public void run()
{  while (thread1 != null)
  {  try
     {  sendMsg = new Message();
        sendMsg.setMsg(s.nextLine());
        System.out.println(sendMsg.getMsg()+ " check");
        streamOut.writeObject(sendMsg);
        streamOut.flush();
     }
     catch(IOException ioe)
     {  System.out.println("Sending error: " + ioe.getMessage());
        stop();
     }
  }
}

    public void handle(String user, Message msg)
{  System.out.println("1");
   if (msg.getMsg().equals(".bye"))
  {  System.out.println("Good bye. Press RETURN to exit ...");
     stop();
  }
  else
     System.out.println(msg.getMsg());
   System.out.println("Msg received");
}

   public void start() throws IOException
{   

    //console = new DataInputStream(System.in);
    System.out.println("1");
    streamOut = new ObjectOutputStream(socket.getOutputStream());
    System.out.println("3");

  if (thread1 == null)
  {  client = new ChatClientThread(this, socket, username);
     System.out.println("Started new ChatClientThread");
     thread1 = new Thread(this);                   
     thread1.start();
  }
  else
      System.out.println("This code is stupid.");
  }

   public void stop()
{  if (thread1 != null)
   {  thread1.stop();  
      thread1 = null;
   }
  try
  {  if (console   != null)  console.close();
     if (streamOut != null)  streamOut.close();
     if (socket    != null)  socket.close();
  }
  catch(IOException ioe)
  {  System.out.println("Error closing ..."); }
  //client.close();  
  client.stop();
}

    public static void main(String args[])

 {  ChatClient client = null;
  //if (args.length != 2)
    // System.out.println("Usage: java ChatClient host port");
  //else
     client = new ChatClient("localhost", 2008);
}
 }

So the way it’s working is, it starts from the main function, goes to the Constructor, takes in username and everything and proceeds to start(). I’m assuming start works, because it prints 1 & 3, but after that I keep entering text but it just won’t proceed to the next point (I know that because it doesn’t print “Started new ChatClientThread”).
Any help would be appreciated. I’ve been working on this code for hours, and I just cannot figure out why the execution stops there.

UPDATE

I edited the ChatClient.start code

    public void start() throws IOException
   {    

    //console = new DataInputStream(System.in);
    System.out.println("1");
    streamOut = new ObjectOutputStream(socket.getOutputStream());
    System.out.println("3");

    if (thread1 == null)
    {  
        System.out.println("Started new ChatClientThread");
        client = new ChatClientThread(this, socket, username);
        System.out.println("Started new ChatClientThread");
        thread1 = new Thread(this);                   
        thread1.start();
    }
    else
        System.out.println("This code is stupid.");
    }

I now know that it does indeed run the constructor of ChatClientThread:

    public ChatClientThread(ChatClient _client, Socket _socket, String uname)
   {  System.out.println("Constructor started");
  client   = _client;
  socket   = _socket;
  username = uname;
  System.out.println("1");
  open();
  System.out.println("2");
  start();
  System.out.println("3");

   }

It prints 1, goes on to ChatClientThread.open:

   public void open()
   {  try
  {     streamIn  = new ObjectInputStream(socket.getInputStream());

  }
  catch(IOException ioe)
  {  System.out.println("Error getting input stream: " + ioe);
     client.stop();
  }
  }

But here is where it gets stuck again. It doesn’t proceed to print 2, so I assume it doesn’t move to ChatClientThread.start piece of code.

  • 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-08T12:28:35+00:00Added an answer on June 8, 2026 at 12:28 pm

    Okay, I just read the FAQ and it’s okay to answer your own question. So here goes.

    Solution Found: ObjectInputStream(socket.getInputStream()); does not work

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text

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.