FileDocCategorySizeDatePackage
Socket.javaAPI DocAndroid 1.5 API43188Wed May 06 22:41:04 BST 2009java.net

Socket

public class Socket extends Object
Provides a client-side TCP socket.
since
Android 1.0

Fields Summary
SocketImpl
impl
static SocketImplFactory
factory
private volatile boolean
isCreated
private boolean
isBound
private boolean
isConnected
private boolean
isClosed
private boolean
isInputShutdown
private boolean
isOutputShutdown
private Object
connectLock
private Proxy
proxy
static final int
MULTICAST_IF
static final int
MULTICAST_TTL
static final int
TCP_NODELAY
static final int
FLAG_SHUTDOWN
Constructors Summary
public Socket()
Creates a new unconnected socket. When a SocketImplFactory is defined it creates the internal socket implementation, otherwise the default socket implementation will be used for this socket.

see
SocketImplFactory
see
SocketImpl
since
Android 1.0

   
     
        Platform.getNetworkSystem().oneTimeInitialization(true);
    
        impl = factory != null ? factory.createSocketImpl()
                : SocketImplProvider.getSocketImpl();
    
public Socket(Proxy proxy)
Creates a new unconnected socket using the given proxy type. When a {@code SocketImplFactory} is defined it creates the internal socket implementation, otherwise the default socket implementation will be used for this socket.

Example that will create a socket connection through a {@code SOCKS} proxy server:
{@code Socket sock = new Socket(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("test.domain.org", 2130)));}

param
proxy the specified proxy for this socket.
throws
IllegalArgumentException if the argument {@code proxy} is {@code null} or of an invalid type.
throws
SecurityException if a security manager exists and it denies the permission to connect to the given proxy.
see
SocketImplFactory
see
SocketImpl
since
Android 1.0

        if (null == proxy || Proxy.Type.HTTP == proxy.type()) {
            // KA023=Proxy is null or invalid type
            throw new IllegalArgumentException(Msg.getString("KA023")); //$NON-NLS-1$
        }
        InetSocketAddress address = (InetSocketAddress) proxy.address();
        if (null != address) {
            InetAddress addr = address.getAddress();
            String host;
            if (null != addr) {
                host = addr.getHostAddress();
            } else {
                host = address.getHostName();
            }
            int port = address.getPort();
            checkConnectPermission(host, port);
        }
        impl = factory != null ? factory.createSocketImpl()
                : SocketImplProvider.getSocketImpl(proxy);
        this.proxy = proxy;
    
public Socket(String dstName, int dstPort)
Creates a new streaming socket connected to the target host specified by the parameters {@code dstName} and {@code dstPort}. The socket is bound to any available port on the local host.

param
dstName the target host name or IP address to connect to.
param
dstPort the port on the target host to connect to.
throws
UnknownHostException if the host name could not be resolved into an IP address.
throws
IOException if an error occurs while creating the socket.
throws
SecurityException if a security manager exists and it denies the permission to connect to the given address and port.
since
Android 1.0

        this();
        InetAddress dstAddress = InetAddress.getByName(dstName);
        checkDestination(dstAddress, dstPort);
        startupSocket(dstAddress, dstPort, null, 0, true);
    
public Socket(String dstName, int dstPort, InetAddress localAddress, int localPort)
Creates a new streaming socket connected to the target host specified by the parameters {@code dstName} and {@code dstPort}. On the local endpoint the socket is bound to the given address {@code localAddress} on port {@code localPort}. If {@code host} is {@code null} a loopback address is used to connect to.

param
dstName the target host name or IP address to connect to.
param
dstPort the port on the target host to connect to.
param
localAddress the address on the local host to bind to.
param
localPort the port on the local host to bind to.
throws
UnknownHostException if the host name could not be resolved into an IP address.
throws
IOException if an error occurs while creating the socket.
throws
SecurityException if a security manager exists and it denies the permission to connect to the given address and port.
since
Android 1.0

        this();
        InetAddress dstAddress = InetAddress.getByName(dstName);
        checkDestination(dstAddress, dstPort);
        startupSocket(dstAddress, dstPort, localAddress, localPort, true);
    
public Socket(String hostName, int port, boolean streaming)
Creates a new streaming or datagram socket connected to the target host specified by the parameters {@code hostName} and {@code port}. The socket is bound to any available port on the local host.

param
hostName the target host name or IP address to connect to.
param
port the port on the target host to connect to.
param
streaming if {@code true} a streaming socket is returned, a datagram socket otherwise.
throws
UnknownHostException if the host name could not be resolved into an IP address.
throws
IOException if an error occurs while creating the socket.
throws
SecurityException if a security manager exists and it denies the permission to connect to the given address and port.
deprecated
Use {@code Socket(String, int)} instead of this for streaming sockets or an appropriate constructor of {@code DatagramSocket} for UDP transport.
since
Android 1.0

        this();
        InetAddress host = InetAddress.getByName(hostName);
        checkDestination(host, port);
        startupSocket(host, port, null, 0, streaming);
    
public Socket(InetAddress dstAddress, int dstPort)
Creates a new streaming socket connected to the target host specified by the parameters {@code dstAddress} and {@code dstPort}. The socket is bound to any available port on the local host.

param
dstAddress the target host address to connect to.
param
dstPort the port on the target host to connect to.
throws
IOException if an error occurs while creating the socket.
throws
SecurityException if a security manager exists and it denies the permission to connect to the given address and port.
since
Android 1.0

        this();
        checkDestination(dstAddress, dstPort);
        startupSocket(dstAddress, dstPort, null, 0, true);
    
public Socket(InetAddress dstAddress, int dstPort, InetAddress localAddress, int localPort)
Creates a new streaming socket connected to the target host specified by the parameters {@code dstAddress} and {@code dstPort}. On the local endpoint the socket is bound to the given address {@code localAddress} on port {@code localPort}.

param
dstAddress the target host address to connect to.
param
dstPort the port on the target host to connect to.
param
localAddress the address on the local host to bind to.
param
localPort the port on the local host to bind to.
throws
IOException if an error occurs while creating the socket.
throws
SecurityException if a security manager exists and it denies the permission to connect to the given address and port.
since
Android 1.0

        this();
        checkDestination(dstAddress, dstPort);
        startupSocket(dstAddress, dstPort, localAddress, localPort, true);
    
public Socket(InetAddress addr, int port, boolean streaming)
Creates a new streaming or datagram socket connected to the target host specified by the parameters {@code addr} and {@code port}. The socket is bound to any available port on the local host.

param
addr the Internet address to connect to.
param
port the port on the target host to connect to.
param
streaming if {@code true} a streaming socket is returned, a datagram socket otherwise.
throws
IOException if an error occurs while creating the socket.
throws
SecurityException if a security manager exists and it denies the permission to connect to the given address and port.
deprecated
Use {@code Socket(InetAddress, int)} instead of this for streaming sockets or an appropriate constructor of {@code DatagramSocket} for UDP transport.
since
Android 1.0

        this();
        checkDestination(addr, port);
        startupSocket(addr, port, null, 0, streaming);
    
protected Socket(SocketImpl anImpl)
Creates an unconnected socket with the given socket implementation.

param
anImpl the socket implementation to be used.
throws
SocketException if an error occurs while creating the socket.
since
Android 1.0

        impl = anImpl;
    
Methods Summary
voidaccepted()
Set the appropriate flags for a socket created by {@code ServerSocket.accept()}.

see
ServerSocket#implAccept

        isCreated = isBound = isConnected = true;
    
public voidbind(java.net.SocketAddress localAddr)
Binds this socket to the given local host address and port specified by the SocketAddress {@code localAddr}. If {@code localAddr} is set to {@code null}, this socket will be bound to an available local address on any free port.

