2002-09-11 Michael Koch <konqueror@gmx.de>
* java/net/Socket.java (Socket): protected to public (since JDK 1.4). Added @specnote. (bind): New method. (connect): Two new methods. (getKeepalive): Get correct socket option. (setKeepalive): Set correct socket option. (getOOBInline): New method. (setOOBInline): New method. * java/net/ServerSocket.java (bind): Two new methods. (getInetAddress): Reimplemented, catch exception. (getLocalSocketAddress): New method. (setReuseAddress): New method. (getReuseAdress): New method. (setReceiveBufferSize): New method. (getReceiveBufferSize): New method. (toString): Made string JDK 1.4 compliant. From-SVN: r57032
This commit is contained in:
parent
35bb45c65b
commit
b7caf8dd94
3 changed files with 268 additions and 5 deletions
|
@ -1,3 +1,23 @@
|
||||||
|
2002-09-11 Michael Koch <konqueror@gmx.de>
|
||||||
|
|
||||||
|
* java/net/Socket.java
|
||||||
|
(Socket): protected to public (since JDK 1.4). Added @specnote.
|
||||||
|
(bind): New method.
|
||||||
|
(connect): Two new methods.
|
||||||
|
(getKeepalive): Get correct socket option.
|
||||||
|
(setKeepalive): Set correct socket option.
|
||||||
|
(getOOBInline): New method.
|
||||||
|
(setOOBInline): New method.
|
||||||
|
* java/net/ServerSocket.java
|
||||||
|
(bind): Two new methods.
|
||||||
|
(getInetAddress): Reimplemented, catch exception.
|
||||||
|
(getLocalSocketAddress): New method.
|
||||||
|
(setReuseAddress): New method.
|
||||||
|
(getReuseAdress): New method.
|
||||||
|
(setReceiveBufferSize): New method.
|
||||||
|
(getReceiveBufferSize): New method.
|
||||||
|
(toString): Made string JDK 1.4 compliant.
|
||||||
|
|
||||||
2002-09-10 Michael Koch <konqueror@gmx.de>
|
2002-09-10 Michael Koch <konqueror@gmx.de>
|
||||||
|
|
||||||
* java/net/SocketImpl.java
|
* java/net/SocketImpl.java
|
||||||
|
|
|
@ -151,6 +151,53 @@ public class ServerSocket
|
||||||
impl.listen(backlog);
|
impl.listen(backlog);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Binds the server socket to a specified socket address
|
||||||
|
*
|
||||||
|
* @param endpoint The socket address to bind to
|
||||||
|
*
|
||||||
|
* @exception IOException If an error occurs
|
||||||
|
*
|
||||||
|
* @since 1.4
|
||||||
|
*/
|
||||||
|
public void bind (SocketAddress endpoint)
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
if (impl == null)
|
||||||
|
throw new IOException ("Cannot initialize Socket implementation");
|
||||||
|
|
||||||
|
InetSocketAddress tmp = (InetSocketAddress) endpoint;
|
||||||
|
|
||||||
|
SecurityManager s = System.getSecurityManager ();
|
||||||
|
if (s != null)
|
||||||
|
s.checkListen (tmp.getPort ());
|
||||||
|
|
||||||
|
impl.bind (tmp.getAddress (), tmp.getPort ());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Binds the server socket to a specified socket address
|
||||||
|
*
|
||||||
|
* @param endpoint The socket address to bind to
|
||||||
|
* @param backlog The length of the pending connection queue
|
||||||
|
* @exception IOException If an error occurs
|
||||||
|
*/
|
||||||
|
public void bind (SocketAddress endpoint, int backlog)
|
||||||
|
throws java.io.IOException
|
||||||
|
{
|
||||||
|
if (impl == null)
|
||||||
|
throw new IOException ("Cannot initialize Socket implementation");
|
||||||
|
|
||||||
|
InetSocketAddress tmp = (InetSocketAddress) endpoint;
|
||||||
|
|
||||||
|
SecurityManager s = System.getSecurityManager ();
|
||||||
|
if (s != null)
|
||||||
|
s.checkListen (tmp.getPort ());
|
||||||
|
|
||||||
|
impl.bind (tmp.getAddress (), tmp.getPort ());
|
||||||
|
impl.listen(backlog);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method returns the local address to which this socket is bound
|
* This method returns the local address to which this socket is bound
|
||||||
*
|
*
|
||||||
|
@ -158,7 +205,14 @@ public class ServerSocket
|
||||||
*/
|
*/
|
||||||
public InetAddress getInetAddress()
|
public InetAddress getInetAddress()
|
||||||
{
|
{
|
||||||
return impl.getInetAddress();
|
try
|
||||||
|
{
|
||||||
|
return (InetAddress) impl.getOption (SocketOptions.SO_BINDADDR);
|
||||||
|
}
|
||||||
|
catch (SocketException e)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -171,6 +225,21 @@ public class ServerSocket
|
||||||
return impl.getLocalPort();
|
return impl.getLocalPort();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the local socket address
|
||||||
|
*
|
||||||
|
* @since 1.4
|
||||||
|
*/
|
||||||
|
public SocketAddress getLocalSocketAddress()
|
||||||
|
{
|
||||||
|
InetAddress addr = getInetAddress();
|
||||||
|
|
||||||
|
if (addr != null)
|
||||||
|
return new InetSocketAddress (getInetAddress(), getLocalPort());
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Accepts a new connection and returns a connected <code>Socket</code>
|
* Accepts a new connection and returns a connected <code>Socket</code>
|
||||||
* instance representing that connection. This method will block until a
|
* instance representing that connection. This method will block until a
|
||||||
|
@ -254,6 +323,91 @@ public class ServerSocket
|
||||||
return ((Integer)timeout).intValue();
|
return ((Integer)timeout).intValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enables/Disables the SO_REUSEADDR option
|
||||||
|
*
|
||||||
|
* @exception SocketException If an error occurs
|
||||||
|
*
|
||||||
|
* @since 1.4
|
||||||
|
*/
|
||||||
|
public void setReuseAddress (boolean on)
|
||||||
|
throws SocketException
|
||||||
|
{
|
||||||
|
if (impl == null)
|
||||||
|
throw new SocketException ("Cannot initialize Socket implementation");
|
||||||
|
|
||||||
|
impl.setOption (SocketOptions.SO_REUSEADDR, new Boolean (on));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if the SO_REUSEADDR option is enabled
|
||||||
|
*
|
||||||
|
* @exception SocketException If an error occurs
|
||||||
|
*
|
||||||
|
* @since 1.4
|
||||||
|
*/
|
||||||
|
public boolean getReuseAddress()
|
||||||
|
throws SocketException
|
||||||
|
{
|
||||||
|
if (impl == null)
|
||||||
|
throw new SocketException ("Cannot initialize Socket implementation");
|
||||||
|
|
||||||
|
Object reuseaddr = impl.getOption (SocketOptions.SO_REUSEADDR);
|
||||||
|
|
||||||
|
if (!(reuseaddr instanceof Boolean))
|
||||||
|
throw new SocketException ("Internal Error");
|
||||||
|
|
||||||
|
return ((Boolean) reuseaddr).booleanValue ();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method sets the value for the system level socket option
|
||||||
|
* SO_RCVBUF to the specified value. Note that valid values for this
|
||||||
|
* option are specific to a given operating system.
|
||||||
|
*
|
||||||
|
* @param size The new receive buffer size.
|
||||||
|
*
|
||||||
|
* @exception SocketException If an error occurs or Socket is not connected
|
||||||
|
*
|
||||||
|
* @since 1.4
|
||||||
|
*/
|
||||||
|
public void setReceiveBufferSize (int size)
|
||||||
|
throws SocketException
|
||||||
|
{
|
||||||
|
if (impl == null)
|
||||||
|
throw new SocketException ("Not connected");
|
||||||
|
|
||||||
|
if (size <= 0)
|
||||||
|
throw new IllegalArgumentException ("SO_RCVBUF value must be > 0");
|
||||||
|
|
||||||
|
impl.setOption (SocketOptions.SO_RCVBUF, new Integer (size));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method returns the value of the system level socket option
|
||||||
|
* SO_RCVBUF, which is used by the operating system to tune buffer
|
||||||
|
* sizes for data transfers.
|
||||||
|
*
|
||||||
|
* @return The receive buffer size.
|
||||||
|
*
|
||||||
|
* @exception SocketException If an error occurs or Socket is not connected
|
||||||
|
*
|
||||||
|
* @since 1.4
|
||||||
|
*/
|
||||||
|
public int getReceiveBufferSize ()
|
||||||
|
throws SocketException
|
||||||
|
{
|
||||||
|
if (impl == null)
|
||||||
|
throw new SocketException ("Not connected");
|
||||||
|
|
||||||
|
Object buf = impl.getOption (SocketOptions.SO_RCVBUF);
|
||||||
|
|
||||||
|
if (!(buf instanceof Integer))
|
||||||
|
throw new SocketException ("Internal Error: Unexpected type");
|
||||||
|
|
||||||
|
return ((Integer) buf).intValue ();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the value of this socket as a <code>String</code>.
|
* Returns the value of this socket as a <code>String</code>.
|
||||||
*
|
*
|
||||||
|
@ -261,7 +415,7 @@ public class ServerSocket
|
||||||
*/
|
*/
|
||||||
public String toString ()
|
public String toString ()
|
||||||
{
|
{
|
||||||
return "ServerSocket " + impl.toString();
|
return "ServerSocket" + impl.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Class methods
|
// Class methods
|
||||||
|
|
|
@ -84,8 +84,10 @@ public class Socket
|
||||||
* Initializes a new instance of <code>Socket</code> object without
|
* Initializes a new instance of <code>Socket</code> object without
|
||||||
* connecting to a remote host. This useful for subclasses of socket that
|
* connecting to a remote host. This useful for subclasses of socket that
|
||||||
* might want this behavior.
|
* might want this behavior.
|
||||||
|
*
|
||||||
|
* @specnote This constructor is public since JDK 1.4
|
||||||
*/
|
*/
|
||||||
protected Socket ()
|
public Socket ()
|
||||||
{
|
{
|
||||||
if (factory != null)
|
if (factory != null)
|
||||||
impl = factory.createSocketImpl();
|
impl = factory.createSocketImpl();
|
||||||
|
@ -265,6 +267,56 @@ public class Socket
|
||||||
impl.connect(raddr, rport);
|
impl.connect(raddr, rport);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Binds the socket to the givent local address/port
|
||||||
|
*
|
||||||
|
* @param bindpoint The address/port to bind to
|
||||||
|
*
|
||||||
|
* @exception If an error occurs
|
||||||
|
*
|
||||||
|
* @since 1.4
|
||||||
|
*/
|
||||||
|
public void bind (SocketAddress bindpoint) throws IOException
|
||||||
|
{
|
||||||
|
if ( !(bindpoint instanceof InetSocketAddress))
|
||||||
|
throw new IllegalArgumentException ();
|
||||||
|
|
||||||
|
InetSocketAddress tmp = (InetSocketAddress) bindpoint;
|
||||||
|
impl.bind (tmp.getAddress(), tmp.getPort());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Connects the socket with a remote address.
|
||||||
|
*
|
||||||
|
* @param endpoint The address to connect to
|
||||||
|
*
|
||||||
|
* @exception IOException If an error occurs
|
||||||
|
*
|
||||||
|
* @since 1.4
|
||||||
|
*/
|
||||||
|
public void connect (SocketAddress endpoint)
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
impl.connect (endpoint, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Connects the socket with a remote address. A timeout of zero is
|
||||||
|
* interpreted as an infinite timeout. The connection will then block
|
||||||
|
* until established or an error occurs.
|
||||||
|
*
|
||||||
|
* @param endpoint The address to connect to
|
||||||
|
*
|
||||||
|
* @exception IOException If an error occurs
|
||||||
|
*
|
||||||
|
* @since 1.4
|
||||||
|
*/
|
||||||
|
public void connect (SocketAddress endpoint, int timeout)
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
impl.connect (endpoint, timeout);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the address of the remote end of the socket. If this socket
|
* Returns the address of the remote end of the socket. If this socket
|
||||||
* is not connected, then <code>null</code> is returned.
|
* is not connected, then <code>null</code> is returned.
|
||||||
|
@ -472,6 +524,43 @@ public class Socket
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enables/disables the SO_OOBINLINE option
|
||||||
|
*
|
||||||
|
* @param on True if SO_OOBLINE should be enabled
|
||||||
|
*
|
||||||
|
* @exception SocketException If an error occurs
|
||||||
|
*
|
||||||
|
* @since 1.4
|
||||||
|
*/
|
||||||
|
public void setOOBInline (boolean on) throws SocketException
|
||||||
|
{
|
||||||
|
if (impl == null)
|
||||||
|
throw new SocketException("Not connected");
|
||||||
|
|
||||||
|
impl.setOption(SocketOptions.SO_OOBINLINE, new Boolean(on));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the current setting of the SO_OOBINLINE option for this socket
|
||||||
|
*
|
||||||
|
* @exception SocketException If an error occurs
|
||||||
|
*
|
||||||
|
* @since 1.4
|
||||||
|
*/
|
||||||
|
public boolean getOOBInline () throws SocketException
|
||||||
|
{
|
||||||
|
if (impl == null)
|
||||||
|
throw new SocketException("Not connected");
|
||||||
|
|
||||||
|
Object buf = impl.getOption(SocketOptions.SO_OOBINLINE);
|
||||||
|
|
||||||
|
if (buf instanceof Boolean)
|
||||||
|
return(((Boolean)buf).booleanValue());
|
||||||
|
else
|
||||||
|
throw new SocketException("Internal Error: Unexpected type");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the value of the SO_TIMEOUT option on the socket. If this value
|
* Sets the value of the SO_TIMEOUT option on the socket. If this value
|
||||||
* is set, and an read/write is performed that does not complete within
|
* is set, and an read/write is performed that does not complete within
|
||||||
|
@ -632,7 +721,7 @@ public class Socket
|
||||||
if (impl == null)
|
if (impl == null)
|
||||||
throw new SocketException("Not connected");
|
throw new SocketException("Not connected");
|
||||||
|
|
||||||
impl.setOption(SocketOptions.SO_RCVBUF, new Boolean(on));
|
impl.setOption(SocketOptions.SO_KEEPALIVE, new Boolean(on));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -650,7 +739,7 @@ public class Socket
|
||||||
if (impl == null)
|
if (impl == null)
|
||||||
throw new SocketException("Not connected");
|
throw new SocketException("Not connected");
|
||||||
|
|
||||||
Object buf = impl.getOption(SocketOptions.SO_RCVBUF);
|
Object buf = impl.getOption(SocketOptions.SO_KEEPALIVE);
|
||||||
|
|
||||||
if (buf instanceof Boolean)
|
if (buf instanceof Boolean)
|
||||||
return(((Boolean)buf).booleanValue());
|
return(((Boolean)buf).booleanValue());
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue