Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

java.net
Class Socket  view Socket download Socket.java

java.lang.Object
  extended byjava.net.Socket

public class Socket
extends java.lang.Object

This class models a client site socket. A socket is a TCP/IP endpoint for network communications conceptually similar to a file handle.

This class does not actually do any work. Instead, it redirects all of its calls to a socket implementation object which implements the SocketImpl interface. The implementation class is instantiated by factory class that implements the SocketImplFactory interface. A default factory is provided, however the factory may be set by a call to the setSocketImplFactory method. Note that this may only be done once per virtual machine. If a subsequent attempt is made to set the factory, a SocketException will be thrown.


Field Summary
(package private)  boolean bound
          True if the socket is bound.
(package private) static SocketImplFactory factory
          This is the user SocketImplFactory for this class.
(package private)  SocketImpl impl
          The implementation object to which calls are redirected
(package private)  boolean implCreated
          True if socket implementation was created by calling their create() method.
private  boolean inputShutdown
          True if input is shutdown.
private  boolean outputShutdown
          True if output is shutdown.
 
Constructor Summary
  Socket()
          Initializes a new instance of Socket object without connecting to a remote host.
  Socket(InetAddress address, int port)
          Initializes a new instance of Socket and connects to the address and port number specified as arguments.
  Socket(InetAddress host, int port, boolean stream)
          Deprecated. Use the DatagramSocket class to create datagram oriented sockets.
  Socket(InetAddress address, int port, InetAddress localAddr, int localPort)
          Initializes a new instance of Socket and connects to the address and port number specified as arguments, plus binds to the specified local address and port.
private Socket(InetAddress raddr, int rport, InetAddress laddr, int lport, boolean stream)
          This constructor is where the real work takes place.
protected Socket(SocketImpl impl)
          Initializes a new instance of Socket object without connecting to a remote host.
  Socket(java.lang.String host, int port)
          Initializes a new instance of Socket and connects to the hostname and port specified as arguments.
  Socket(java.lang.String host, int port, boolean stream)
          Deprecated. Use the DatagramSocket class to create datagram oriented sockets.
  Socket(java.lang.String host, int port, InetAddress localAddr, int localPort)
          Initializes a new instance of Socket that connects to the named host on the specified port and binds to the specified local address and port.
 
Method Summary
 void bind(SocketAddress bindpoint)
          Binds the socket to the givent local address/port
 void close()
          Closes the socket.
 void connect(SocketAddress endpoint)
          Connects the socket with a remote address.
 void connect(SocketAddress endpoint, int timeout)
          Connects the socket with a remote address.
 java.nio.channels.SocketChannel getChannel()
          Returns the socket channel associated with this socket.
private  SocketImpl getImpl()
           
 InetAddress getInetAddress()
          Returns the address of the remote end of the socket.
 java.io.InputStream getInputStream()
          Returns an InputStream for reading from this socket.
 boolean getKeepAlive()
          This method returns the value of the socket level socket option SO_KEEPALIVE.
 InetAddress getLocalAddress()
          Returns the local address to which this socket is bound.
 int getLocalPort()
          Returns the local port number to which this socket is bound.
 SocketAddress getLocalSocketAddress()
          Returns local socket address.
 boolean getOOBInline()
          Returns the current setting of the SO_OOBINLINE option for this socket
 java.io.OutputStream getOutputStream()
          Returns an OutputStream for writing to this socket.
 int getPort()
          Returns the port number of the remote end of the socket connection.
 int getReceiveBufferSize()
          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.
 SocketAddress getRemoteSocketAddress()
          Returns the remote socket address.
 boolean getReuseAddress()
          Checks if the SO_REUSEADDR option is enabled
 int getSendBufferSize()
          This method returns the value of the system level socket option SO_SNDBUF, which is used by the operating system to tune buffer sizes for data transfers.
 int getSoLinger()
          Returns the value of the SO_LINGER option on the socket.
 int getSoTimeout()
          Returns the value of the SO_TIMEOUT option on the socket.
 boolean getTcpNoDelay()
          Tests whether or not the TCP_NODELAY option is set on the socket.
 int getTrafficClass()
          Returns the current traffic class
 boolean isBound()
          Checks if the socket is already bound.
 boolean isClosed()
          Checks if the socket is closed.
 boolean isConnected()
          Checks if the socket is connected
 boolean isInputShutdown()
          Checks if the socket's input stream is shutdown
 boolean isOutputShutdown()
          Checks if the socket's output stream is shutdown
 void sendUrgentData(int data)
          Sends urgent data through the socket
 void setKeepAlive(boolean on)
          This method sets the value for the socket level socket option SO_KEEPALIVE.
 void setOOBInline(boolean on)
          Enables/disables the SO_OOBINLINE option
 void setReceiveBufferSize(int size)
          This method sets the value for the system level socket option SO_RCVBUF to the specified value.
 void setReuseAddress(boolean reuseAddress)
          Enables/Disables the SO_REUSEADDR option
 void setSendBufferSize(int size)
          This method sets the value for the system level socket option SO_SNDBUF to the specified value.
static void setSocketImplFactory(SocketImplFactory fac)
          Sets the SocketImplFactory.
 void setSoLinger(boolean on, int linger)
          Sets the value of the SO_LINGER option on the socket.
 void setSoTimeout(int timeout)
          Sets the value of the SO_TIMEOUT option on the socket.
 void setTcpNoDelay(boolean on)
          Sets the TCP_NODELAY option on the socket.
 void setTrafficClass(int tc)
          Sets the traffic class value
 void shutdownInput()
          Closes the input side of the socket stream.
 void shutdownOutput()
          Closes the output side of the socket stream.
 java.lang.String toString()
          Converts this Socket to a String.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

factory

static SocketImplFactory factory
This is the user SocketImplFactory for this class. If this variable is null, a default factory is used.


impl

SocketImpl impl
The implementation object to which calls are redirected


implCreated

boolean implCreated
True if socket implementation was created by calling their create() method.


bound

boolean bound
True if the socket is bound. Package private so it can be set from ServerSocket when accept is called.


inputShutdown

private boolean inputShutdown
True if input is shutdown.


outputShutdown

private boolean outputShutdown
True if output is shutdown.

Constructor Detail

Socket

public Socket()
Initializes a new instance of Socket object without connecting to a remote host. This useful for subclasses of socket that might want this behavior.

Since:
1.1

Socket

protected Socket(SocketImpl impl)
          throws SocketException
Initializes a new instance of Socket object without connecting to a remote host. This is useful for subclasses of socket that might want this behavior.

Additionally, this socket will be created using the supplied implementation class instead the default class or one returned by a factory. If this value is null, the default Socket implementation is used.

Since:
1.1

Socket

public Socket(java.lang.String host,
              int port)
       throws UnknownHostException,
              java.io.IOException
Initializes a new instance of Socket and connects to the hostname and port specified as arguments.


Socket

public Socket(InetAddress address,
              int port)
       throws java.io.IOException
Initializes a new instance of Socket and connects to the address and port number specified as arguments.


Socket

public Socket(java.lang.String host,
              int port,
              InetAddress localAddr,
              int localPort)
       throws java.io.IOException
Initializes a new instance of Socket that connects to the named host on the specified port and binds to the specified local address and port.

Since:
1.1

Socket

public Socket(InetAddress address,
              int port,
              InetAddress localAddr,
              int localPort)
       throws java.io.IOException
Initializes a new instance of Socket and connects to the address and port number specified as arguments, plus binds to the specified local address and port.

Since:
1.1

Socket

public Socket(java.lang.String host,
              int port,
              boolean stream)
       throws java.io.IOException
Deprecated. Use the DatagramSocket class to create datagram oriented sockets.