param
localAddr the specific address and port on the local machine to bind to.
throws
IllegalArgumentException if the given SocketAddress is invalid or not supported.
throws
IOException if the socket is already bound or an error occurs while binding.
since
Android 1.0

        checkClosedAndCreate(true);
        if (isBound()) {
            throw new BindException(Msg.getString("K0315")); //$NON-NLS-1$
        }

        int port = 0;
        InetAddress addr = InetAddress.ANY;
        if (localAddr != null) {
            if (!(localAddr instanceof InetSocketAddress)) {
                throw new IllegalArgumentException(Msg.getString(
                        "K0316", localAddr.getClass())); //$NON-NLS-1$
            }
            InetSocketAddress inetAddr = (InetSocketAddress) localAddr;
            if ((addr = inetAddr.getAddress()) == null) {
                throw new SocketException(Msg.getString(
                        "K0317", inetAddr.getHostName())); //$NON-NLS-1$
            }
            port = inetAddr.getPort();
        }

        synchronized (this) {
            try {
                impl.bind(addr, port);
                isBound = true;
            } catch (IOException e) {
                impl.close();
                throw e;
            }
        }
    
private voidcheckClosedAndCreate(boolean create)
Checks whether the socket is closed, and throws an exception. Otherwise creates the underlying SocketImpl.

throws
SocketException if the socket is closed.

        if (isClosed()) {
            throw new SocketException(Msg.getString("K003d")); //$NON-NLS-1$
        }
        if (!create) {
            if (!isConnected()) {
                throw new SocketException(Msg.getString("K0320")); //$NON-NLS-1$
                // a connected socket must be created
            }

            /*
             * return directly to fix a possible bug, if !create, should return
             * here
             */
            return;
        }
        if (isCreated) {
            return;
        }
        synchronized (this) {
            if (isCreated) {
                return;
            }
            try {
                impl.create(true);
            } catch (SocketException e) {
                throw e;
            } catch (IOException e) {
                throw new SocketException(e.toString());
            }
            isCreated = true;
        }
    
private voidcheckConnectPermission(java.lang.String hostname, int dstPort)
Checks whether the connection destination satisfies the security policy.

param
hostname the destination hostname.
param
dstPort the port on the destination host.

        SecurityManager security = System.getSecurityManager();
        if (security != null) {
            security.checkConnect(hostname, dstPort);
        }
    
voidcheckDestination(java.net.InetAddress destAddr, int dstPort)
Checks whether the connection destination satisfies the security policy and the validity of the port range.

param
destAddr the destination host address.
param
dstPort the port on the destination host.

        if (dstPort < 0 || dstPort > 65535) {
            throw new IllegalArgumentException(Msg.getString("K0032")); //$NON-NLS-1$
        }
        // BEGIN android-changed
        checkConnectPermission(destAddr.getHostAddress(), dstPort);
        // END android-changed
    
public synchronized voidclose()
Closes the socket. It is not possible to reconnect or rebind to this socket thereafter which means a new socket instance has to be created.

throws
IOException if an error occurs while closing the socket.
since
Android 1.0

        isClosed = true;
        impl.close();
    
public voidconnect(java.net.SocketAddress remoteAddr)
Connects this socket to the given remote host address and port specified by the SocketAddress {@code remoteAddr}.

param
remoteAddr the address and port of the remote host to connect to.
throws
IllegalArgumentException if the given SocketAddress is invalid or not supported.
throws
IOException if the socket is already connected or an error occurs while connecting.
since
Android 1.0

        connect(remoteAddr, 0);
    
public voidconnect(java.net.SocketAddress remoteAddr, int timeout)
Connects this socket to the given remote host address and port specified by the SocketAddress {@code remoteAddr} with the specified timeout. The connecting method will block until the connection is established or an error occurred.

