diff options
author | Michael Koch <konqueror@gmx.de> | 2003-06-08 10:12:09 +0000 |
---|---|---|
committer | Michael Koch <mkoch@gcc.gnu.org> | 2003-06-08 10:12:09 +0000 |
commit | 742ed2f3daf7e24066154963409f867acf10e8a9 (patch) | |
tree | 076b1d41bea49b88e8f54b25b466cf2cef10072a /libjava/java/net | |
parent | f61e2125825d5e69ddb10d2232af4ca0e94ecf26 (diff) | |
download | gcc-742ed2f3daf7e24066154963409f867acf10e8a9.zip gcc-742ed2f3daf7e24066154963409f867acf10e8a9.tar.gz gcc-742ed2f3daf7e24066154963409f867acf10e8a9.tar.bz2 |
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.
From-SVN: r67618
Diffstat (limited to 'libjava/java/net')
-rw-r--r-- | libjava/java/net/Socket.java | 120 |
1 files changed, 56 insertions, 64 deletions
diff --git a/libjava/java/net/Socket.java b/libjava/java/net/Socket.java index cd3b5ec..8535fa9 100644 --- a/libjava/java/net/Socket.java +++ b/libjava/java/net/Socket.java @@ -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; + } } /** |