Initializes a new instance of Socket and connects to the hostname and port specified as arguments. If the stream argument is set to true, then a stream socket is created. If it is false, a datagram socket is created.


Socket

public Socket(InetAddress host,
              int port,
              boolean stream)
       throws java.io.IOException
Deprecated. Use the DatagramSocket class to create datagram oriented sockets.

Initializes a new instance of Socket and connects to the address and port number specified as arguments. If the stream param is true, a stream socket will be created, otherwise a datagram socket is created.


Socket

private Socket(InetAddress raddr,
               int rport,
               InetAddress laddr,
               int lport,
               boolean stream)
        throws java.io.IOException
This constructor is where the real work takes place. Connect to the specified address and port. Use default local values if not specified, otherwise use the local host and port passed in. Create as stream or datagram based on "stream" argument.

Method Detail

getImpl

private SocketImpl getImpl()
                    throws SocketException

bind

public void bind(SocketAddress bindpoint)
          throws java.io.IOException
Binds the socket to the givent local address/port

Since:
1.4

connect

public void connect(SocketAddress endpoint)
             throws java.io.IOException
Connects the socket with a remote address.

Since:
1.4

connect

public void connect(SocketAddress endpoint,
                    int timeout)
             throws java.io.IOException
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.

Since:
1.4

getInetAddress

public InetAddress getInetAddress()
Returns the address of the remote end of the socket. If this socket is not connected, then null is returned.


getLocalAddress

public InetAddress getLocalAddress()
Returns the local address to which this socket is bound. If this socket is not connected, then a wildcard address, for which

Since:
1.1

getPort

public int getPort()
Returns the port number of the remote end of the socket connection. If this socket is not connected, then 0 is returned.


getLocalPort

public int getLocalPort()
Returns the local port number to which this socket is bound. If this socket is not connected, then -1 is returned.


getLocalSocketAddress

public SocketAddress getLocalSocketAddress()
Returns local socket address.

Since:
1.4

getRemoteSocketAddress

public SocketAddress getRemoteSocketAddress()
Returns the remote socket address.

Since:
1.4

getInputStream

public java.io.InputStream getInputStream()
                                   throws java.io.IOException
Returns an InputStream for reading from this socket.


getOutputStream

public java.io.OutputStream getOutputStream()
                                     throws java.io.IOException
Returns an OutputStream for writing to this socket.


setTcpNoDelay

public void setTcpNoDelay(boolean on)
                   throws SocketException
Sets the TCP_NODELAY option on the socket.

Since:
1.1

getTcpNoDelay

public boolean getTcpNoDelay()
                      throws SocketException
Tests whether or not the TCP_NODELAY option is set on the socket. Returns true if enabled, false if disabled. When on it disables the Nagle algorithm which means that packets are always send immediatly and never merged together to reduce network trafic.

Since:
1.1

setSoLinger

public void setSoLinger(boolean on,
                        int linger)
                 throws SocketException
Sets the value of the SO_LINGER option on the socket. If the SO_LINGER option is set on a socket and there is still data waiting to be sent when the socket is closed, then the close operation will block until either that data is delivered or until the timeout period expires. The linger interval is specified in hundreths of a second (platform specific?)

Since:
1.1

getSoLinger

public int getSoLinger()
                throws SocketException
Returns the value of the SO_LINGER option on the socket. If the SO_LINGER option is set on a socket and there is still data waiting to be sent when the socket is closed, then the close operation will block until either that data is delivered or until the timeout period expires. This method either returns the timeouts (in hundredths of of a second (platform specific?)) if SO_LINGER is set, or -1 if SO_LINGER is not set.

Since:
1.1

sendUrgentData

public void sendUrgentData(int data)
                    throws java.io.IOException
Sends urgent data through the socket

Since:
1.4

setOOBInline

public void setOOBInline(boolean on)
                  throws SocketException