param
remoteAddr the address and port of the remote host to connect to.
param
timeout the timeout value in milliseconds or {@code 0} for an infinite timeout.
throws
IllegalArgumentException if the given SocketAddress is invalid or not supported or the timeout value is negative.
throws
IOException if the socket is already connected or an error occurs while connecting.
since
Android 1.0

        checkClosedAndCreate(true);
        if (timeout < 0) {
            throw new IllegalArgumentException(Msg.getString("K0036")); //$NON-NLS-1$
        }
        if (isConnected()) {
            throw new SocketException(Msg.getString("K0079")); //$NON-NLS-1$
        }
        if (remoteAddr == null) {
            throw new IllegalArgumentException(Msg.getString("K0318")); //$NON-NLS-1$
        }

        if (!(remoteAddr instanceof InetSocketAddress)) {
            throw new IllegalArgumentException(Msg.getString(
                    "K0316", remoteAddr.getClass())); //$NON-NLS-1$
        }
        InetSocketAddress inetAddr = (InetSocketAddress) remoteAddr;
        InetAddress addr;
        if ((addr = inetAddr.getAddress()) == null) {
            throw new UnknownHostException(Msg.getString("K0317", remoteAddr));//$NON-NLS-1$
        }
        int port = inetAddr.getPort();

        checkDestination(addr, port);
        synchronized (connectLock) {
            try {
                if (!isBound()) {
                    // socket allready created at this point by earlier call or
                    // checkClosedAndCreate this caused us to lose socket
                    // options on create
                    // impl.create(true);
                    if (!NetUtil.usingSocks(proxy)) {
                        impl.bind(InetAddress.ANY, 0);
                    }
                    isBound = true;
                }
                impl.connect(remoteAddr, timeout);
                isConnected = true;
            } catch (IOException e) {
                impl.close();
                throw e;
            }
        }
    
public java.nio.channels.SocketChannelgetChannel()
Gets the SocketChannel of this socket, if one is available. The current implementation of this method returns always {@code null}.

return
the related SocketChannel or {@code null} if no channel exists.
since
Android 1.0

        return null;
    
public java.net.InetAddressgetInetAddress()
Gets the IP address of the target host this socket is connected to.

return
the IP address of the connected target host or {@code null} if this socket is not yet connected.
since
Android 1.0

        if (!isConnected()) {
            return null;
        }
        return impl.getInetAddress();
    
public java.io.InputStreamgetInputStream()
Gets an input stream to read data from this socket.

return
the byte-oriented input stream.
throws
IOException if an error occurs while creating the input stream or the socket is in an invalid state.
since
Android 1.0

        checkClosedAndCreate(false);
        if (isInputShutdown()) {
            throw new SocketException(Msg.getString("K0321")); //$NON-NLS-1$
        }
        return impl.getInputStream();
    
public booleangetKeepAlive()
Gets the setting of the socket option {@code SocketOptions.SO_KEEPALIVE}.

return
{@code true} if the {@code SocketOptions.SO_KEEPALIVE} is enabled, {@code false} otherwise.
throws
SocketException if an error occurs while reading the socket option.
see
SocketOptions#SO_KEEPALIVE
since
Android 1.0

        checkClosedAndCreate(true);
        return ((Boolean) impl.getOption(SocketOptions.SO_KEEPALIVE))
                .booleanValue();
    
public java.net.InetAddressgetLocalAddress()
Gets the local IP address this socket is bound to.

return
the local IP address of this socket or {@code InetAddress.ANY} if the socket is unbound.
since
Android 1.0

        if (!isBound()) {
            return InetAddress.ANY;
        }
        return Platform.getNetworkSystem().getSocketLocalAddress(impl.fd,
                InetAddress.preferIPv6Addresses());
    
public intgetLocalPort()
Gets the local port this socket is bound to.

return
the local port of this socket or {@code -1} if the socket is unbound.
since
Android 1.0

        if (!isBound()) {
            return -1;
        }
        return impl.getLocalPort();
    
