How to check the server socket is alive or not?

Hi,

I've an scenario where in one side i've server socket program which listen's client request. another side i've client socket program which connects to that server socket program thro ip address and port number.

With this i'm sending messages from client socket to server socket program. In server socket program, i'm receiving messages and printing. Eventhough when i stop the server program, the client program keeps sending messges....

check my programs....

Server program:

public class Server {

ServerSocket server;

public Server() {

try {

this.server = new ServerSocket(6123);

} catch (IOException e) {

System.out.println("############## Exception: Couldn't create Socket object ##############");

e.printStackTrace();

}

}

public void listen() {

try {

while (true) {

System.out.println("((( - Listening - )))");

Socket sock = server.accept();

InetAddress addr = sock.getInetAddress();

System.out.println("Connection made to " + addr.getHostName()

+ " (" + addr.getHostAddress() + ")");

BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));

String input;

while ((input = in.readLine()) != null) {

System.out.println("Received messaage = " + input);

}

}

} catch (IOException e) {

System.out.println("Exception detected SERVER: " + e);

}

}

public static void main(String[] args) {

Server serverInstance = new Server();

serverInstance.listen();

}

}

Client program:

public class Client {

Socket echoSocket = null;

PrintWriter out = null;

BufferedReader in = null;

public static void main(String[] args) throws IOException, InterruptedException {

Client client = new Client();

client.sendMessageToServer();

}

public void sendMessageToServer() throws IOException, InterruptedException {

InetSocketAddress soc=null;

try {

try {

echoSocket = new Socket("localhost", 6123);

soc = new InetSocketAddress(InetAddress.getByName("localhost"), 6123);

out = new PrintWriter(echoSocket.getOutputStream(), true);

in = new BufferedReader(new InputStreamReader(

echoSocket.getInputStream()));

} catch (UnknownHostException e) {

System.err.println("Don't know about host: taranis."+e);

System.exit(1);

} catch (IOException e) {

System.err.println("Couldn't get I/O for the connection to: taranis.");

System.exit(1);

}

boolean flag = false;

int i = 0;

while (true) {

out.println("<<<<<<<<<<< Message: " + new Date(System.currentTimeMillis()));

System.out.println("<<<<<<<<<<< Message sent at: " + new Date(System.currentTimeMillis()));

out = new PrintWriter(echoSocket.getOutputStream(), true);

in = new BufferedReader(new InputStreamReader(

echoSocket.getInputStream()));

out.println("<<<<<<<<<<< Message: " + new Date(System.currentTimeMillis()));

}

} catch (Exception e) {

System.out.println("############## Exception: Couldn't create Client Socket object ##############");

e.printStackTrace();

}

}

How to find the server socket is alive or not before sending a message?

[3475 byte] By [Senthilrajaa] at [2007-11-27 3:03:49]
# 1
Trying to actually send something to the server is the reliable way of checking if it's alive or not.
karma-9a at 2007-7-12 3:47:51 > top of Java-index,Java Essentials,Java Programming...
# 2
no i dont understand
Senthilrajaa at 2007-7-12 3:47:51 > top of Java-index,Java Essentials,Java Programming...
# 3
> no i dont understandthe only way you can know if the socket is alive is to try and use it. connect to it and send some data down it. if that works, the socket is alive, otherwise, it isn't
georgemca at 2007-7-12 3:47:51 > top of Java-index,Java Essentials,Java Programming...
# 4

see in the client program, i've an infinite while loop. i'm keep sending messages to the server infinitely. eventhough the server program stops in between. but i'm not getting any errors or anything from client side. I dont know the status of the server. but still keep on sending...

how to get the status of the server program?

Senthilrajaa at 2007-7-12 3:47:51 > top of Java-index,Java Essentials,Java Programming...
# 5

> see in the client program, i've an infinite while

> loop. i'm keep sending messages to the server

> infinitely. eventhough the server program stops in

> between. but i'm not getting any errors or anything

> from client side.

Not true, you'll get an IOException, that you will handle in a catch block to break out of the loop.

I haven't looked at your code (please use the code tags) but in practice, using an alive flag (set to true) in the loop i.e. while (alive && ...) .. and setting the flag to false in the catch block should do the trick.

karma-9a at 2007-7-12 3:47:51 > top of Java-index,Java Essentials,Java Programming...