Enables/disables the SO_OOBINLINE option

Since:
1.4

getOOBInline

public boolean getOOBInline()
                     throws SocketException
Returns the current setting of the SO_OOBINLINE option for this socket

Since:
1.4

setSoTimeout

public void setSoTimeout(int timeout)
                  throws SocketException
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 the timeout period, a short count is returned (or an EWOULDBLOCK signal would be sent in Unix if no data had been read). A value of 0 for this option implies that there is no timeout (ie, operations will block forever). On systems that have separate read and write timeout values, this method returns the read timeout. This value is in milliseconds.

Since:
1.1

getSoTimeout

public int getSoTimeout()
                 throws SocketException
Returns 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 the timeout period, a short count is returned (or an EWOULDBLOCK signal would be sent in Unix if no data had been read). A value of 0 for this option implies that there is no timeout (ie, operations will block forever). On systems that have separate read and write timeout values, this method returns the read timeout. This value is in thousandths of a second (implementation specific?).

Since:
1.1

setSendBufferSize

public void setSendBufferSize(int size)
                       throws SocketException
This method sets the value for the system level socket option SO_SNDBUF to the specified value. Note that valid values for this option are specific to a given operating system.

Since:
1.2

getSendBufferSize

public int getSendBufferSize()
                      throws SocketException
This method returns the value of the system level socket option SO_SNDBUF, which is used by the operating system to tune buffer sizes for data transfers.

Since:
1.2

setReceiveBufferSize

public void setReceiveBufferSize(int size)
                          throws SocketException
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.

Since:
1.2

getReceiveBufferSize

public int getReceiveBufferSize()
                         throws SocketException
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.

Since:
1.2

setKeepAlive

public void setKeepAlive(boolean on)
                  throws SocketException
This method sets the value for the socket level socket option SO_KEEPALIVE.

Since:
1.3

getKeepAlive

public boolean getKeepAlive()
                     throws SocketException
This method returns the value of the socket level socket option SO_KEEPALIVE.

Since:
1.3

close

public void close()
           throws java.io.IOException
Closes the socket.


toString

public java.lang.String toString()
Converts this Socket to a String.


setSocketImplFactory

public static void setSocketImplFactory(SocketImplFactory fac)
                                 throws java.io.IOException
Sets the SocketImplFactory. This may be done only once per virtual machine. Subsequent attempts will generate a SocketException. Note that a SecurityManager check is made prior to setting the factory. If insufficient privileges exist to set the factory, then an IOException will be thrown.


shutdownInput

public void shutdownInput()
                   throws java.io.IOException
Closes the input side of the socket stream.

Since:
1.3

shutdownOutput

public void shutdownOutput()
                    throws java.io.IOException
Closes the output side of the socket stream.

Since:
1.3

getChannel

public java.nio.channels.SocketChannel getChannel()
Returns the socket channel associated with this socket.

Since:
1.4

getReuseAddress

public boolean getReuseAddress()
                        throws SocketException
Checks if the SO_REUSEADDR option is enabled

Since:
1.4

setReuseAddress

public void setReuseAddress(boolean reuseAddress)
                     throws SocketException
Enables/Disables the SO_REUSEADDR option

Since:
1.4

getTrafficClass

public int getTrafficClass()
                    throws SocketException
Returns the current traffic class

Since:
1.4

setTrafficClass

public void setTrafficClass(int tc)
                     throws SocketException
Sets the traffic class value

Since:
1.4

isConnected

public boolean isConnected()
Checks if the socket is connected

Since:
1.4

isBound

public boolean isBound()
Checks if the socket is already bound.

Since:
1.4

isClosed

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

Since:
1.4

isInputShutdown

public boolean isInputShutdown()
Checks if the socket's input stream is shutdown

Since:
1.4

isOutputShutdown

public boolean isOutputShutdown()
Checks if the socket's output stream is shutdown

Since:
1.4