2003-06-08 Michael Koch <konqueror@gmx.de>
authorMichael Koch <konqueror@gmx.de>
Sun, 8 Jun 2003 10:12:09 +0000 (10:12 +0000)
committerMichael Koch <mkoch@gcc.gnu.org>
Sun, 8 Jun 2003 10:12:09 +0000 (10:12 +0000)
* 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.

From-SVN: r67618

libjava/ChangeLog
libjava/java/net/Socket.java

index 815a1706fd1ef15d08f7470f93b8b5b725df13f2..bdfd7753ef7edb8968e30f18ab60983e141b3d31 100644 (file)
@@ -1,3 +1,12 @@
+2003-06-08  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-06-08  Michael Koch  <konqueror@gmx.de>
 
        * java/net/DatagramSocket.java
index cd3b5ecb85719d0e9e57f3901d86ca5757977c2a..8535fa9ef61152b6b6c1f35a79dad2d212f6569a 100644 (file)
@@ -281,8 +281,6 @@ public class Socket
                  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 @@ public class Socket
     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;
-          }
-      }
   }
 
   /**
@@ -362,12 +314,40 @@ public class Socket
   {
     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 class Socket
   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);
   }
 
   /**
@@ -423,8 +394,29 @@ public class Socket
 
     if (ch != null && !ch.isBlocking ())
       throw new IllegalBlockingModeException ();
-    
-    impl.connect (endpoint, timeout);
+  
+    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;
+      }
   }
 
   /**