2 Replies Latest reply on Oct 18, 2007 1:24 AM by 843790

    SocketOutputStream throws Bad file descriptor

      I'm currently experiencing several problems with socket and file I/O reads/writes where the FileDescriptor seems to be invalid. This causes one of four exceptions to be thrown including the following: java.net.SocketException: Bad file descriptor, java.io.IOException: Bad file descriptor, java.net.SocketException: Socket operation on non-socket and java.net.SocketException : Inappropriate ioctl for device. An example exception is included below.

      java.net.SocketException: Bad file descriptor
           at java.net.SocketOutputStream.socketWrite0(Native Method)
           at java.net.SocketOutputStream.socketWrite(Unknown Source)
           at java.net.SocketOutputStream.write(Unknown Source)
           at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
           at java.io.BufferedOutputStream.flush(Unknown Source)

      I have modifed java.net.SocketOutputStream and java.io.FileDescriptor so that I can see the value of FileDescriptor's fd member variable (an int). When the SocketOutputStream is initialized, I print the value obtained from the FileDescriptor. In all failure cases, the value of FileDescriptor's fd int is 0. In all success cases, the value is greater than 0. (Note, I am not testing sockets that fail to open or files that don't exist; all tests are against sockets and files which should be successfully opened.)

      I am using the following version of Java 1.6 on linux. It does not seem to matter which version of the linux kernel I use (I've tried 2.6.5 up to 2.6.22 on Red Hat AS4, SuSE and Ubuntu). It is not clear if the fd's value (0) is coming from the kernel or from the JNI implementation.

      java version "1.6.0"
      Java(TM) SE Runtime Environment (build 1.6.0-b105)
      Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode)

      Has anyone witnessed this behavior before?