Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 270
  • Last Modified:

closing a socket

I have a very basic question: how can I test a Socket to determine whether or not it is open?

I know I receive an exception if the socket is unexpectedly closed.  I also know I receive a known return code (e.g. null) if I read or write to a closed socket.  But how do I ask a Socket whether or not it is open without having to read or write?

Thanks!

Joe 
0
BrindleFly
Asked:
BrindleFly
  • 2
1 Solution
 
evijayCommented:
I didnt find any way to do this (as per my knowledge). The best way to handle this is

After you say
InputStream is = socket.getInputStream();
create a push back input stream
PushbackInputStream ps = new PushBackInputStream (ps);

use ps instead of is to read data from socket.

Write an utility routine to test for socket closure as below

public static boolean isSocketClosed(PushbackInputStream pInpStr) throws Exception
{
        int rd;
        try { rd  = pInpStr.read(); } catch (Exception e) { System.out.println("Socket closed !!"); return true; }
        System.out.println("not yet closed!!");
        pInpStr.unread(rd);
        return false;
}

0
 
Charmaine041198Commented:
Do you mean to test if a socket is still valid?

eg.
// start

Socket mysocket = new Socket(hostname,portnum);

// Test if socket is valid
if(mysocket = null) {
    // socket closed, do something
} else {
    // socket open, read or write using socket
}

// end
0
 
evijayCommented:
What i said was, once you open a socket, anyway, you will get the socket input and output streams.
Instead of using Socket input stream, i want you to pass it thru pushback input stream and then use it.
Here is an example of the code which can answer your doubts.

//server code file - tserv.java
import java.io.*;
import java.net.*;
public class tserv {

      public static void main(String args[]) throws Exception
      {
            byte b[] = new byte[1000];
            ServerSocket s = new ServerSocket(13500);
            Socket ss = s.accept();
            OutputStream os = (OutputStream) ss.getOutputStream();
            for (int i = 0; i < 100; i++) {
                  os.write(b);
                  System.out.println("written ");
                  Thread.currentThread().sleep(5000);
                  // suppose some serious error occured
                  System.exit(1);
            }
      }
}


// client code file - tcli.java

import java.io.*;
import java.net.*;
public class tcli {
public static boolean isSocketClosed(PushbackInputStream pInpStr) throws Exception
{
      int rd;
      try { rd  = pInpStr.read(); } catch (Exception e) { System.out.println("Socket closed !!"); return true; }
      System.out.println("not yet closed!!");
      pInpStr.unread(rd);
      return false;
}
public static void main(String [] args) throws Exception
{
      Socket s  = new Socket("napa.mcom.com", 13500);
      PushbackInputStream pInpStr;
      pInpStr = new PushbackInputStream(s.getInputStream());
      byte b[] = new byte[1000];
      while (true) {
        
            Thread.currentThread().sleep(100);
            if (isSocketClosed(pInpStr) == false)
                  pInpStr.read(b);
            else
                  break;
            System.out.println("Read");
            Thread.currentThread().sleep(3000);
      }
      
}
}


0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now