marcuthh
marcuthh

Reputation: 596

Java - Where to detect and catch SocketException

I have a Java Server/Client application that allows client input until disconnection, using a while loop. This is done inside a ClientHandler class object that extends Thread and uses the run() method, so each connected client is being communicated with on it's own Thread.

This is what happens so far:

public void run()
{
    //receive and respond to client input
    try
    {
        //strings to handle input from user
        String received, code, message;

        //get current system date and time
        //to be checked against item deadlines
        Calendar now = Calendar.getInstance();

        //get initial input from client
        received = input.nextLine();                

        //as long as last item deadline has not been reached
        while (now.before(getLastDeadline()))
        {   
            //////////////////////////////////
            ///processing of client message///
            //////////////////////////////////

            //reload current system time and repeat loop 
            now = Calendar.getInstance();

            //get next input from connected client
            received = input.nextLine();
            //run loop again
        }
    }
    //client disconnects with no further input
    //no more input detected
    catch (NoSuchElementException nseEx)
    {
        //output to server console
        System.out.println("Connection to bidder has been lost!");
        //no system exit, still allow new client connection
    }
}

This all works fine, and the NoSuchElementException is handled when a client stops running their program (as there will be no subsequent input).

What I want to do is detect when a client Socket is disconnected from the server, so the server can update a display of currently connected clients. I have been told to do this by catching SocketException, and I have read up on this exception but am still a bit confused by how I would have to implement it.

From what I understand (although I could be wrong), a SocketException has to be caught on the client-side. Is this correct? If this is the case, can the SocketException run in harmony with the NoSuchElementException that I already have in place, or will that exception have to be removed/replaced?

A basic example of how to incorporate catching a SocketException would be a massive help, as I've not been able to find any relevant examples online.

Thanks,

Mark

Upvotes: 2

Views: 1093

Answers (1)

Pace
Pace

Reputation: 43817

You are already catching the SocketException actually. The nextLine call will (eventually) call read() on the underlying SocketInputStream returned by the Socket. This call to read() will throw a SocketException (which is a subclass of IOException). The Scanner class will catch the IOException and then return the NoSuchElementException. So there really isn't anything more you need to do.

You can access the actual SocketException if you want by calling ioException on the Scanner once you've caught the NoSuchElementException. Also, if you're trying to keep track of a list of connected clients then this will have to be done on the server side. You can catch a SocketException on the client side but that would indicate that the server has disconnected unexpectedly which isn't really what you're looking for.

Upvotes: 1

Related Questions