0

So I'm in the making of a very simple server/client complex using java. So far I have managed to figure out what happens if the client quits, because then the server receives null while listening from any input from the client.

BUT - what happens if the client is connected and the server quits for any reason... the server is supposed to wait for input from the client, but how can the client know that the server is not listening anymore? For me the clients call to the server just goes into the void... nothing happens...

Can I do something to find out when the server goes down? Time-out, ping/pong or something?

As You surely can see I'm quite new at this, I'm just curious. This was a puzzle for me ever since I attended computer science at the university.

Thanks in advance. dr_xemacs.

dr_xemacs
  • 51
  • 4

3 Answers3

0

(I am assuming you are working with blocking server socket and socket and not with non blocking ones) Similarly to the server, reading from streams of a closed connection will return null.

However if you instead do not want to rely on this or a scared that the connection to the server could somehow persist, you can also use time outs (check this out! ) which will throw SocketTimeoutException when the time is up and, to keep track of whether the server is up or not, create a ping/packet to assure server is still up and running.

Edit: I did a quick search and this could be useful to you! Take a look!

Leonardo
  • 25
  • 8
0

How can the client know that the server is not listening anymore?

If the client doesn't attempt to interact at some level with the service, it won't know.

Assuming that the client has sent a request, a few different scenarios.

  • If the service is no longer listening on the designated port, the client will typically get a "Connection Refused" exception.

  • If the service is still running (in a sense) but it is not working properly, then connection attempts from the client are likely to time out.

  • If the service's host is down, the client liable get a timeout.

  • If there are network connectivity or firewall issues, the client could get a timeout or some other exception.

Can I do something to find out when the server goes down? Time-out, ping/pong or something?

You attempt to connect and send a request. If it fails or times out, that means the service is down. If you are designing and implementing the service yourself, you could include a special "healthcheck" request for clients to "ping" on. But the flip-side is that network and server resources will be consumed in receiving and responding to these requests. It can affect your ability to scale up the number of clients, for example, if each client pings the service every N seconds.

But a client typically doesn't need to know whether the service is up or down. It typically only cares that service responds when it it sends a real request. And the simplest way to handle that is to just send the request and deal with the outcome. (The client code has to deal with all possible outcomes anyway when doing a real request. The service can go down, etc between the last healthcheck ping and when the client sends a real request.)

Bottom line: Don't bother with checking availability in the client unless the application (i.e. the end user) really needs to know.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
-1

Your Server probably may be running on a certain port and so you can add a health check at the client side and update a global flag with status to let client know about its availibity :-

Socket socket = null;
try
{
    socket = new Socket(host, port);
    return true;
}
catch (Exception e)
{
    return false;
}
finally
{
    if(socket != null)
        try 
        {
             socket.close();
        }
        catch(Exception e){}
}
Traian GEICU
  • 1,750
  • 3
  • 14
  • 26
Amit
  • 633
  • 6
  • 13