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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:23:52+00:00 2026-05-30T11:23:52+00:00

Based on examples here , the simple socket server client will terminate when .bye

  • 0

Based on examples here, the simple socket server client will terminate when .bye is sent. The code below uses a thread-based approach to accept multiple clients but does not handle graceful handle client termination and will error out by repeating null.

The first example in the aforementioned link causes the client and server to exit. The server code below does not have a provision to disconnect when “.bye” is received. I would like the client to gracefully disconnect from the server. The server should close the connection when the “.bye” is received from the client. I believe this needs to be handled in the System.out.println(streamIn.readUTF()); in the ChatServerThread?

Question updated to reflect answer feedback, see history for original code:

import java.net.*;
import java.io.*;

public class ChatServerThread implements Runnable
//public class ChatServerThread extends Thread
{  private Socket          socket   = null;
   private ChatServer      server   = null;
   private int             ID       = -1;
   private DataInputStream streamIn =  null;
   private DataOutputStream streamOut = null;

   public ChatServerThread(ChatServer _server, Socket _socket)
   {  server = _server;  socket = _socket;  ID = socket.getPort();
   }
   public void run() {
   try {
       handleClient();
   } catch( EOFException eof ) {
        System.out.println("Client closed the connection.");
   } catch( IOException ioe ) {
        ioe.printStackTrace();
   }
}

   public void handleClient() throws IOException {
      boolean done = false;
      try {
      System.out.println("Server Thread " + ID + " running.");
      while (!done) {
        String nextCommand = streamIn.readUTF();
        if( nextCommand.equals(".bye") ) {
           done = true;
        } else {
           System.out.println( nextCommand );
        }
     }
   } finally {
     streamIn.close();
     streamOut.close();
     socket.close();
   }
   }
   public void open() throws IOException
   {
      streamIn = new DataInputStream(new BufferedInputStream(socket.getInputStre
am()));
      streamOut = new DataOutputStream(new BufferedOutputStream(socket.getOutput
Stream()));
   }
   public void close() throws IOException
   {  if (socket != null)    socket.close();
      if (streamIn != null)  streamIn.close();
      if (streamOut != null) streamOut.close();
   }
}

The first error is due to implements Runnable. The second I am not sure though java.io.* is imported so I am not sure why it’s complaining.

ChatServer.java:34: error: cannot find symbol
         client.start();
               ^
  symbol:   method start()
  location: variable client of type ChatServerThread
  • 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-30T11:23:54+00:00Added an answer on May 30, 2026 at 11:23 am

    Generally speaking you don’t shutdown a server because a client said bye. That means any user could connect to your server and remotely shutdown without your control. Shutting down the server is handled by the admin or owner of the server, and generally done by running a command on the server through an authenticated ssh session. On unix/linux you might run:

    service chatserver stop
    

    That’s another topic how to make that work, but just to give you a general overview of best practices. Now if you want to shutdown a client when the conversation is over. That makes more sense:

    public class ChatServerThread implements Runnable {
    
        .......
    
        public void run() {
            try {
                handleClient();
            } catch( EOFException eof ) {
                System.out.println("Client closed the connection.");
            } catch( IOException ioe ) {
                ioe.printStacktrace();
            }
        }
    
        public void handleClient() throws IOException {
           boolean done = false;
           try {
              while(!done) {  
                 String nextCommand = streamIn.readUTF();
                 if( nextCommand.equals(".bye") ) {
                    done = true;
                 } else {
                    System.out.println( nextCommand );
                 }
              }
           } finally {
              streamIn.close();
              streamOut.close();
              socket.close();
           }
        }
    }
    

    Then you can free up that thread to service the next client, or simply shut it down. With DataInputStream you will get a EOFException when the client closes the socket without sending a “.bye”. So you can catch the EOFException before the IOException, and simply shutdown.

    Notice I did not subclass Thread. Instead I implemented a Runnable which gives you more flexibility in the future should you want to create a Thread pool or something fancier.

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

Sidebar

Related Questions

I try to implement simple server on python based on HTTPServer. How can i
I write soap-client based on Delcampe API. Simple methods work fine, but functions with
I'm learning andEngine and trying to make a simple game based on some examples.
I've got the following code which is based off an example i found here
I'm looking for examples where a human-based service replaced an automated one. For example,
Based on their work, how do you distinguish a great SQL developer? Examples might
I would like to look at some examples of some good form layouts (web-based)
I have an app based on the CoreDataBooks example that uses an addingManagedObjectContext to
Please look at the two examples below: irb(#<ActionView::Base:0x2863d58>):030:0> paintings_path => /some-nice-alias-path irb(#<ActionView::Base:0x2863d58>):029:0> self.controller_name.to_s +
I'm trying to create a simple WebSocket example using the HTML5/JS API. Based on

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.