Build 1.0_r1(from source)

java.net
Class Socket

java.lang.Object
  extended by java.net.Socket
Direct Known Subclasses:
SSLSocket

public class Socket
extends Object

This class represents sockets to be used in connection-oriented (streaming) protocols.


Field Summary
(package private) static SocketImplFactory factory
           
(package private) static int FLAG_SHUTDOWN
           
(package private)  SocketImpl impl
           
(package private) static int MULTICAST_IF
           
(package private) static int MULTICAST_TTL
           
(package private) static int TCP_NODELAY
           
 
Constructor Summary
  Socket()
          Construct a connection-oriented Socket.
  Socket(InetAddress dstAddress, int dstPort)
          Construct a stream socket connected to the nominated destination host address/port.
  Socket(InetAddress addr, int port, boolean streaming)
          Deprecated. As of JDK 1.1, replaced by Socket
  Socket(InetAddress dstAddress, int dstPort, InetAddress localAddress, int localPort)
          Construct a stream socket connected to the nominated destination host address/port.
  Socket(Proxy proxy)
          Constructs a connection-oriented Socket with specified proxy.
protected Socket(SocketImpl anImpl)
          Creates an unconnected socket, wrapping the socketImpl argument.
  Socket(String dstName, int dstPort)
          Construct a stream socket connected to the nominated destination host/port.
  Socket(String hostName, int port, boolean streaming)
          Deprecated. As of JDK 1.1, replaced by Socket
  Socket(String dstName, int dstPort, InetAddress localAddress, int localPort)
          Construct a stream socket connected to the nominated destination host/port.
 
Method Summary
(package private)  void accepted()
          Set the appropriate flags for a Socket created by ServerSocket.accept().
 void bind(SocketAddress localAddr)
          Bind the Socket to the nominated local host/port.
(package private)  void checkDestination(InetAddress destAddr, int dstPort)
          Check the connection destination satisfies the security policy and is in the valid port range.
 void close()
          Close the socket.
 void connect(SocketAddress remoteAddr)
          Connect the Socket to the host/port specified by the SocketAddress.
 void connect(SocketAddress remoteAddr, int timeout)
          Connect the Socket to the host/port specified by the SocketAddress with a specified timeout.
 SocketChannel getChannel()
          if Socket is created by a SocketChannel, returns the related SocketChannel
 InetAddress getInetAddress()
          Returns an InetAddress instance representing the address this socket has connected to.
 InputStream getInputStream()
          Answer the socket input stream, to read byte data off the socket.
 boolean getKeepAlive()
          Answer the SO_KEEPALIVE option for this socket.
 InetAddress getLocalAddress()
          Returns an InetAddress instance representing the local address this socket is bound to.
 int getLocalPort()
          Answer the local port to which the socket is bound.
 SocketAddress getLocalSocketAddress()
          Answer the local SocketAddress for this socket, or null if the socket is not bound.
 boolean getOOBInline()
          Get the state of the SO_OOBINLINE socket option.
 OutputStream getOutputStream()
          Answer the socket output stream, for writing byte data on the socket.
 int getPort()
          Returns the number of the remote port this socket is connected to.
 int getReceiveBufferSize()
          Answer the socket receive buffer size (SO_RCVBUF).
 SocketAddress getRemoteSocketAddress()
          Answer the remote SocketAddress for this socket, or null if the socket is not connected.
 boolean getReuseAddress()
          Get the state of the SO_REUSEADDR socket option.
 int getSendBufferSize()
          Answer the socket send buffer size (SO_SNDBUF).
 int getSoLinger()
          Answer the linger-on-close timeout for this socket (the SO_LINGER value).
 int getSoTimeout()
          Answer the socket read timeout.
 boolean getTcpNoDelay()
          Answer true if the socket is using Nagle's algorithm.
 int getTrafficClass()
          Get the IP_TOS socket option.
 boolean isBound()
          Return if the socket is bound to a local address and port.
 boolean isClosed()
          Return if the socket is closed.
 boolean isConnected()
          Return if the socket is connected.
 boolean isInputShutdown()
          Return if shutdownInput() has been called.
 boolean isOutputShutdown()
          Return if shutdownOutput() has been called.
(package private) static boolean preferIPv4Stack()
           
 void sendUrgentData(int value)
          Send the single byte of urgent data on the socket.
 void setKeepAlive(boolean value)
          Set the SO_KEEPALIVE option for this socket.
 void setOOBInline(boolean oobinline)
          Set the SO_OOBINLINE socket option.
 void setPerformancePreferences(int connectionTime, int latency, int bandwidth)
          sets performance preference for connectionTime,latency and bandwidth
 void setReceiveBufferSize(int size)
          Set the socket receive buffer size.
 void setReuseAddress(boolean reuse)
          Set the SO_REUSEADDR socket option.
 void setSendBufferSize(int size)
          Set the socket send buffer size.
static void setSocketImplFactory(SocketImplFactory fac)
          Specifies the application's socket implementation factory.
 void setSoLinger(boolean on, int timeout)
          Set the SO_LINGER option, with the specified time, in seconds.
 void setSoTimeout(int timeout)
          Set the read timeout on this socket.
 void setTcpNoDelay(boolean on)
          Set whether the socket is to use Nagle's algorithm.
 void setTrafficClass(int value)
          Set the IP_TOS socket option.
 void shutdownInput()
          Shutdown the input portion of the socket.
 void shutdownOutput()
          Shutdown the output portion of the socket.
(package private)  void startupSocket(InetAddress dstAddress, int dstPort, InetAddress localAddress, int localPort, boolean streaming)
          Creates a stream socket, binds it to the nominated local address/port, then connects it to the nominated destination address/port.
 String toString()
          Returns a string containing a concise, human-readable description of the socket.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

impl

SocketImpl impl

factory

static SocketImplFactory factory

MULTICAST_IF

static final int MULTICAST_IF
See Also:
Constant Field Values

MULTICAST_TTL

static final int MULTICAST_TTL
See Also:
Constant Field Values

TCP_NODELAY

static final int TCP_NODELAY
See Also:
Constant Field Values

FLAG_SHUTDOWN

static final int FLAG_SHUTDOWN
See Also:
Constant Field Values
Constructor Detail

Socket

public Socket()
Construct a connection-oriented Socket. The Socket is created in the factory if declared, or otherwise of the default type.

See Also:
SocketImplFactory

Socket

public Socket(Proxy proxy)
Constructs a connection-oriented Socket with specified proxy. Method checkConnect is called if a security manager exists, and the proxy host address and port number are passed as parameters.

Parameters:
proxy - the specified proxy for this Socket.
Throws:
IllegalArgumentException - if the proxy is null or of an invalid type.
SecurityException - if a security manager exists and it denies the permission to connect to proxy.

Socket

public Socket(String dstName,
              int dstPort)
       throws UnknownHostException,
              IOException
Construct a stream socket connected to the nominated destination host/port. By default, the socket binds it to any available port on the default localhost.

Parameters:
dstName - the destination host to connect to
dstPort - the port on the destination host to connect to
Throws:
UnknownHostException - if the host cannot be resolved
IOException - if an error occurs while instantiating the socket

Socket

public Socket(String dstName,
              int dstPort,
              InetAddress localAddress,
              int localPort)
       throws IOException
Construct a stream socket connected to the nominated destination host/port. The socket is bound it to the nominated localAddress/port.

Parameters:
dstName - the destination host to connect to
dstPort - the port on the destination host to connect to
localAddress - the local host address to bind to
localPort - the local port to bind to
Throws:
UnknownHostException - if the host cannot be resolved
IOException - if an error occurs while instantiating the socket

Socket

@Deprecated
public Socket(String hostName,
                         int port,
                         boolean streaming)
       throws IOException
Deprecated. As of JDK 1.1, replaced by Socket

Answer a new socket. This constructor is deprecated.

Parameters:
hostName - the host name
port - the port on the host
streaming - if true, answer a stream socket, else answer a a datagram socket.
Throws:
UnknownHostException - if the host cannot be resolved
IOException - if an error occurs while instantiating the socket
See Also:
Socket(String,int)

Socket

public Socket(InetAddress dstAddress,
              int dstPort)
       throws IOException
Construct a stream socket connected to the nominated destination host address/port. By default, the socket binds it to any available port on the default localhost.

Parameters:
dstAddress - the destination host address to connect to
dstPort - the port on the destination host to connect to
Throws:
IOException - if an error occurs while instantiating the socket

Socket

public Socket(InetAddress dstAddress,
              int dstPort,
              InetAddress localAddress,
              int localPort)
       throws IOException
Construct a stream socket connected to the nominated destination host address/port. The socket is bound it to the nominated localAddress/port.

Parameters:
dstAddress - the destination host address to connect to
dstPort - the port on the destination host to connect to
localAddress - the local host address to bind to
localPort - the local port to bind to
Throws:
IOException - if an error occurs while instantiating the socket

Socket

@Deprecated
public Socket(InetAddress addr,
                         int port,
                         boolean streaming)
       throws IOException
Deprecated. As of JDK 1.1, replaced by Socket

Answer a new socket. This constructor is deprecated.

Parameters:
addr - the internet address
port - the port on the host
streaming - if true, answer a stream socket, else answer a datagram socket.
Throws:
UnknownHostException - if the host cannot be resolved
IOException - if an error occurs while instantiating the socket
See Also:
Socket(InetAddress,int)

Socket

protected Socket(SocketImpl anImpl)
          throws SocketException
Creates an unconnected socket, wrapping the socketImpl argument.

Parameters:
anImpl - the socket to wrap
Throws:
SocketException - if an error occurs assigning the implementation
Method Detail

checkDestination

void checkDestination(InetAddress destAddr,
                      int dstPort)
Check the connection destination satisfies the security policy and is in the valid port range.

Parameters:
destAddr - the destination host address
dstPort - the port on the destination host

close

public void close()
           throws IOException
Close the socket. It is not valid to use the socket thereafter.

Throws:
IOException - if an error occurs during the close

getInetAddress

public InetAddress getInetAddress()
Returns an InetAddress instance representing the address this socket has connected to.

Returns:
if this socket is connected, the address it is connected to. A null return signifies no connection has been made.

getInputStream

public InputStream getInputStream()
                           throws IOException
Answer the socket input stream, to read byte data off the socket. Note, multiple input streams may be created on a single socket.

Returns:
a byte oriented read stream for this socket
Throws:
IOException - if an error occurs creating the stream
See Also:
SocketInputStream

getKeepAlive

public boolean getKeepAlive()
                     throws SocketException
Answer the SO_KEEPALIVE option for this socket.

Returns:
the socket SO_KEEPALIVE option setting
Throws:
SocketException - if an error occurs on the option access

getLocalAddress

public InetAddress getLocalAddress()
Returns an InetAddress instance representing the local address this socket is bound to.

Returns:
the local address that this socket has bound to

getLocalPort

public int getLocalPort()
Answer the local port to which the socket is bound.

Returns:
the local port to which the socket is bound

getOutputStream

public OutputStream getOutputStream()
                             throws IOException
Answer the socket output stream, for writing byte data on the socket. Note, multiplie output streams may be created on a single socket.

Returns:
OutputStream a byte oriented write stream for this socket
Throws:
IOException - if an error occurs creating the stream
See Also:
SocketOutputStream

getPort

public int getPort()
Returns the number of the remote port this socket is connected to.

Returns:
int the remote port number that this socket has connected to. A return of 0 (zero) indicates that there is no connection in place.

getSoLinger

public int getSoLinger()
                throws SocketException
Answer the linger-on-close timeout for this socket (the SO_LINGER value).

Returns:
this socket's SO_LINGER value. A value of -1 will be returned if the option is not enabled.
Throws:
SocketException - if an error occurs on querying this property

getReceiveBufferSize

public int getReceiveBufferSize()
                         throws SocketException
Answer the socket receive buffer size (SO_RCVBUF).

Returns:
socket receive buffer size
Throws:
SocketException - if an error occurs on the option access

getSendBufferSize

public int getSendBufferSize()
                      throws SocketException
Answer the socket send buffer size (SO_SNDBUF).

Returns:
socket send buffer size
Throws:
SocketException - if an error occurs on the option access

getSoTimeout

public int getSoTimeout()
                 throws SocketException
Answer the socket read timeout. The SO_TIMEOUT option, a value of 0 indicates it is disabled and a read operation will block indefinitely waiting for data.

Returns:
the socket read timeout
Throws:
SocketException - if an error occurs on the option access

getTcpNoDelay

public boolean getTcpNoDelay()
                      throws SocketException
Answer true if the socket is using Nagle's algorithm. The TCP_NODELAY option setting.

Returns:
the socket TCP_NODELAY option setting
Throws:
SocketException - if an error occurs on the option access

setKeepAlive

public void setKeepAlive(boolean value)
                  throws SocketException
Set the SO_KEEPALIVE option for this socket.

Parameters:
value - the socket SO_KEEPALIVE option setting
Throws:
SocketException - if an error occurs setting the option

setSocketImplFactory

public static void setSocketImplFactory(SocketImplFactory fac)
                                 throws IOException
Specifies the application's socket implementation factory. This may only be executed the once over the lifetime of the application.

Parameters:
fac - the socket factory to set
Throws:
IOException - thrown if the factory has already been set

setSendBufferSize

public void setSendBufferSize(int size)
                       throws SocketException
Set the socket send buffer size.

Parameters:
size - the buffer size, in bytes
Throws:
SocketException - if an error occurs while setting the size or the size is invalid.

setReceiveBufferSize

public void setReceiveBufferSize(int size)
                          throws SocketException
Set the socket receive buffer size.

Parameters:
size - the buffer size, in bytes
Throws:
SocketException - tf an error occurs while setting the size or the size is invalid.

setSoLinger

public void setSoLinger(boolean on,
                        int timeout)
                 throws SocketException
Set the SO_LINGER option, with the specified time, in seconds. The SO_LINGER option is silently limited to 65535 seconds.

Parameters:
on - if linger is enabled
timeout - the linger timeout value, in seconds
Throws:
SocketException - if an error occurs setting the option

setSoTimeout

public void setSoTimeout(int timeout)
                  throws SocketException
Set the read timeout on this socket. The SO_TIMEOUT option, is specified in milliseconds. The read operation will block indefinitely for a zero value.

Parameters:
timeout - the read timeout value
Throws:
SocketException - if an error occurs setting the option

setTcpNoDelay

public void setTcpNoDelay(boolean on)
                   throws SocketException
Set whether the socket is to use Nagle's algorithm. The TCP_NODELAY option setting.

Parameters:
on - the socket TCP_NODELAY option setting
Throws:
SocketException - if an error occurs setting the option

startupSocket

void startupSocket(InetAddress dstAddress,
                   int dstPort,
                   InetAddress localAddress,
                   int localPort,
                   boolean streaming)
             throws IOException
Creates a stream socket, binds it to the nominated local address/port, then connects it to the nominated destination address/port.

Parameters:
dstAddress - the destination host address
dstPort - the port on the destination host
localAddress - the address on the local machine to bind
localPort - the port on the local machine to bind
Throws:
IOException - thrown if a error occurs during the bind or connect operations

toString

public String toString()
Returns a string containing a concise, human-readable description of the socket.

Overrides:
toString in class Object
Returns:
the description

shutdownInput

public void shutdownInput()
                   throws IOException
Shutdown the input portion of the socket.

Throws:
IOException - if an error occurs while closing the socket input
SocketException - if the socket is closed

shutdownOutput

public void shutdownOutput()
                    throws IOException
Shutdown the output portion of the socket.

Throws:
IOException - if an error occurs while closing the socket output
SocketException - if the socket is closed

getLocalSocketAddress

public SocketAddress getLocalSocketAddress()
Answer the local SocketAddress for this socket, or null if the socket is not bound.

This is useful on multihomed hosts.

Returns:
the local socket address

getRemoteSocketAddress

public SocketAddress getRemoteSocketAddress()
Answer the remote SocketAddress for this socket, or null if the socket is not connected.

Returns:
the remote socket address

isBound

public boolean isBound()
Return if the socket is bound to a local address and port.

Returns:
true if the socket is bound to a local address, false otherwise.

isConnected

public boolean isConnected()
Return if the socket is connected.

Returns:
true if the socket is connected, false otherwise.

isClosed

public boolean isClosed()
Return if the socket is closed.

Returns:
true if the socket is closed, false otherwise.

bind

public void bind(SocketAddress localAddr)
          throws IOException
Bind the Socket to the nominated local host/port.

Parameters:
localAddr - the local machine address and port to bind on
Throws:
IllegalArgumentException - if the SocketAddress is not supported
IOException - if the socket is already bound, or a problem occurs during the bind

connect

public void connect(SocketAddress remoteAddr)
             throws IOException
Connect the Socket to the host/port specified by the SocketAddress.

Parameters:
remoteAddr - the remote machine address and port to connect to
Throws:
IllegalArgumentException - if the SocketAddress is not supported
IOException - if the socket is already connected, or a problem occurs during the connect

connect

public void connect(SocketAddress remoteAddr,
                    int timeout)
             throws IOException
Connect the Socket to the host/port specified by the SocketAddress with a specified timeout.

Parameters:
remoteAddr - the remote machine address and port to connect to
timeout - the millisecond timeout value, the connect will block indefinitely for a zero value.
Throws:
IllegalArgumentException - if the timeout is negative, or the SocketAddress is not supported
IOException - if the socket is already connected, or a problem occurs during the connect

isInputShutdown

public boolean isInputShutdown()
Return if shutdownInput() has been called.

Returns:
true if shutdownInput has been called, false otherwise.

isOutputShutdown

public boolean isOutputShutdown()
Return if shutdownOutput() has been called.

Returns:
true if shutdownOutput has been called, false otherwise.

setReuseAddress

public void setReuseAddress(boolean reuse)
                     throws SocketException
Set the SO_REUSEADDR socket option.

Parameters:
reuse - the socket SO_REUSEADDR option setting
Throws:
SocketException - if the socket is closed or the option is invalid.

getReuseAddress

public boolean getReuseAddress()
                        throws SocketException
Get the state of the SO_REUSEADDR socket option.

Returns:
true if the SO_REUSEADDR is enabled, false otherwise.
Throws:
SocketException - if the socket is closed or the option is invalid.

setOOBInline

public void setOOBInline(boolean oobinline)
                  throws SocketException
Set the SO_OOBINLINE socket option. When this option is enabled, out of band data is recieved in the normal data stream.

Parameters:
oobinline - the socket SO_OOBINLINE option setting
Throws:
SocketException - if the socket is closed or the option is invalid.

getOOBInline

public boolean getOOBInline()
                     throws SocketException
Get the state of the SO_OOBINLINE socket option.

Returns:
true if the SO_OOBINLINE is enabled, false otherwise.
Throws:
SocketException - if the socket is closed or the option is invalid.

setTrafficClass

public void setTrafficClass(int value)
                     throws SocketException
Set the IP_TOS socket option.

Parameters:
value - the socket IP_TOS setting
Throws:
SocketException - if the socket is closed or the option is invalid.

getTrafficClass

public int getTrafficClass()
                    throws SocketException
Get the IP_TOS socket option.

Returns:
the IP_TOS socket option value
Throws:
SocketException - if the option is invalid

sendUrgentData

public void sendUrgentData(int value)
                    throws IOException
Send the single byte of urgent data on the socket.

Parameters:
value - the byte of urgent data
Throws:
IOException - when an error occurs sending urgent data

accepted

void accepted()
Set the appropriate flags for a Socket created by ServerSocket.accept().

See Also:
ServerSocket.implAccept(java.net.Socket)

preferIPv4Stack

static boolean preferIPv4Stack()

getChannel

public SocketChannel getChannel()
if Socket is created by a SocketChannel, returns the related SocketChannel

Returns:
the related SocketChannel

setPerformancePreferences

public void setPerformancePreferences(int connectionTime,
                                      int latency,
                                      int bandwidth)
sets performance preference for connectionTime,latency and bandwidth

Parameters:
connectionTime - the importance of connect time
latency - the importance of latency
bandwidth - the importance of bandwidth

Build 1.0_r1(from source)

Please submit a feedback, bug or feature