Spec-Zone .ru
спецификации, руководства, описания, API
JavaTM 2 Platform
Std. Ed. v1.3.1

java.net
Class Socket

java.lang.Object
  |
  +--java.net.Socket

public class Socket
extends Object

This class implements client sockets (also called just "sockets"). A socket is an endpoint for communication between two machines.

The actual work of the socket is performed by an instance of the SocketImpl class. An application, by changing the socket factory that creates the socket implementation, can configure itself to create sockets appropriate to the local firewall.

Since:
JDK1.0
See Also:
setSocketImplFactory(java.net.SocketImplFactory), SocketImpl

Constructor Summary
protected Socket()
          Creates an unconnected socket, with the system-default type of SocketImpl.
  Socket(InetAddress address, int port)
          Creates a stream socket and connects it to the specified port number at the specified IP address.
  Socket(InetAddress host, int port, boolean stream)
          Deprecated. Use DatagramSocket instead for UDP transport.
  Socket(InetAddress address, int port, InetAddress localAddr, int localPort)
          Creates a socket and connects it to the specified remote address on the specified remote port.
protected Socket(SocketImpl impl)
          Creates an unconnected Socket with a user-specified SocketImpl.
  Socket(String host, int port)
          Creates a stream socket and connects it to the specified port number on the named host.
  Socket(String host, int port, boolean stream)
          Deprecated. Use DatagramSocket instead for UDP transport.
  Socket(String host, int port, InetAddress localAddr, int localPort)
          Creates a socket and connects it to the specified remote host on the specified remote port.
 
Method Summary
 void close()
          Closes this socket.
 InetAddress getInetAddress()
          Returns the address to which the socket is connected.
 InputStream getInputStream()
          Returns an input stream for this socket.
 boolean getKeepAlive()
          Tests if SO_KEEPALIVE is enabled.
 InetAddress getLocalAddress()
          Gets the local address to which the socket is bound.
 int getLocalPort()
          Returns the local port to which this socket is bound.
 OutputStream getOutputStream()
          Returns an output stream for this socket.
 int getPort()
          Returns the remote port to which this socket is connected.
 int getReceiveBufferSize()
          Gets the value of the SO_RCVBUF option for this Socket, that is the buffer size used by the platform for input on this Socket.
 int getSendBufferSize()
          Get value of the SO_SNDBUF option for this Socket, that is the buffer size used by the platform for output on this Socket.
 int getSoLinger()
          Returns setting for SO_LINGER.
 int getSoTimeout()
          Returns setting for SO_TIMEOUT.
 boolean getTcpNoDelay()
          Tests if TCP_NODELAY is enabled.
 void setKeepAlive(boolean on)
          Enable/disable SO_KEEPALIVE.
 void setReceiveBufferSize(int size)
          Sets the SO_RCVBUF option to the specified value for this Socket.
 void setSendBufferSize(int size)
          Sets the SO_SNDBUF option to the specified value for this Socket.
static void setSocketImplFactory(SocketImplFactory fac)
          Sets the client socket implementation factory for the application.
 void setSoLinger(boolean on, int linger)
          Enable/disable SO_LINGER with the specified linger time in seconds.
 void setSoTimeout(int timeout)
          Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds.
 void setTcpNoDelay(boolean on)
          Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm).
 void shutdownInput()
          Places the input stream for this socket at "end of stream".
 void shutdownOutput()
          Disables the output stream for this socket.
 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
 

Constructor Detail

Socket

protected Socket()
Creates an unconnected socket, with the system-default type of SocketImpl.
Since:
JDK1.1

Socket

protected Socket(SocketImpl impl)
          throws SocketException
Creates an unconnected Socket with a user-specified SocketImpl.

Parameters:
impl - an instance of a SocketImpl the subclass wishes to use on the Socket.
Throws:
SocketException - if there is an error in the underlying protocol, such as a TCP error.
Since:
JDK1.1

Socket

public Socket(String host,
              int port)
       throws UnknownHostException,
              IOException
Creates a stream socket and connects it to the specified port number on the named host.

If the application has specified a server socket factory, that factory's createSocketImpl method is called to create the actual socket implementation. Otherwise a "plain" socket is created.

If there is a security manager, its checkConnect method is called with the host address and port as its arguments. This could result in a SecurityException.