public java.net.SocketAddressgetLocalSocketAddress()
Gets the local address and port of this socket as a SocketAddress or {@code null} if the socket is unbound. This is useful on multihomed hosts.

return
the bound local socket address and port.
since
Android 1.0

        if (!isBound()) {
            return null;
        }
        return new InetSocketAddress(getLocalAddress(), getLocalPort());
    
public booleangetOOBInline()
Gets the setting of the socket option {@code SocketOptions.SO_OOBINLINE}.

return
{@code true} if the {@code SocketOptions.SO_OOBINLINE} is enabled, {@code false} otherwise.
throws
SocketException if an error occurs while reading the socket option.
see
SocketOptions#SO_OOBINLINE
since
Android 1.0

        checkClosedAndCreate(true);
        return ((Boolean) impl.getOption(SocketOptions.SO_OOBINLINE))
                .booleanValue();
    
public java.io.OutputStreamgetOutputStream()
Gets an output stream to write data into this socket.

return
the byte-oriented output stream.
throws
IOException if an error occurs while creating the output stream or the socket is in an invalid state.
since
Android 1.0

        checkClosedAndCreate(false);
        if (isOutputShutdown()) {
            throw new SocketException(Msg.getString("KA00f")); //$NON-NLS-1$
        }
        return impl.getOutputStream();
    
public intgetPort()
Gets the port number of the target host this socket is connected to.

return
the port number of the connected target host or {@code 0} if this socket is not yet connected.
since
Android 1.0

        if (!isConnected()) {
            return 0;
        }
        return impl.getPort();
    
public synchronized intgetReceiveBufferSize()
Gets the receive buffer size of this socket.

return
the current value of the option {@code SocketOptions.SO_RCVBUF}.
throws
SocketException if an error occurs while reading the socket option.
see
SocketOptions#SO_RCVBUF
since
Android 1.0

        checkClosedAndCreate(true);
        return ((Integer) impl.getOption(SocketOptions.SO_RCVBUF)).intValue();
    
public java.net.SocketAddressgetRemoteSocketAddress()
Gets the remote address and port of this socket as a {@code SocketAddress} or {@code null} if the socket is not connected.

return
the remote socket address and port.
since
Android 1.0

        if (!isConnected()) {
            return null;
        }
        return new InetSocketAddress(getInetAddress(), getPort());
    
public booleangetReuseAddress()
Gets the setting of the socket option {@code SocketOptions.SO_REUSEADDR}.

return
{@code true} if the {@code SocketOptions.SO_REUSEADDR} is enabled, {@code false} otherwise.
throws
SocketException if an error occurs while reading the socket option.
see
SocketOptions#SO_REUSEADDR
since
Android 1.0

        checkClosedAndCreate(true);
        return ((Boolean) impl.getOption(SocketOptions.SO_REUSEADDR))
                .booleanValue();
    
public synchronized intgetSendBufferSize()
Gets the send buffer size of this socket.

return
the current value of the option {@code SocketOptions.SO_SNDBUF}.
throws
SocketException if an error occurs while reading the socket option.
see
SocketOptions#SO_SNDBUF
since
Android 1.0

        checkClosedAndCreate(true);
        return ((Integer) impl.getOption(SocketOptions.SO_SNDBUF)).intValue();
    
public intgetSoLinger()
Gets the value of the socket option {@code SocketOptions.SO_LINGER}.

return
the current value of the option {@code SocketOptions.SO_LINGER} or {@code -1} if this option is disabled.
throws
SocketException if an error occurs while reading the socket option.
see
SocketOptions#SO_LINGER
since
Android 1.0

        checkClosedAndCreate(true);
        return ((Integer) impl.getOption(SocketOptions.SO_LINGER)).intValue();
    
public synchronized intgetSoTimeout()
Gets the timeout for this socket during which a reading operation shall block while waiting for data.

