5 Replies Latest reply: May 2, 2007 7:11 AM by 807606 RSS

    How to check the server socket is alive or not?

    807606
      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?