Save This Page
Home » apache-harmony-6.0-src-r917296-snapshot » java » net » [javadoc | source]
java.net
public class: Socket [javadoc | source]
java.lang.Object
   java.net.Socket

Direct Known Subclasses:
    SSLSocket

Provides a client-side TCP socket.
Field Summary
 SocketImpl impl     
static  SocketImplFactory factory     
static final  int MULTICAST_IF     
static final  int MULTICAST_TTL     
static final  int TCP_NODELAY     
static final  int FLAG_SHUTDOWN     
Constructor:
 public Socket() 
 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)));}

    Parameters:
    proxy - the specified proxy for this socket.
    Throws:
    IllegalArgumentException - if the argument {@code proxy} is {@code null} or of an invalid type.
    SecurityException - if a security manager exists and it denies the permission to connect to the given proxy.
    Also see:
    SocketImplFactory
    SocketImpl
 protected Socket(SocketImpl anImpl) throws SocketException 
    Creates an unconnected socket with the given socket implementation.
    Parameters:
    anImpl - the socket implementation to be used.
    Throws:
    SocketException - if an error occurs while creating the socket.
 public Socket(String dstName,
    int dstPort) throws UnknownHostException, IOException 
    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.
    Parameters:
    dstName - the target host name or IP address to connect to.
    dstPort - the port on the target host to connect to.
    Throws:
    UnknownHostException - if the host name could not be resolved into an IP address.
    IOException - if an error occurs while creating the socket.
    SecurityException - if a security manager exists and it denies the permission to connect to the given address and port.
 public Socket(InetAddress dstAddress,
    int dstPort) throws IOException 
    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.
    Parameters:
    dstAddress - the target host address to connect to.
    dstPort - the port on the target host to connect to.
    Throws:
    IOException - if an error occurs while creating the socket.
    SecurityException - if a security manager exists and it denies the permission to connect to the given address and port.
 public Socket(String hostName,
    int port,
    boolean streaming) throws IOException 
    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.
    Parameters:
    hostName - the target host name or IP address to connect to.
    port - the port on the target host to connect to.
    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.
    IOException - if an error occurs while creating the socket.
    SecurityException - if a security manager exists and it denies the permission to connect to the given address and port.
 public Socket(InetAddress addr,
    int port,
    boolean streaming) throws IOException 
    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.
    Parameters:
    addr - the Internet address to connect to.
    port - the port on the target host to connect to.
    streaming - if {@code true} a streaming socket is returned, a datagram socket otherwise.
    Throws:
    IOException - if an error occurs while creating the socket.
    SecurityException - if a security manager exists and it denies the permission to connect to the given address and port.
 public Socket(String dstName,
    int dstPort,
    InetAddress localAddress,
    int localPort) throws IOException 
    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.
    Parameters:
    dstName - the target host name or IP address to connect to.
    dstPort - the port on the target host to connect to.
    localAddress - the address on the local host to bind to.
    localPort - the port on the local host to bind to.
    Throws:
    UnknownHostException - if the host name could not be resolved into an IP address.
    IOException - if an error occurs while creating the socket.
    SecurityException - if a security manager exists and it denies the permission to connect to the given address and port.
 public Socket(InetAddress dstAddress,
    int dstPort,
    InetAddress localAddress,
    int localPort) throws IOException 
    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}.
    Parameters:
    dstAddress - the target host address to connect to.
    dstPort - the port on the target host to connect to.
    localAddress - the address on the local host to bind to.
    localPort - the port on the local host to bind to.
    Throws:
    IOException - if an error occurs while creating the socket.
    SecurityException - if a security manager exists and it denies the permission to connect to the given address and port.
Method from java.net.Socket Summary:
accepted,   bind,   checkDestination,   close,   connect,   connect,   getChannel,   getInetAddress,   getInputStream,   getKeepAlive,   getLocalAddress,   getLocalPort,   getLocalSocketAddress,   getOOBInline,   getOutputStream,   getPort,   getReceiveBufferSize,   getRemoteSocketAddress,   getReuseAddress,   getSendBufferSize,   getSoLinger,   getSoTimeout,   getTcpNoDelay,   getTrafficClass,   isBound,   isClosed,   isConnected,   isInputShutdown,   isOutputShutdown,   preferIPv4Stack,   sendUrgentData,   setKeepAlive,   setOOBInline,   setPerformancePreferences,   setReceiveBufferSize,   setReuseAddress,   setSendBufferSize,   setSoLinger,   setSoTimeout,   setSocketImplFactory,   setTcpNoDelay,   setTrafficClass,   shutdownInput,   shutdownOutput,   startupSocket,   toString
Methods from java.lang.Object:
clone,   equals,   finalize,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from java.net.Socket Detail:
  void accepted() 
    Set the appropriate flags for a socket created by {@code ServerSocket.accept()}.
 public  void bind(SocketAddress localAddr) throws IOException 
    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.
  void checkDestination(InetAddress destAddr,
    int dstPort) 
    Checks whether the connection destination satisfies the security policy and the validity of the port range.
 public synchronized  void close() throws IOException 
    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.
 public  void connect(SocketAddress remoteAddr) throws IOException 
    Connects this socket to the given remote host address and port specified by the SocketAddress {@code remoteAddr}.
 public  void connect(SocketAddress remoteAddr,
    int timeout) throws IOException 
    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.
 public SocketChannel getChannel() 
    Gets the SocketChannel of this socket, if one is available. The current implementation of this method returns always {@code null}.
 public InetAddress getInetAddress() 
    Gets the IP address of the target host this socket is connected to.
 public InputStream getInputStream() throws IOException 
    Gets an input stream to read data from this socket.
 public boolean getKeepAlive() throws SocketException 
    Gets the setting of the socket option {@code SocketOptions.SO_KEEPALIVE}.
 public InetAddress getLocalAddress() 
    Gets the local IP address this socket is bound to.
 public int getLocalPort() 
    Gets the local port this socket is bound to.
 public SocketAddress getLocalSocketAddress() 
    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.
 public boolean getOOBInline() throws SocketException 
    Gets the setting of the socket option {@code SocketOptions.SO_OOBINLINE}.
 public OutputStream getOutputStream() throws IOException 
    Gets an output stream to write data into this socket.
 public int getPort() 
    Gets the port number of the target host this socket is connected to.
 public synchronized int getReceiveBufferSize() throws SocketException 
    Gets the receive buffer size of this socket.
 public SocketAddress getRemoteSocketAddress() 
    Gets the remote address and port of this socket as a {@code SocketAddress} or {@code null} if the socket is not connected.
 public boolean getReuseAddress() throws SocketException 
    Gets the setting of the socket option {@code SocketOptions.SO_REUSEADDR}.
 public synchronized int getSendBufferSize() throws SocketException 
    Gets the send buffer size of this socket.
 public int getSoLinger() throws SocketException 
    Gets the value of the socket option {@code SocketOptions.SO_LINGER}.
 public synchronized int getSoTimeout() throws SocketException 
    Gets the timeout for this socket during which a reading operation shall block while waiting for data.
 public boolean getTcpNoDelay() throws SocketException 
    Gets the setting of the socket option {@code SocketOptions.TCP_NODELAY}.
 public int getTrafficClass() throws SocketException 
    Gets the value of the socket option {@code SocketOptions.IP_TOS}.
 public boolean isBound() 
    Returns whether this socket is bound to a local address and port.
 public boolean isClosed() 
    Returns whether this socket is closed.
 public boolean isConnected() 
    Returns whether this socket is connected to a remote host.
 public boolean isInputShutdown() 
    Returns whether the incoming channel of the socket has already been closed.
 public boolean isOutputShutdown() 
    Returns whether the outgoing channel of the socket has already been closed.
 static boolean preferIPv4Stack() 
 public  void sendUrgentData(int value) throws IOException 
    Sends the given single byte data which is represented by the lowest octet of {@code value} as "TCP urgent data".
 public  void setKeepAlive(boolean value) throws SocketException 
    Sets the state of the {@code SocketOptions.SO_KEEPALIVE} for this socket.
 public  void setOOBInline(boolean oobinline) throws SocketException 
    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.
 public  void setPerformancePreferences(int connectionTime,
    int latency,
    int bandwidth) 
    Sets performance preferences for connectionTime, latency and bandwidth.

    This method does currently nothing.

 public synchronized  void setReceiveBufferSize(int size) throws SocketException 
    Sets the receive buffer size of this socket.
 public  void setReuseAddress(boolean reuse) throws SocketException 
    Sets the state of the {@code SocketOptions.SO_REUSEADDR} for this socket.
 public synchronized  void setSendBufferSize(int size) throws SocketException 
    Sets the send buffer size of this socket.
 public  void setSoLinger(boolean on,
    int timeout) throws SocketException 
    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}.
 public synchronized  void setSoTimeout(int timeout) throws SocketException 
    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.
 public static synchronized  void setSocketImplFactory(SocketImplFactory fac) throws IOException 
    Sets the internal factory for creating socket implementations. This may only be executed once during the lifetime of the application.
 public  void setTcpNoDelay(boolean on) throws SocketException 
    Sets the state of the {@code SocketOptions.TCP_NODELAY} for this socket.
 public  void setTrafficClass(int value) throws SocketException 
    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.
 public  void shutdownInput() throws IOException 
    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}.
 public  void shutdownOutput() throws IOException 
    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}.
  void startupSocket(InetAddress dstAddress,
    int dstPort,
    InetAddress localAddress,
    int localPort,
    boolean streaming) throws IOException 
    Creates a stream socket, binds it to the nominated local address/port, then connects it to the nominated destination address/port.
 public String toString() 
    Returns a {@code String} containing a concise, human-readable description of the socket.