return
the current value of the option {@code SocketOptions.SO_TIMEOUT} or {@code 0} which represents an infinite timeout.
throws
SocketException if an error occurs while reading the socket option.
see
SocketOptions#SO_TIMEOUT
since
Android 1.0

        checkClosedAndCreate(true);
        return ((Integer) impl.getOption(SocketOptions.SO_TIMEOUT)).intValue();
    
public booleangetTcpNoDelay()
Gets the setting of the socket option {@code SocketOptions.TCP_NODELAY}.

return
{@code true} if the {@code SocketOptions.TCP_NODELAY} is enabled, {@code false} otherwise.
throws
SocketException if an error occurs while reading the socket option.
see
SocketOptions#TCP_NODELAY
since
Android 1.0

        checkClosedAndCreate(true);
        return ((Boolean) impl.getOption(SocketOptions.TCP_NODELAY))
                .booleanValue();
    
public intgetTrafficClass()
Gets the value of the socket option {@code SocketOptions.IP_TOS}.

return
the value which represents the type of service.
throws
SocketException if an error occurs while reading the socket option.
see
SocketOptions#IP_TOS
since
Android 1.0

        checkClosedAndCreate(true);
        return ((Number) impl.getOption(SocketOptions.IP_TOS)).intValue();
    
public booleanisBound()
Returns whether this socket is bound to a local address and port.

return
{@code true} if the socket is bound to a local address, {@code false} otherwise.
since
Android 1.0

        return isBound;
    
public booleanisClosed()
Returns whether this socket is closed.

return
{@code true} if the socket is closed, {@code false} otherwise.
since
Android 1.0

        return isClosed;
    
public booleanisConnected()
Returns whether this socket is connected to a remote host.

return
{@code true} if the socket is connected, {@code false} otherwise.
since
Android 1.0

        return isConnected;
    
public booleanisInputShutdown()
Returns whether the incoming channel of the socket has already been closed.

return
{@code true} if reading from this socket is not possible anymore, {@code false} otherwise.
since
Android 1.0

        return isInputShutdown;
    
public booleanisOutputShutdown()
Returns whether the outgoing channel of the socket has already been closed.

return
{@code true} if writing to this socket is not possible anymore, {@code false} otherwise.
since
Android 1.0

        return isOutputShutdown;
    
static booleanpreferIPv4Stack()

        String result = AccessController.doPrivileged(new PriviAction<String>(
                "java.net.preferIPv4Stack")); //$NON-NLS-1$
        return "true".equals(result); //$NON-NLS-1$
    
public voidsendUrgentData(int value)
Sends the given single byte data which is represented by the lowest octet of {@code value} as "TCP urgent data".

param
value the byte of urgent data to be sent.
throws
IOException if an error occurs while sending urgent data.
since
Android 1.0

        if (!impl.supportsUrgentData()) {
            throw new SocketException(Msg.getString("K0333")); //$NON-NLS-1$
        }
        impl.sendUrgentData(value);
    
public voidsetKeepAlive(boolean value)
Sets the state of the {@code SocketOptions.SO_KEEPALIVE} for this socket.

param
value the state whether this option is enabled or not.
throws
SocketException if an error occurs while setting the option.
see
SocketOptions#SO_KEEPALIVE
since
Android 1.0

        if (impl != null) {
            checkClosedAndCreate(true);
            impl.setOption(SocketOptions.SO_KEEPALIVE, value ? Boolean.TRUE
                    : Boolean.FALSE);
        }
    
public voidsetOOBInline(boolean oobinline)
Sets the state of the {@code SocketOptions.SO_OOBINLINE} for this socket. When this option is enabled urgent data can be received in-line with normal data.

param
oobinline whether this option is enabled or not.
throws
SocketException if an error occurs while setting the option.
see
SocketOptions#SO_OOBINLINE
since
Android 1.0

        checkClosedAndCreate(true);
        impl.setOption(SocketOptions.SO_OOBINLINE, oobinline ? Boolean.TRUE
                : Boolean.FALSE);
    