Parameters:
host - the host name.
port - the port number.
Throws:
UnknownHostException - if the IP address of the host could not be determined.
IOException - if an I/O error occurs when creating the socket.
SecurityException - if a security manager exists and its checkConnect method doesn't allow the operation.
See Also:
setSocketImplFactory(java.net.SocketImplFactory), SocketImpl, SocketImplFactory.createSocketImpl(), SecurityManager.checkConnect(java.lang.String, int)

Socket

public Socket(InetAddress address,
              int port)
       throws IOException
Creates a stream socket and connects it to the specified port number at the specified IP address.

If the application has specified a socket factory, that factory's createSocketImpl method is called to create the actual socket implementation. Otherwise a "plain" socket is created.

If there is a security manager, its checkConnect method is called with the host address and port as its arguments. This could result in a SecurityException.

Parameters:
address - the IP address.
port - the port number.
Throws:
IOException - if an I/O error occurs when creating the socket.
SecurityException - if a security manager exists and its checkConnect method doesn't allow the operation.
See Also:
setSocketImplFactory(java.net.SocketImplFactory), SocketImpl, SocketImplFactory.createSocketImpl(), SecurityManager.checkConnect(java.lang.String, int)

Socket

public Socket(String host,
              int port,
              InetAddress localAddr,
              int localPort)
       throws IOException
Creates a socket and connects it to the specified remote host on the specified remote port. The Socket will also bind() to the local address and port supplied.

If there is a security manager, its checkConnect method is called with the host address and port as its arguments. This could result in a SecurityException.

Parameters:
host - the name of the remote host
port - the remote port
localAddr - the local address the socket is bound to
localPort - the local port the socket is bound to
Throws:
IOException - if an I/O error occurs when creating the socket.
SecurityException - if a security manager exists and its checkConnect method doesn't allow the operation.
Since:
JDK1.1
See Also:
SecurityManager.checkConnect(java.lang.String, int)

Socket

public Socket(InetAddress address,
              int port,
              InetAddress localAddr,
              int localPort)
       throws IOException
Creates a socket and connects it to the specified remote address on the specified remote port. The Socket will also bind() to the local address and port supplied.

If there is a security manager, its checkConnect method is called with the host address and port as its arguments. This could result in a SecurityException.

Parameters:
address - the remote address
port - the remote port
localAddr - the local address the socket is bound to
localPort - the local port the socket is bound to
Throws:
IOException - if an I/O error occurs when creating the socket.
SecurityException - if a security manager exists and its checkConnect method doesn't allow the operation.
Since:
JDK1.1
See Also:
SecurityManager.checkConnect(java.lang.String, int)

Socket

public Socket(String host,
              int port,
              boolean stream)
       throws IOException
Deprecated. Use DatagramSocket instead for UDP transport.

Creates a stream socket and connects it to the specified port number on the named host.

If the stream argument is true, this creates a stream socket. If the stream argument is false, it creates a datagram socket.

If the application has specified a server socket factory, that factory's createSocketImpl method is called to create the actual socket implementation. Otherwise a "plain" socket is created.

If there is a security manager, its checkConnect method is called with the host address and port as its arguments. This could result in a SecurityException.

Parameters:
host - the host name.
port - the port number.
stream - a boolean indicating whether this is a stream socket or a datagram socket.
Throws:
IOException - if an I/O error occurs when creating the socket.
SecurityException - if a security manager exists and its checkConnect method doesn't allow the operation.
See Also:
setSocketImplFactory(java.net.SocketImplFactory), SocketImpl, SocketImplFactory.createSocketImpl(), SecurityManager.checkConnect(java.lang.String, int)

Socket

public Socket(InetAddress host,
              int port,
              boolean stream)
       throws IOException
Deprecated. Use DatagramSocket instead for UDP transport.

Creates a socket and connects it to the specified port number at the specified IP address.

If the stream argument is true, this creates a stream socket. If the stream argument is false, it creates a datagram socket.

If the application has specified a server socket factory, that factory's createSocketImpl method is called to create the actual socket implementation. Otherwise a "plain" socket is created.

If there is a security manager, its checkConnect method is called with host.getHostAddress() and port as its arguments. This could result in a SecurityException.

Parameters:
host - the IP address.
port - the port number.
stream - if true, create a stream socket; otherwise, create a datagram socket.
Throws:
IOException - if an I/O error occurs when creating the socket.
SecurityException - if a security manager exists and its checkConnect method doesn't allow the operation.
See Also:
setSocketImplFactory(java.net.SocketImplFactory), SocketImpl, SocketImplFactory.createSocketImpl(), SecurityManager.checkConnect(java.lang.String, int)
Method Detail

