This is the mail archive of the java-patches@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]

Patch: java.net.Socket


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi list,


the attached patch fixes the behaviour of java.net.Socket. When using 
the Socket() constructor no native socket was actually created and 
could not be used. This patch fixes this. Please review and comment.

Ok to commit to trunk ?

Should this go into 3.3 branch too as this is a real bug fix ?


Michael
- -- 
Homepage: http://www.worldforge.org/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQE+yNlNWSOgCCdjSDsRAjX+AJ9Fnaa5PSQUvRUcW852HuFvkaNQVwCbBNJy
qAne65Pt4Qck5UpW3BAkqKU=
=Xt2m
-----END PGP SIGNATURE-----
Index: ChangeLog
===================================================================
RCS file: /cvs/gcc/gcc/libjava/ChangeLog,v
retrieving revision 1.1900
diff -u -b -B -r1.1900 ChangeLog
--- ChangeLog	19 May 2003 09:14:57 -0000	1.1900
+++ ChangeLog	19 May 2003 13:11:07 -0000
@@ -1,5 +1,14 @@
 2003-05-19  Michael Koch  <konqueror@gmx.de>
 
+	* java/net/Socket.java
+	(Socket): Dont initialize inputShutdown and outputShutdown twice,
+	call bind() and connect() to actually do the bind and connect tasks.
+	(bind): Connect to canonical address if bindpoint is null, create
+	socket and bind it to bindpoint.
+	(connect): Check for exceptions.
+
+2003-05-19  Michael Koch  <konqueror@gmx.de>
+
 	* java/util/Calendar.java
 	(get): Not final anymore since JDK 1.4
 	(set): Likewise.
Index: java/net/Socket.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/net/Socket.java,v
retrieving revision 1.22
diff -u -b -B -r1.22 Socket.java
--- java/net/Socket.java	2 May 2003 09:27:59 -0000	1.22
+++ java/net/Socket.java	19 May 2003 13:11:07 -0000
@@ -281,8 +281,6 @@
                  boolean stream) throws IOException
   {
     this();
-    this.inputShutdown = false;
-    this.outputShutdown = false;
 
     if (impl == null)
       throw new IOException("Cannot initialize Socket implementation");
@@ -291,59 +289,13 @@
     if (sm != null)
       sm.checkConnect(raddr.getHostName(), rport);
 
-    // create socket
-    impl.create(stream);
+    // bind/connect socket
+    bind (new InetSocketAddress (laddr, lport));
+    connect (new InetSocketAddress (raddr, rport));
 
     // FIXME: JCL p. 1586 says if localPort is unspecified, bind to any port,
     // i.e. '0' and if localAddr is unspecified, use getLocalAddress() as
     // that default.  JDK 1.2 doc infers not to do a bind.
-    
-    // bind/connect to address/port
-    if (laddr != null)
-      {
-        try
-	  {
-            impl.bind(laddr, lport);
-          }
-	catch (IOException exception)
-          {
-            impl.close();
-            throw exception;
-          }
-        catch (RuntimeException exception)
-          {
-            impl.close();
-            throw exception;
-          }
-        catch (Error error)
-          {
-            impl.close();
-            throw error;
-          }
-      }
-
-    if (raddr != null)
-      {
-        try
-          {
-            impl.connect(raddr, rport);
-          }
-        catch (IOException exception)
-          {
-            impl.close();
-            throw exception;
-          }
-        catch (RuntimeException exception)
-          {
-            impl.close();
-            throw exception;
-          }
-        catch (Error error)
-          {
-            impl.close();
-            throw error;
-          }
-      }
   }
 
   /**
@@ -363,11 +315,39 @@
     if (closed)
       throw new SocketException ("Socket is closed");
     
+    // XXX: JDK 1.4.1 API documentation says that if bindpoint is null the
+    // socket will be bound to an ephemeral port and a valid local address.
+    if (bindpoint == null)
+      bindpoint = new InetSocketAddress (InetAddress.ANY_IF, 0);
+    
     if ( !(bindpoint instanceof InetSocketAddress))
       throw new IllegalArgumentException ();
 
     InetSocketAddress tmp = (InetSocketAddress) bindpoint;
-    impl.bind (tmp.getAddress(), tmp.getPort());
+    
+    // create socket
+    impl.create (true);
+    
+    // bind to address/port
+    try
+      {
+        impl.bind(tmp.getAddress (), tmp.getPort ());
+      }
+    catch (IOException exception)
+      {
+        impl.close ();
+        throw exception;
+      }
+    catch (RuntimeException exception)
+      {
+        impl.close ();
+        throw exception;
+      }
+    catch (Error error)
+      {
+        impl.close ();
+        throw error;
+      }
   }
   
   /**
@@ -385,16 +365,7 @@
   public void connect (SocketAddress endpoint)
     throws IOException
   {
-    if (closed)
-      throw new SocketException ("Socket is closed");
-    
-    if (! (endpoint instanceof InetSocketAddress))
-      throw new IllegalArgumentException ("Address type not supported");
-
-    if (ch != null && !ch.isBlocking ())
-      throw new IllegalBlockingModeException ();
-    
-    impl.connect (endpoint, 0);
+    connect (endpoint, 0);
   }
 
   /**
@@ -424,7 +395,28 @@
     if (ch != null && !ch.isBlocking ())
       throw new IllegalBlockingModeException ();
     
+    if (!isBound ())
+      bind (null);
+
+    try
+      {
     impl.connect (endpoint, timeout);
+      }
+    catch (IOException exception)
+      {
+        impl.close ();
+        throw exception;
+      }
+    catch (RuntimeException exception)
+      {
+        impl.close ();
+        throw exception;
+      }
+    catch (Error error)
+      {
+        impl.close ();
+        throw error;
+      }
   }
 
   /**

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