Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4169831

Setting SO_TIMEOUT on Socket does not take effect

XMLWordPrintable

    • 1.2fcs
    • sparc
    • solaris_2.5
    • Verified



      Name: yyC67448 Date: 08/28/98




      read() call on InputStream associated with Socket infinitly blocks
      ever if the SO_TIMEOUT value is set to positive ( > 0 ) value via
      Socket.setSoTimeout(int tout) call.


      Here is the javadoc comment for Socket.setSoTimeout() method:

          /**
           * Enable/disable SO_TIMEOUT with the specified timeout, in
           * milliseconds. With this option set to a non-zero timeout,
           * a read() call on the InputStream associated with this Socket
           * will block for only this amount of time. If the timeout expires,
           * a <B>java.io.InterruptedIOException</B> is raised, though the
           * Socket is still valid. The option <B>must</B> be enabled
           * prior to entering the blocking operation to have effect. The
           * timeout must be > 0.
           * A timeout of zero is interpreted as an infinite timeout.
           *
           * @since JDK 1.1
           */


      Here is the test demonstrating the bug:

      ---------------------------- test.java --------------------------------
      import java.net.*;
      import java.io.*;


      class test
      {

        public static void main(String args[])
        {
          InetAddress sin = null;
          Socket soc = null,soc1 = null;
          InputStream is = null;
          OutputStream os = null;
          ServerSocket srv = null;
          int port = 0;
          int tout = 1000;


          /*
           * Try to get localhost address
           */
          try {
            sin = InetAddress.getLocalHost();
          } catch(Exception e)
          {
            System.out.println("Can not get localhost Address:" + e);
            System.exit(-1);
          }


          try {
            srv = new ServerSocket(port);
            port = srv.getLocalPort();
          } catch(Exception e)
          {
            System.out.println("Unexpected Exception thrown " + e );
            System.exit(-1);
          }

          try {
            soc = new Socket(sin, port, true);
            soc1 = srv.accept();
          } catch(Exception e)
          {
            System.out.println("Can not create socket:" + e);
            System.exit(-1);
          }

          System.out.println("Connection accepted!");


          /*
           * Try to set SO_TIMEOUT value
           */
          try {
            soc.setSoTimeout(tout);
            System.out.println("value = " + soc.getSoTimeout());
          }
          catch(Exception e)
          {
            try {
      soc.close();
      soc1.close();
      srv.close();
            } catch(Exception e1)
            {
      System.out.println("Can not close socket :" + e1);
      System.exit(-1);
            }
            System.out.println("Unexpected exception:" + e);
          }

          try {
            is = soc.getInputStream();
            os = soc1.getOutputStream();
            System.out.println("Got Input/Ouput Streams");
            is.read();
          } catch(InterruptedIOException e)
          {
      System.out.println("OKAY");
              System.exit(0);
          }
          catch(Exception e)
          {
      System.out.println("Unexpected exception:" + e);
      System.exit(0);
          }
          
          System.out.println("No exceptions !");

          try {
            soc.close();
            soc1.close();
            srv.close();
          } catch(Exception e)
          {
            System.out.println("Can not close socket:" + e);
            System.exit(-1);
          }
          
        }
      }

      ---------------------- Output from the test on JDK-1.2beta4-K -------------
      Connection accepted!
      value = 1000
      Got Input/Ouput Streams
      OKAY
      ------------------------------------------------------------------------------


      ---------------------- Output from the test on JDK-1.2fcs-C ----------------
      Connection accepted!
      value = 1000
      Got Input/Ouput Streams
      OKAY
      ------------------------------------------------------------------------------

      ---------------------- Output from the test on JDK-1.2fcs-G ----------------
      Connection accepted!
      value = 1000
      Got Input/Ouput Streams
      ------------------------------------------------------------------------------



      ======================================================================

            mmcclosksunw Michael Mccloskey (Inactive)
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: