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

Martin Egholm Nielsen martin@egholm-nielsen.dk
Thu Jul 21 09:55:00 GMT 2005


Hi there,

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...

Here's the output from both:

# ./ssb
Accepting...
false
-1
2234
#

$ java -cp . SocketBindTest
Accepting...
true
80
2245

Maybe this is not a but, or maybe it is, and has been fixed in a newer 
version than my ancient 3.4.3?

BR,
  Martin Egholm



More information about the Java mailing list