Android
java.net
public class

java.net.Socket

java.lang.Object
java.net.Socket

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

Known Direct Subclasses

Summary

Public Constructors

            Socket()
Construct a connection-oriented Socket.
            Socket(Proxy proxy)
Constructs a connection-oriented Socket with specified proxy.
            Socket(String dstName, int dstPort)
Construct a stream socket connected to the nominated destination host/port.
            Socket(String dstName, int dstPort, InetAddress localAddress, int localPort)
Construct a stream socket connected to the nominated destination host/port.
            Socket(String hostName, int port, boolean streaming)
This constructor is deprecated. As of JDK 1.1, replaced by Socket
            Socket(InetAddress dstAddress, int dstPort)
Construct a stream socket connected to the nominated destination host address/port.
            Socket(InetAddress dstAddress, int dstPort, InetAddress localAddress, int localPort)
Construct a stream socket connected to the nominated destination host address/port.
            Socket(InetAddress addr, int port, boolean streaming)
This constructor is deprecated. As of JDK 1.1, replaced by Socket

Protected Constructors

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

Public Methods

          void  bind(SocketAddress localAddr)
Bind the Socket to the nominated local host/port.
  synchronized        void  close()
Close the socket.
          void  connect(SocketAddress remoteAddr, int timeout)
Connect the Socket to the host/port specified by the SocketAddress with a specified timeout.
          void  connect(SocketAddress remoteAddr)
Connect the Socket to the host/port specified by the SocketAddress.
          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.
  synchronized        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.
  synchronized        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).
  synchronized        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.
          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
  synchronized        void  setReceiveBufferSize(int size)
Set the socket receive buffer size.
          void  setReuseAddress(boolean reuse)
Set the SO_REUSEADDR socket option.
  synchronized        void  setSendBufferSize(int size)
Set the socket send buffer size.
          void  setSoLinger(boolean on, int timeout)
Set the SO_LINGER option, with the specified time, in seconds.
  synchronized        void  setSoTimeout(int timeout)
Set the read timeout on this socket.
  synchronized    static    void  setSocketImplFactory(SocketImplFactory fac)
Specifies the application's socket implementation factory.
          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.
          String  toString()
Returns a string containing a concise, human-readable description of the socket.
Methods inherited from class java.lang.Object

Details

Public Constructors

public Socket()

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

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.

public Socket(String dstName, int dstPort)

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

public Socket(String dstName, int dstPort, InetAddress localAddress, int localPort)

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

public Socket(String hostName, int port, boolean streaming)

This constructor is 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

public Socket(InetAddress dstAddress, int dstPort)

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

public Socket(InetAddress dstAddress, int dstPort, InetAddress localAddress, int localPort)

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

public Socket(InetAddress addr, int port, boolean streaming)

This constructor is 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

Protected Constructors

protected Socket(SocketImpl anImpl)

Creates an unconnected socket, wrapping the socketImpl argument.

Parameters

anImpl the socket to wrap

Throws

SocketException if an error occurs assigning the implementation

Public Methods

public void bind(SocketAddress localAddr)

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

public synchronized void close()

Close the socket. It is not valid to use the socket thereafter.

Throws

IOException if an error occurs during the close

public void connect(SocketAddress remoteAddr, int timeout)

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

public void connect(SocketAddress remoteAddr)

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

public SocketChannel getChannel()

if Socket is created by a SocketChannel, returns the related SocketChannel

Returns

  • the related SocketChannel

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.

public InputStream getInputStream()

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

public boolean getKeepAlive()

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

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

public int getLocalPort()

Answer the local port to which the socket is bound.

Returns

  • the local port to which the socket is bound

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

public boolean getOOBInline()

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.

public OutputStream getOutputStream()

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

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.

public synchronized int getReceiveBufferSize()

Answer the socket receive buffer size (SO_RCVBUF).

Returns

  • socket receive buffer size

Throws

SocketException if an error occurs on the option access

public SocketAddress getRemoteSocketAddress()

Answer the remote SocketAddress for this socket, or null if the socket is not connected.

Returns

  • the remote socket address

public boolean getReuseAddress()

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.

public synchronized int getSendBufferSize()

Answer the socket send buffer size (SO_SNDBUF).

Returns

  • socket send buffer size

Throws

SocketException if an error occurs on the option access

public int getSoLinger()

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

public synchronized int getSoTimeout()

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

public boolean getTcpNoDelay()

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

public int getTrafficClass()

Get the IP_TOS socket option.

Returns

  • the IP_TOS socket option value

Throws

SocketException if the option is invalid

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.

public boolean isClosed()

Return if the socket is closed.

Returns

  • true if the socket is closed, false otherwise.

public boolean isConnected()

Return if the socket is connected.

Returns

  • true if the socket is connected, false otherwise.

public boolean isInputShutdown()

Return if shutdownInput() has been called.

Returns

  • true if shutdownInput has been called, false otherwise.

public boolean isOutputShutdown()

Return if shutdownOutput() has been called.

Returns

  • true if shutdownOutput has been called, false otherwise.

public void sendUrgentData(int value)

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

public void setKeepAlive(boolean value)

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

public void setOOBInline(boolean oobinline)

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.

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

public synchronized void setReceiveBufferSize(int size)

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.

public void setReuseAddress(boolean reuse)

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.

public synchronized void setSendBufferSize(int size)

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.

public void setSoLinger(boolean on, int timeout)

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

public synchronized void setSoTimeout(int timeout)

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

public static synchronized void setSocketImplFactory(SocketImplFactory fac)

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

public void setTcpNoDelay(boolean on)

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

public void setTrafficClass(int value)

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.

public void shutdownInput()

Shutdown the input portion of the socket.

Throws

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

public void shutdownOutput()

Shutdown the output portion of the socket.

Throws

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

public String toString()

Returns a string containing a concise, human-readable description of the socket.

Returns

  • the description
Copyright 2007 Google Inc. Build 0.9_r1-98467 - 14 Aug 2008 18:48