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

The Archive Base Latest Questions

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

When a client disconnects using CTRL-C or the connection is terminated the server should

  • 0

When a client disconnects using CTRL-C or the connection is terminated the server should produce a message stating that it has done so rather then producing an exception as follows:

Exception in thread “Thread-1” java.lang.NullPointerException
at ChatServerThread.handleClient(ChatServerThread.java:31)
at ChatServerThread.run(ChatServerThread.java:17)

It began exhbiting this behavior after I converted from read/writeUTF() to readLine() and writeByte() respectively, and having the server respond to the client with what the client sent. See Exception in thread for details.

Question is how to get the EOFException to functionality to work again so that Client closed the connection. is printed verse the exception message. Line 31 is if( nextCommand.equals(".bye") ) {

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 BufferedReader 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 ) { \\This does not seem to be working now and it previously was
        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.readLine();
        if( nextCommand.equals(".bye") ) {
           System.out.println("Client disconnected with bye.");
           done = true;
        } else {
           System.out.println( nextCommand );
           String nextReply = "You sent me: " + nextCommand.toUpperCase() + '\n';
           streamOut.writeBytes ( nextReply );
        }
     }
   } finally {
     streamIn.close();
     streamOut.close();
     socket.close();
   }
   }
   public void open() throws IOException
   {
      streamIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
      streamOut = new DataOutputStream(socket.getOutputStream());
   }
   public void close() throws IOException
   {  if (socket != null)    socket.close();
      if (streamIn != null)  streamIn.close();
      if (streamOut != null) streamOut.close();
   }
}
  • 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-30T15:23:44+00:00Added an answer on May 30, 2026 at 3:23 pm

    The exception is thrown from the second line:

    String nextCommand = streamIn.readLine();
    if( nextCommand.equals(".bye") ) {
    

    Obviously the nextCommand is null. streamIn is a BufferedReader, quoting JavaDoc of readLine():

    Returns:

    A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached

    This is a different behaviour when compared to DataInputStream.readUTF():

    Returns:

    a Unicode string.

    Throws:

    EOFException – if this input stream reaches the end before reading all the bytes.

    My guess is that Ctrl + C interrupts blocking readLine() and signalling end of stream.

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

Sidebar

Related Questions

I have a client application that should send notify messages to an optional server
I have written a simple server - client program with Swing interface using singleton
I'm using Indy10 under Delphi2009. I have a server/client application, with TidTCPServer and TidTCPClient.
I am writing a auction server and client and using a class called BidHandler
I am using select() statement that handles multiple client connections using Unix C sockets.
I am trying to deal with client disconnects in a simple java application that
I'm using a tcp socket connection between an iPhone app and a NodeJS server
Client side form validation becomes easiest by using jQuery. I tried all validators and
A client of mine has asked me to integrate a 3rd party API into
My client has a multi-page PDF file. They need it split by page. Does

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.