public voidsetPerformancePreferences(int connectionTime, int latency, int bandwidth)
Sets performance preferences for connectionTime, latency and bandwidth.

This method does currently nothing.

param
connectionTime the value representing the importance of a short connecting time.
param
latency the value representing the importance of low latency.
param
bandwidth the value representing the importance of high bandwidth.
since
Android 1.0

        // Our socket implementation only provide one protocol: TCP/IP, so
        // we do nothing for this method
    
public synchronized voidsetReceiveBufferSize(int size)
Sets the receive buffer size of this socket.

param
size the buffer size in bytes. This value must be a positive number greater than {@code 0}.
throws
SocketException if an error occurs while setting the size or the given value is an invalid size.
see
SocketOptions#SO_RCVBUF
since
Android 1.0

        checkClosedAndCreate(true);
        if (size < 1) {
            throw new IllegalArgumentException(Msg.getString("K0035")); //$NON-NLS-1$
        }
        impl.setOption(SocketOptions.SO_RCVBUF, Integer.valueOf(size));
    
public voidsetReuseAddress(boolean reuse)
Sets the state of the {@code SocketOptions.SO_REUSEADDR} for this socket.

param
reuse the state whether this option is enabled or not.
throws
SocketException if an error occurs while setting the option.
see
SocketOptions#SO_REUSEADDR
since
Android 1.0

        checkClosedAndCreate(true);
        impl.setOption(SocketOptions.SO_REUSEADDR, reuse ? Boolean.TRUE
                : Boolean.FALSE);
    
public synchronized voidsetSendBufferSize(int size)
Sets the send buffer size of this socket.

param
size the buffer size in bytes. This value must be a positive number greater than {@code 0}.
throws
SocketException if an error occurs while setting the size or the given value is an invalid size.
see
SocketOptions#SO_SNDBUF
since
Android 1.0

        checkClosedAndCreate(true);
        if (size < 1) {
            throw new IllegalArgumentException(Msg.getString("K0035")); //$NON-NLS-1$
        }
        impl.setOption(SocketOptions.SO_SNDBUF, Integer.valueOf(size));
    
public voidsetSoLinger(boolean on, int timeout)
Sets the state of the {@code SocketOptions.SO_LINGER} with the given timeout in seconds. The timeout value for this option is silently limited to the maximum of {@code 65535}.

param
on the state whether this option is enabled or not.
param
timeout the linger timeout value in seconds.
throws
SocketException if an error occurs while setting the option.
see
SocketOptions#SO_LINGER
since
Android 1.0

        checkClosedAndCreate(true);
        if (on && timeout < 0) {
            throw new IllegalArgumentException(Msg.getString("K0045")); //$NON-NLS-1$
        }
        // BEGIN android-changed
        /*
         * The spec indicates that the right way to turn off an option
         * is to pass Boolean.FALSE, so that's what we do here.
         */
        if (on) {
            if (timeout > 65535) {
                timeout = 65535;
            }
            impl.setOption(SocketOptions.SO_LINGER, Integer.valueOf(timeout));
        } else {
            impl.setOption(SocketOptions.SO_LINGER, Boolean.FALSE);
        }
        // END android-changed
    
public synchronized voidsetSoTimeout(int timeout)
Sets the reading timeout in milliseconds for this socket. The read operation will block indefinitely if this option value is set to {@code 0}. The timeout must be set before calling the read operation. A {@code SocketTimeoutException} is thrown when this timeout expires.

param
timeout the reading timeout value as number greater than {@code 0} or {@code 0} for an infinite timeout.
throws
SocketException if an error occurs while setting the option.
see
SocketOptions#SO_TIMEOUT
since
Android 1.0

        checkClosedAndCreate(true);
        if (timeout < 0) {
            throw new IllegalArgumentException(Msg.getString("K0036")); //$NON-NLS-1$
        }
        impl.setOption(SocketOptions.SO_TIMEOUT, Integer.valueOf(timeout));
    
