java.net
Class PlainSocketImpl

java.lang.Object
  |
  +--java.net.SocketImpl
        |
        +--java.net.PlainSocketImpl
All Implemented Interfaces:
SocketOptions
Direct Known Subclasses:
SocksSocketImpl

class PlainSocketImpl
extends SocketImpl

Default Socket Implementation. This implementation does not implement any security checks. Note this class should NOT be public.

Version:
1.58, 04/23/02
Author:
Steven B. Byrne

Field Summary
private  boolean closePending
           
private  int CONNECTION_NOT_RESET
           
private  int CONNECTION_RESET
           
private  int CONNECTION_RESET_PENDING
           
private  Object fdLock
           
private  int fdUseCount
           
private  Object resetLock
           
private  int resetState
           
private  boolean shut_rd
           
static int SHUT_RD
           
private  boolean shut_wr
           
static int SHUT_WR
           
private  SocketInputStream socketInputStream
           
(package private)  int timeout
           
private  int trafficClass
           
 
Fields inherited from class java.net.SocketImpl
address, fd, localport, port, serverSocket, socket
 
Fields inherited from interface java.net.SocketOptions
IP_MULTICAST_IF, IP_MULTICAST_IF2, IP_MULTICAST_LOOP, IP_TOS, SO_BINDADDR, SO_BROADCAST, SO_KEEPALIVE, SO_LINGER, SO_OOBINLINE, SO_RCVBUF, SO_REUSEADDR, SO_SNDBUF, SO_TIMEOUT, TCP_NODELAY
 
Constructor Summary
(package private) PlainSocketImpl()
          Constructs an empty instance.
(package private) PlainSocketImpl(FileDescriptor fd)
          Constructs an instance with the given file descriptor.
 
Method Summary
protected  void accept(SocketImpl s)
          Accepts connections.
 FileDescriptor acquireFD()
           
protected  int available()
          Returns the number of bytes that can be read without blocking.
protected  void bind(InetAddress address, int lport)
          Binds the socket to the specified address of the specified local port.
protected  void close()
          Closes the socket.
protected  void connect(InetAddress address, int port)
          Creates a socket and connects it to the specified address on the specified port.
protected  void connect(SocketAddress address, int timeout)
          Creates a socket and connects it to the specified address on the specified port.
protected  void connect(String host, int port)
          Creates a socket and connects it to the specified port on the specified host.
private  void connectToAddress(InetAddress address, int port, int timeout)
           
protected  void create(boolean stream)
          Creates a socket with a boolean that specifies whether this is a stream socket (true) or an unconnected UDP socket (false).
private  void doConnect(InetAddress address, int port, int timeout)
          The workhorse of the connection operation.
protected  void finalize()
          Cleans up if the user forgets to close it.
protected  InputStream getInputStream()
          Gets an InputStream for this socket.
 Object getOption(int opt)
          Enabled: Fetch the value of an option.
protected  OutputStream getOutputStream()
          Gets an OutputStream for this socket.
 int getTimeout()
           
private static void initProto()
           
 boolean isClosedOrPending()
           
 boolean isConnectionReset()
           
 boolean isConnectionResetPending()
           
protected  void listen(int count)
          Listens, for a specified amount of time, for connections.
 void releaseFD()
           
protected  void sendUrgentData(int data)
          Send one byte of urgent data on the socket.
 void setConnectionReset()
           
 void setConnectionResetPending()
           
(package private)  void setInputStream(SocketInputStream in)
           
 void setOption(int opt, Object val)
          Enabled: Enable/disable the option specified by optID.
protected  void shutdownInput()
          Shutdown read-half of the socket connection;
protected  void shutdownOutput()
          Shutdown write-half of the socket connection;
private  void socketAccept(SocketImpl s)
           
private  int socketAvailable()
           
private  void socketBind(InetAddress address, int port)
           
private  void socketClose0(boolean useDeferredClose)
           
private  void socketConnect(InetAddress address, int port, int timeout)
           
private  void socketCreate(boolean isServer)
           
private  int socketGetOption(int opt, Object iaContainerObj)
           
private  void socketListen(int count)
           
private  void socketSendUrgentData(int data)
           
private  void socketSetOption(int cmd, boolean on, Object value)
           
private  void socketShutdown(int howto)
           
protected  boolean supportsUrgentData()
          Returns whether or not this SocketImpl supports sending urgent data.
 
Methods inherited from class java.net.SocketImpl
getFileDescriptor, getInetAddress, getLocalPort, getPort, getServerSocket, getSocket, reset, setServerSocket, setSocket, toString
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

timeout

int timeout

trafficClass

