Mazzy
Mazzy

Reputation: 14179

check if a server socket is open on the other side

I would check if a server socket placed in the server is open. In the client when I create the socket Socket sock = new Socket(host,port) I check if it's open doing:

if(sock == null)
    System.out.println("The server is not connected!");
else
    //Doing some task

but i don't receive any result. there is method to check if the server socket at on the other side is open? I tried even the method isConnected() but nothing

Upvotes: 6

Views: 11175

Answers (2)

Sanjay T. Sharma
Sanjay T. Sharma

Reputation: 23208

The simplest way to check reachability and liveness is to create a blank Socket and use the connect() method with a reasonable timeout. I wrote a similar answer with a small code snippet here.

Upvotes: 11

Peter Lawrey
Peter Lawrey

Reputation: 533432

If the server is not listening you will get an Connection Refused in an exception. You will never get a null from a new

Upvotes: 8

Related Questions