Package java.net

Class Socket

java.lang.Object
java.net.Socket
All Implemented Interfaces:
Closeable, AutoCloseable
Direct Known Subclasses:
SSLSocket

public class Socket
extends Object
implements Closeable
Provides a client-side TCP socket.
  • Constructor Details

    • Socket

      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 Also:
      SocketImplFactory, SocketImpl
    • Socket

      public Socket​(Proxy proxy)
      Creates a new unconnected socket using the given proxy type. When a 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 SOCKS proxy server:
      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 proxy is null or of an invalid type.
      See Also:
      SocketImplFactory, SocketImpl
    • Socket

      public Socket​(String dstName, int dstPort) throws UnknownHostException, IOException
      Creates a new streaming socket connected to the target host specified by the parameters dstName and dstPort. The socket is bound to any available port on the local host.

      This implementation tries each IP address for the given hostname (in RFC 3484 order) until it either connects successfully or it exhausts the set.

      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.
    • Socket

      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 dstName and dstPort. On the local endpoint the socket is bound to the given address localAddress on port localPort. If host is null a loopback address is used to connect to.

      This implementation tries each IP address for the given hostname (in RFC 3484 order) until it either connects successfully or it exhausts the set.

      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.
    • Socket

      @Deprecated public Socket​(String hostName, int port, boolean streaming) throws IOException
      Deprecated.
      Use Socket(String, int) instead of this for streaming sockets or an appropriate constructor of DatagramSocket for UDP transport.
      Creates a new streaming or datagram socket connected to the target host specified by the parameters hostName and port. The socket is bound to any available port on the local host.

      This implementation tries each IP address for the given hostname (in RFC 3484 order) until it either connects successfully or it exhausts the set.

      Parameters:
      hostName - the target host name or IP address to connect to.
      port - the port on the target host to connect to.
      streaming - if 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.
    • Socket

      public Socket​(InetAddress dstAddress, int dstPort) throws IOException
      Creates a new streaming socket connected to the target host specified by the parameters dstAddress and 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.
    • Socket

      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 dstAddress and dstPort. On the local endpoint the socket is bound to the given address localAddress on port 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.
    • Socket

      @Deprecated public Socket​(InetAddress addr, int port, boolean streaming) throws IOException
      Deprecated.
      Use Socket(InetAddress, int) instead of this for streaming sockets or an appropriate constructor of DatagramSocket for UDP transport.
      Creates a new streaming or datagram socket connected to the target host specified by the parameters addr and 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 true a streaming socket is returned, a datagram socket otherwise.
      Throws:
      IOException - if an error occurs while creating the socket.
    • Socket

      protected Socket​(SocketImpl impl) throws SocketException
      Creates an unconnected socket with the given socket implementation.
      Parameters:
      impl - the socket implementation to be used.
      Throws:
      SocketException - if an error occurs while creating the socket.
  • Method Details