{
this();
- if (impl == null)
- throw new IOException("Cannot initialize Socket implementation");
-
- // create socket
- impl.create(true);
-
// bind/listen socket
bind (new InetSocketAddress (bindAddr, port), backlog);
}
if (closed)
throw new SocketException ("ServerSocket is closed");
- if (impl == null)
- throw new IOException ("Cannot initialize Socket implementation");
-
if (! (endpoint instanceof InetSocketAddress))
throw new IllegalArgumentException ("Address type not supported");
if (s != null)
s.checkListen (tmp.getPort ());
- // bind to address/port
try
{
- impl.bind (tmp.getAddress (), tmp.getPort ());
+ impl.bind (tmp.getAddress (), tmp.getPort ());
+ impl.listen(backlog);
}
catch (IOException exception)
{
- impl.close();
+ close();
throw exception;
}
catch (RuntimeException exception)
{
- impl.close();
+ close();
throw exception;
}
catch (Error error)
{
- impl.close();
- throw error;
- }
-
- // listen on socket
- try
- {
- impl.listen(backlog);
- }
- catch (IOException exception)
- {
- impl.close();
- throw exception;
- }
- catch (RuntimeException exception)
- {
- impl.close();
- throw exception;
- }
- catch (Error error)
- {
- impl.close();
+ close();
throw error;
}
}
*/
public Socket accept () throws IOException
{
- if (impl == null)
- throw new IOException ("Cannot initialize Socket implementation");
-
SecurityManager sm = System.getSecurityManager ();
if (sm != null)
sm.checkListen (impl.getLocalPort ());
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));
}
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))
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");
public int getReceiveBufferSize ()
throws SocketException
{
- if (impl == null)
- throw new SocketException ("Not connected");
-
Object buf = impl.getOption (SocketOptions.SO_RCVBUF);
if (!(buf instanceof Integer))
*/
SocketImpl impl;
- private boolean inputShutdown;
- private boolean outputShutdown;
+ private boolean inputShutdown = false;
+ private boolean outputShutdown = false;
SocketChannel ch; // this field must have been set if created by SocketChannel
impl = factory.createSocketImpl();
else
impl = new PlainSocketImpl();
-
- inputShutdown = false;
- outputShutdown = false;
}
/**
* <p>
* Additionally, this socket will be created using the supplied
* implementation class instead the default class or one returned by a
- * factory. This value can be <code>null</code>, but if it is, all instance
- * methods in <code>Socket</code> should be overridden because most of them
- * rely on this value being populated.
+ * factory. If this value is <code>null</code>, the default Socket
+ * implementation is used.
*
* @param impl The <code>SocketImpl</code> to use for this
* <code>Socket</code>
*/
protected Socket (SocketImpl impl) throws SocketException
{
- this.impl = impl;
- this.inputShutdown = false;
- this.outputShutdown = false;
+ if (impl == null)
+ this.impl = new PlainSocketImpl();
+ else
+ this.impl = impl;
}
/**
{
this();
- if (raddr == null)
- throw new NullPointerException ();
-
- if (impl == null)
- throw new IOException("Cannot initialize Socket implementation");
-
SecurityManager sm = System.getSecurityManager();
if (sm != null)
sm.checkConnect(raddr.getHostName(), rport);
}
catch (IOException exception)
{
- impl.close ();
+ close ();
throw exception;
}
catch (RuntimeException exception)
{
- impl.close ();
+ close ();
throw exception;
}
catch (Error error)
{
- impl.close ();
+ close ();
throw error;
}
}
}
catch (IOException exception)
{
- impl.close ();
+ close ();
throw exception;
}
catch (RuntimeException exception)
{
- impl.close ();
+ close ();
throw exception;
}
catch (Error error)
{
- impl.close ();
+ close ();
throw error;
}
}
*/
public InetAddress getInetAddress ()
{
- if (impl != null)
- return impl.getInetAddress();
-
- return null;
+ return impl.getInetAddress();
}
/**
*/
public InetAddress getLocalAddress ()
{
- if (impl == null)
- return null;
-
InetAddress addr = null;
try
{
*/
public void setTcpNoDelay (boolean on) throws SocketException
{
- if (impl == null)
- throw new SocketException("Not connected");
-
impl.setOption(SocketOptions.TCP_NODELAY, new Boolean(on));
}
*/
public boolean getTcpNoDelay() throws SocketException
{
- if (impl == null)
- throw new SocketException("Not connected");
-
Object on = impl.getOption(SocketOptions.TCP_NODELAY);
if (on instanceof Boolean)
*/
public void setSoLinger(boolean on, int linger) throws SocketException
{
- if (impl == null)
- throw new SocketException("No socket created");
-
if (on == true)
{
if (linger < 0)
*/
public int getSoLinger() throws SocketException
{
- if (impl == null)
- throw new SocketException("Not connected");
-
Object linger = impl.getOption(SocketOptions.SO_LINGER);
if (linger instanceof Integer)
return(((Integer)linger).intValue());
*/
public void setOOBInline (boolean on) throws SocketException
{
- if (impl == null)
- throw new SocketException("Not connected");
-
impl.setOption(SocketOptions.SO_OOBINLINE, new Boolean(on));
}
*/
public boolean getOOBInline () throws SocketException
{
- if (impl == null)
- throw new SocketException("Not connected");
-
Object buf = impl.getOption(SocketOptions.SO_OOBINLINE);
if (buf instanceof Boolean)
*/
public synchronized void setSoTimeout (int timeout) throws SocketException
{
- if (impl == null)
- throw new SocketException("Not connected");
-
if (timeout < 0)
throw new IllegalArgumentException("SO_TIMEOUT value must be >= 0");
*/
public synchronized int getSoTimeout () throws SocketException
{
- if (impl == null)
- throw new SocketException("Not connected");
-
Object timeout = impl.getOption(SocketOptions.SO_TIMEOUT);
if (timeout instanceof Integer)
return(((Integer)timeout).intValue());
*/
public void setSendBufferSize (int size) throws SocketException
{
- if (impl == null)
- throw new SocketException("Not connected");
-
if (size <= 0)
throw new IllegalArgumentException("SO_SNDBUF value must be > 0");
*/
public int getSendBufferSize () throws SocketException
{
- if (impl == null)
- throw new SocketException("Not connected");
-
Object buf = impl.getOption(SocketOptions.SO_SNDBUF);
if (buf instanceof Integer)
*/
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");
*/
public int getReceiveBufferSize () throws SocketException
{
- if (impl == null)
- throw new SocketException("Not connected");
-
Object buf = impl.getOption(SocketOptions.SO_RCVBUF);
if (buf instanceof Integer)
*/
public void setKeepAlive (boolean on) throws SocketException
{
- if (impl == null)
- throw new SocketException("Not connected");
-
impl.setOption(SocketOptions.SO_KEEPALIVE, new Boolean(on));
}
*/
public boolean getKeepAlive () throws SocketException
{
- if (impl == null)
- throw new SocketException("Not connected");
-
Object buf = impl.getOption(SocketOptions.SO_KEEPALIVE);
if (buf instanceof Boolean)
*/
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))
*/
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));
}
*/
public int getTrafficClass () throws SocketException
{
- if (impl == null)
- throw new SocketException ("Cannot initialize Socket implementation");
-
Object obj = impl.getOption(SocketOptions.IP_TOS);
if (obj instanceof Integer)
*/
public void setTrafficClass (int tc) throws SocketException
{
- if (impl == null)
- throw new SocketException ("Cannot initialize Socket implementation");
-
if (tc < 0 || tc > 255)
throw new IllegalArgumentException();