getInetAddress

public InetAddress getInetAddress()
Returns the address to which the socket is connected.
Returns:
the remote IP address to which this socket is connected.

getLocalAddress

public InetAddress getLocalAddress()
Gets the local address to which the socket is bound.
Returns:
the local address to which the socket is bound.
Since:
JDK1.1

getPort

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

getLocalPort

public int getLocalPort()
Returns the local port to which this socket is bound.
Returns:
the local port number to which this socket is connected.

getInputStream

public InputStream getInputStream()
                           throws IOException
Returns an input stream for this socket.
Returns:
an input stream for reading bytes from this socket.
Throws:
IOException - if an I/O error occurs when creating the input stream.

getOutputStream

public OutputStream getOutputStream()
                             throws IOException
Returns an output stream for this socket.
Returns:
an output stream for writing bytes to this socket.
Throws:
IOException - if an I/O error occurs when creating the output stream.

setTcpNoDelay

public void setTcpNoDelay(boolean on)
                   throws SocketException
Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm).
Parameters:
on - true to enable TCP_NODELAY, false to disable.
Throws:
SocketException - if there is an error in the underlying protocol, such as a TCP error.
Since:
JDK1.1
See Also:
getTcpNoDelay()

getTcpNoDelay

public boolean getTcpNoDelay()
                      throws SocketException
Tests if TCP_NODELAY is enabled.
Returns:
a boolean indicating whether or not TCP_NODELAY is enabled.
Throws:
SocketException - if there is an error in the underlying protocol, such as a TCP error.
Since:
JDK1.1
See Also:
setTcpNoDelay(boolean)

setSoLinger

public void setSoLinger(boolean on,
                        int linger)
                 throws SocketException
Enable/disable SO_LINGER with the specified linger time in seconds. The maximum timeout value is platform specific. The setting only affects socket close.
Parameters:
on - whether or not to linger on.
linger - how to linger for, if on is true.
Throws:
SocketException - if there is an error in the underlying protocol, such as a TCP error.
IllegalArgumentException - if the linger value is negative.
Since:
JDK1.1
See Also:
getSoLinger()

getSoLinger

public int getSoLinger()
                throws SocketException
Returns setting for SO_LINGER. -1 returns implies that the option is disabled. The setting only affects socket close.
Returns:
the setting for SO_LINGER.
Throws:
SocketException - if there is an error in the underlying protocol, such as a TCP error.
Since:
JDK1.1
See Also:
setSoLinger(boolean, int)

setSoTimeout

public void setSoTimeout(int timeout)
                  throws SocketException
Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. With this option set to a non-zero timeout, a read() call on the InputStream associated with this Socket will block for only this amount of time. If the timeout expires, a java.io.InterruptedIOException is raised, though the Socket is still valid. The option must be enabled prior to entering the blocking operation to have effect. The timeout must be > 0. A timeout of zero is interpreted as an infinite timeout.
Parameters:
timeout - the specified timeout, in milliseconds.
Throws:
SocketException - if there is an error in the underlying protocol, such as a TCP error.
Since:
JDK 1.1
See Also:
getSoTimeout()

getSoTimeout

public int getSoTimeout()
                 throws SocketException
Returns setting for SO_TIMEOUT. 0 returns implies that the option is disabled (i.e., timeout of infinity).
Returns:
the setting for SO_TIMEOUT
Throws:
SocketException - if there is an error in the underlying protocol, such as a TCP error.
Since:
JDK1.1
See Also:
setSoTimeout(int)

setSendBufferSize

public void setSendBufferSize(int size)
                       throws SocketException
Sets the SO_SNDBUF option to the specified value for this Socket. The SO_SNDBUF option is used by the platform's networking code as a hint for the size to set the underlying network I/O buffers.

Increasing buffer size can increase the performance of network I/O for high-volume connection, while decreasing it can help reduce the backlog of incoming data. For UDP, this sets the maximum size of a packet that may be sent on this Socket.

Because SO_SNDBUF is a hint, applications that want to verify what size the buffers were set to should call getSendBufferSize().

Parameters:
size - the size to which to set the send buffer size. This value must be greater than 0.
Throws:
SocketException - if there is an error in the underlying protocol, such as a TCP error.
IllegalArgumentException - if the value is 0 or is negative.
See Also:
getSendBufferSize()

getSendBufferSize

public int getSendBufferSize()
                      throws SocketException