private int trafficClass

shut_rd

private boolean shut_rd

shut_wr

private boolean shut_wr

socketInputStream

private SocketInputStream socketInputStream

fdUseCount

private int fdUseCount

fdLock

private Object fdLock

closePending

private boolean closePending

CONNECTION_NOT_RESET

private int CONNECTION_NOT_RESET

CONNECTION_RESET_PENDING

private int CONNECTION_RESET_PENDING

CONNECTION_RESET

private int CONNECTION_RESET

resetState

private int resetState

resetLock

private Object resetLock

SHUT_RD

public static final int SHUT_RD

SHUT_WR

public static final int SHUT_WR
Constructor Detail

PlainSocketImpl

PlainSocketImpl()
Constructs an empty instance.


PlainSocketImpl

PlainSocketImpl(FileDescriptor fd)
Constructs an instance with the given file descriptor.

Method Detail

create

protected void create(boolean stream)
               throws IOException
Creates a socket with a boolean that specifies whether this is a stream socket (true) or an unconnected UDP socket (false).

Specified by:
create in class SocketImpl
Parameters:
stream - if true, create a stream socket; otherwise, create a datagram socket.
IOException

connect

protected void connect(String host,
                       int port)
                throws UnknownHostException,
                       IOException
Creates a socket and connects it to the specified port on the specified host.

Specified by:
connect in class SocketImpl
Parameters:
host - the specified host
port - the specified port
UnknownHostException
IOException

connect

protected void connect(InetAddress address,
                       int port)
                throws IOException
Creates a socket and connects it to the specified address on the specified port.

Specified by:
connect in class SocketImpl
Parameters:
address - the address
port - the specified port
IOException

connect

protected void connect(SocketAddress address,
                       int timeout)
                throws IOException
Creates a socket and connects it to the specified address on the specified port.

Specified by:
connect in class SocketImpl
Parameters:
address - the address
timeout - the timeout value in milliseconds, or zero for no timeout.
Throws:
IOException - if connection fails
IllegalArgumentException - if address is null or is a SocketAddress subclass not supported by this socket
Since:
1.4

connectToAddress

private void connectToAddress(InetAddress address,
                              int port,
                              int timeout)
                       throws IOException
IOException

setOption

public void setOption(int opt,
                      Object val)
               throws SocketException
Description copied from interface: SocketOptions
Enabled: Enable/disable the option specified by optID. If the option is to be enabled, and it takes an option-specific "value", this is passed in value. The actual type of value is option-specific, and it is an error to pass something that isn't of the expected type:
 SocketImpl s;
 ...
 s.setOption(SO_LINGER, new Integer(10));
    // OK - set SO_LINGER w/ timeout of 10 sec.
 s.setOption(SO_LINGER, new Double(10));
    // ERROR - expects java.lang.Integer
If the requested option is binary, it can be set using this method by a java.lang.Boolean:
 s.setOption(TCP_NODELAY, new Boolean(true));
    // OK - enables TCP_NODELAY, a binary option
 

Any option can be disabled using this method with a Boolean(false):
 s.setOption(TCP_NODELAY, new Boolean(false));
    // OK - disables TCP_NODELAY
 s.setOption(SO_LINGER, new Boolean(false));
    // OK - disables SO_LINGER
 

For an option that has a notion of on and off, and requires a non-boolean parameter, setting its value to anything other than Boolean(false) implicitly enables it.
Throws SocketException if the option is unrecognized, the socket is closed, or some low-level error occurred

Parameters:
opt - identifies the option
val - the parameter of the socket option
Throws:
SocketException - if the option is unrecognized, the socket is closed, or some low-level error occurred
See Also:
SocketOptions.getOption(int)

getOption

public Object getOption(int opt)
                 throws SocketException
Description copied from interface: SocketOptions
Enabled: Fetch the value of an option. Binary options will return java.lang.Boolean(true) if enabled, java.lang.Boolean(false) if disabled, e.g.:
 SocketImpl s;
 ...
 Boolean noDelay = (Boolean)(s.getOption(TCP_NODELAY));
 if (noDelay.booleanValue()) {
     // true if TCP_NODELAY is enabled...
 ...
 }
 

For options that take a particular type as a parameter, getOption(int) will return the paramter's value, else it will return java.lang.Boolean(false):

 Object o = s.getOption(SO_LINGER);
 if (o instanceof Integer) {
     System.out.print("Linger time is " + ((Integer)o).intValue());
 } else {
   // the true type of o is java.lang.Boolean(false);
 }
 

Parameters:
opt - an int identifying the option to fetch
Returns:
the value of the option
Throws:
SocketException - if the socket is closed
See Also:
SocketOptions.setOption(int, java.lang.Object)

