This is the mail archive of the java@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Bug in ServerSocket - accept()'s Socket is not bound? (3.4.3)


> When I try the following piece of code:
> > === 8< 8< 8< ===
> > import java.io.IOException;
> import java.net.ServerSocket;
> import java.net.Socket;
> > public class SocketBindTest
> {
> public static void main(String[] args) throws IOException
> {
> System.out.println("Accepting...");
> Socket s = new ServerSocket(80).accept();
> System.out.println(s.isBound());
> System.out.println(s.getLocalPort());
> System.out.println(s.getPort());
> } // main
> } // SocketBindTest
> > === 8< 8< 8< ===
> > I would expect the returned Socket instance, s, to be bound to same port > as my server-socket - namely port 80. However, GCJ's Socket reports the > socket as unbound, hence the port becomes "-1". A different behaviour is > seen with Sun's...


It's a bug:

Breakpoint 1, SocketBindTest.main(java.lang.String[]) (args=@24c40)
    at SocketBindTest.java:11

(gdb) p _Jv_DeepDebug(socket,2)
java.net.Socket@15ce60
{
  boolean implCreated = false,
  boolean outputShutdown = false,
  java.net.SocketImpl impl = gnu.java.net.PlainSocketImpl@15a3c0
    {
      int localport = 80,
      java.io.OutputStream out = null,
      java.io.FileDescriptor fd = null,
      int timeout = 0,
      java.io.InputStream in = null,
      java.net.InetAddress address = 0x15b840,
      int native_fd = 15,
      java.net.InetAddress localAddress = null,
      int port = 41693,
      boolean inChannelOperation = false,
    },
  boolean bound = false,
  boolean inputShutdown = false,
}

As you can see, ServerSocket.accept doesn't set BOUND.  I think the
right place to fix this is ServerSocket.implAccept().
Super!
I'll make the "bound" field package-protected and set the boolean true...

// Martin


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]