Get value of the SO_SNDBUF option for this Socket, that is the buffer size used by the platform for output on this Socket.
Returns:
the value of the SO_SNDBUF option for this Socket.
Throws:
SocketException - if there is an error in the underlying protocol, such as a TCP error.
See Also:
setSendBufferSize(int)

setReceiveBufferSize

public void setReceiveBufferSize(int size)
                          throws SocketException
Sets the SO_RCVBUF option to the specified value for this Socket. The SO_RCVBUF option is used by the platform's networking code as a hint for the size to set the underlying network I/O buffers.

Increasing buffer size can increase the performance of network I/O for high-volume connection, while decreasing it can help reduce the backlog of incoming data. For UDP, this sets the maximum size of a packet that may be sent on this Socket.

Because SO_RCVBUF is a hint, applications that want to verify what size the buffers were set to should call getReceiveBufferSize().

Parameters:
size - the size to which to set the receive buffer size. This value must be greater than 0.
Throws:
IllegalArgumentException - if the value is 0 or is negative.
SocketException - if there is an error in the underlying protocol, such as a TCP error.
See Also:
getReceiveBufferSize()

getReceiveBufferSize

public int getReceiveBufferSize()
                         throws SocketException
Gets the value of the SO_RCVBUF option for this Socket, that is the buffer size used by the platform for input on this Socket.
Returns:
the value of the SO_RCVBUF option for this Socket.
Throws:
SocketException - if there is an error in the underlying protocol, such as a TCP error.
See Also:
setReceiveBufferSize(int)

setKeepAlive

public void setKeepAlive(boolean on)
                  throws SocketException
Enable/disable SO_KEEPALIVE.
Parameters:
on - whether or not to have socket keep alive turned on.
Throws:
SocketException - if there is an error in the underlying protocol, such as a TCP error.
Since:
1.3
See Also:
getKeepAlive()

getKeepAlive

public boolean getKeepAlive()
                     throws SocketException
Tests if SO_KEEPALIVE is enabled.
Returns:
a boolean indicating whether or not SO_KEEPALIVE is enabled.
Throws:
SocketException - if there is an error in the underlying protocol, such as a TCP error.
Since:
1.3
See Also:
setKeepAlive(boolean)

close

public void close()
           throws IOException
Closes this socket.
Throws:
IOException - if an I/O error occurs when closing this socket.

shutdownInput

public void shutdownInput()
                   throws IOException
Places the input stream for this socket at "end of stream". Any data sent to the input stream side of the socket is acknowledged and then silently discarded. If you read from a socket input stream after invoking shutdownInput() on the socket, the stream will return EOF.
Throws:
IOException - if an I/O error occurs when shutting down this socket.
See Also:
shutdownOutput(), close(), setSoLinger(boolean, int)

shutdownOutput

public void shutdownOutput()
                    throws IOException
Disables the output stream for this socket. For a TCP socket, any previously written data will be sent followed by TCP's normal connection termination sequence. If you write to a socket output stream after invoking shutdownOutput() on the socket, the stream will throw an IOException.
Throws:
IOException - if an I/O error occurs when shutting down this socket.
See Also:
shutdownInput(), close(), setSoLinger(boolean, int)

toString

public String toString()
Converts this socket to a String.
Overrides:
toString in class Object
Returns:
a string representation of this socket.

setSocketImplFactory

public static void setSocketImplFactory(SocketImplFactory fac)
                                 throws IOException
Sets the client socket implementation factory for the application. The factory can be specified only once.

When an application creates a new client socket, the socket implementation factory's createSocketImpl method is called to create the actual socket implementation.

If there is a security manager, this method first calls the security manager's checkSetFactory method to ensure the operation is allowed. This could result in a SecurityException.

Parameters:
fac - the desired factory.
Throws:
IOException - if an I/O error occurs when setting the socket factory.
SocketException - if the factory is already defined.
SecurityException - if a security manager exists and its checkSetFactory method doesn't allow the operation.
See Also:
SocketImplFactory.createSocketImpl(), SecurityManager.checkSetFactory()

JavaTM 2 Platform
Std. Ed. v1.3.1

Submit a bug or feature
For further API reference and developer documentation, see Java 2 SDK SE Developer Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.

Java, Java 2D, and JDBC are trademarks or registered trademarks of Sun Microsystems, Inc. in the US and other countries.
Copyright 1993-2001 Sun Microsystems, Inc. 901 San Antonio Road
Palo Alto, California, 94303, U.S.A. All Rights Reserved.

free hit counter