doConnect

private void doConnect(InetAddress address,
                       int port,
                       int timeout)
                throws IOException
The workhorse of the connection operation. Tries several times to establish a connection to the given . If unsuccessful, throws an IOException indicating what went wrong.

IOException

bind

protected void bind(InetAddress address,
                    int lport)
             throws IOException
Binds the socket to the specified address of the specified local port.

Specified by:
bind in class SocketImpl
Parameters:
address - the address
lport - the port number.
IOException

listen

protected void listen(int count)
               throws IOException
Listens, for a specified amount of time, for connections.

Specified by:
listen in class SocketImpl
Parameters:
count - the amount of time to listen for connections
IOException

accept

protected void accept(SocketImpl s)
               throws IOException
Accepts connections.

Specified by:
accept in class SocketImpl
Parameters:
s - the connection
IOException

getInputStream

protected InputStream getInputStream()
                              throws IOException
Gets an InputStream for this socket.

Specified by:
getInputStream in class SocketImpl
Returns:
a stream for reading from this socket.
IOException

setInputStream

void setInputStream(SocketInputStream in)

getOutputStream

protected OutputStream getOutputStream()
                                throws IOException
Gets an OutputStream for this socket.

Specified by:
getOutputStream in class SocketImpl
Returns:
an output stream for writing to this socket.
IOException

available

protected int available()
                 throws IOException
Returns the number of bytes that can be read without blocking.

Specified by:
available in class SocketImpl
Returns:
the number of bytes that can be read from this socket without blocking.
IOException

close

protected void close()
              throws IOException
Closes the socket.

Specified by:
close in class SocketImpl
IOException

shutdownInput

protected void shutdownInput()
                      throws IOException
Shutdown read-half of the socket connection;

Overrides:
shutdownInput in class SocketImpl
IOException
See Also:
java.net.Socket#shutdownOutput(), java.net.Socket#close(), java.net.Socket#setSoLinger(boolean, int)

shutdownOutput

protected void shutdownOutput()
                       throws IOException
Shutdown write-half of the socket connection;

Overrides:
shutdownOutput in class SocketImpl
IOException
See Also:
java.net.Socket#shutdownInput(), java.net.Socket#close(), java.net.Socket#setSoLinger(boolean, int)

supportsUrgentData

protected boolean supportsUrgentData()
Description copied from class: SocketImpl
Returns whether or not this SocketImpl supports sending urgent data. By default, false is returned unless the method is overridden in a sub-class

Overrides:
supportsUrgentData in class SocketImpl
Returns:
true if urgent data supported
See Also:
java.net.SocketImpl#address

sendUrgentData

protected void sendUrgentData(int data)
                       throws IOException
Description copied from class: SocketImpl
Send one byte of urgent data on the socket. The byte to be sent is the low eight bits of the parameter

Specified by:
sendUrgentData in class SocketImpl
Parameters:
data - The byte of data to send
IOException

finalize

protected void finalize()
                 throws IOException
Cleans up if the user forgets to close it.

Overrides:
finalize in class Object
IOException

acquireFD

public final FileDescriptor acquireFD()

releaseFD

public final void releaseFD()

isConnectionReset

public boolean isConnectionReset()

isConnectionResetPending

public boolean isConnectionResetPending()

setConnectionReset

public void setConnectionReset()

setConnectionResetPending

public void setConnectionResetPending()

isClosedOrPending

public boolean isClosedOrPending()

getTimeout

public int getTimeout()

socketCreate

private void socketCreate(boolean isServer)
                   throws IOException
IOException

socketConnect

private void socketConnect(InetAddress address,
                           int port,
                           int timeout)
                    throws IOException
IOException

socketBind

private void socketBind(InetAddress address,
                        int port)
                 throws IOException
IOException

socketListen

private void socketListen(int count)
                   throws IOException
IOException

socketAccept

private void socketAccept(SocketImpl s)
                   throws IOException
IOException

socketAvailable

private int socketAvailable()
                     throws IOException
IOException

socketClose0

private void socketClose0(boolean useDeferredClose)
                   throws IOException
IOException

socketShutdown

private void socketShutdown(int howto)
                     throws IOException
IOException

initProto

private static void initProto()

socketSetOption

private void socketSetOption(int cmd,
                             boolean on,
                             Object value)
                      throws SocketException
SocketException

socketGetOption

private int socketGetOption(int opt,
                            Object iaContainerObj)
                     throws SocketException
SocketException

socketSendUrgentData

private void socketSendUrgentData(int data)
                           throws IOException
IOException


comments?