public static synchronized voidsetSocketImplFactory(java.net.SocketImplFactory fac)
Sets the internal factory for creating socket implementations. This may only be executed once during the lifetime of the application.

param
fac the socket implementation factory to be set.
throws
IOException if the factory has been already set.
since
Android 1.0

        SecurityManager security = System.getSecurityManager();
        if (security != null) {
            security.checkSetFactory();
        }
        if (factory != null) {
            throw new SocketException(Msg.getString("K0044")); //$NON-NLS-1$
        }
        factory = fac;
    
public voidsetTcpNoDelay(boolean on)
Sets the state of the {@code SocketOptions.TCP_NODELAY} for this socket.

param
on the state whether this option is enabled or not.
throws
SocketException if an error occurs while setting the option.
see
SocketOptions#TCP_NODELAY
since
Android 1.0

        checkClosedAndCreate(true);
        impl.setOption(SocketOptions.TCP_NODELAY, Boolean.valueOf(on));
    
public voidsetTrafficClass(int value)
Sets the value of the {@code SocketOptions.IP_TOS} for this socket. See the specification RFC 1349 for more information about the type of service field.

param
value the value to be set for this option with a valid range of {@code 0-255}.
throws
SocketException if an error occurs while setting the option.
see
SocketOptions#IP_TOS
since
Android 1.0

        checkClosedAndCreate(true);
        if (value < 0 || value > 255) {
            throw new IllegalArgumentException();
        }
        impl.setOption(SocketOptions.IP_TOS, Integer.valueOf(value));
    
public voidshutdownInput()
Closes the input stream of this socket. Any further data sent to this socket will be discarded. Reading from this socket after this method has been called will return the value {@code EOF}.

throws
IOException if an error occurs while closing the socket input stream.
throws
SocketException if the input stream is already closed.
since
Android 1.0

        if (isInputShutdown()) {
            throw new SocketException(Msg.getString("K0321")); //$NON-NLS-1$
        }
        checkClosedAndCreate(false);
        impl.shutdownInput();
        isInputShutdown = true;
    
public voidshutdownOutput()
Closes the output stream of this socket. All buffered data will be sent followed by the termination sequence. Writing to the closed output stream will cause an {@code IOException}.

throws
IOException if an error occurs while closing the socket output stream.
throws
SocketException if the output stream is already closed.
since
Android 1.0

        if (isOutputShutdown()) {
            throw new SocketException(Msg.getString("KA00f")); //$NON-NLS-1$
        }
        checkClosedAndCreate(false);
        impl.shutdownOutput();
        isOutputShutdown = true;
    
voidstartupSocket(java.net.InetAddress dstAddress, int dstPort, java.net.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.

param
dstAddress the destination host address.
param
dstPort the port on the destination host.
param
localAddress the address on the local machine to bind.
param
localPort the port on the local machine to bind.
throws
IOException thrown if an error occurs during the bind or connect operations.


        if (localPort < 0 || localPort > 65535) {
            throw new IllegalArgumentException(Msg.getString("K0046")); //$NON-NLS-1$
        }

        InetAddress addr = localAddress == null ? InetAddress.ANY
                : localAddress;
        synchronized (this) {
            impl.create(streaming);
            isCreated = true;
            try {
                if (!streaming || !NetUtil.usingSocks(proxy)) {
                    impl.bind(addr, localPort);
                }
                isBound = true;
                impl.connect(dstAddress, dstPort);
                isConnected = true;
            } catch (IOException e) {
                impl.close();
                throw e;
            }
        }
    
public java.lang.StringtoString()
Returns a {@code String} containing a concise, human-readable description of the socket.

return
the textual representation of this socket.
since
Android 1.0

        if (!isConnected()) {
            return "Socket[unconnected]"; //$NON-NLS-1$
        }
        return impl.toString();