< prev index next >

src/java.base/share/classes/java/net/Socket.java

Print this page




  72      */
  73     SocketImpl impl;
  74 
  75     /**
  76      * Socket input/output streams
  77      */
  78     private volatile InputStream in;
  79     private volatile OutputStream out;
  80     private static final VarHandle IN, OUT;
  81     static {
  82         try {
  83             MethodHandles.Lookup l = MethodHandles.lookup();
  84             IN = l.findVarHandle(Socket.class, "in", InputStream.class);
  85             OUT = l.findVarHandle(Socket.class, "out", OutputStream.class);
  86         } catch (Exception e) {
  87             throw new InternalError(e);
  88         }
  89     }
  90 
  91     /**
  92      * Creates an unconnected Socket.
  93      * <p>
  94      * If the application has specified a client socket implementation
  95      * factory, that factory's {@code createSocketImpl} method is called to
  96      * create the actual socket implementation. Otherwise a system-default
  97      * socket implementation is created.
  98      *
  99      * @since   1.1
 100      * @revised 1.4
 101      */
 102     public Socket() {
 103         setImpl();
 104     }
 105 
 106     /**
 107      * Creates an unconnected socket, specifying the type of proxy, if any,
 108      * that should be used regardless of any other settings.
 109      * <P>
 110      * If there is a security manager, its {@code checkConnect} method
 111      * is called with the proxy host address and port number
 112      * as its arguments. This could result in a SecurityException.
 113      * <P>
 114      * Examples:
 115      * <UL> <LI>{@code Socket s = new Socket(Proxy.NO_PROXY);} will create
 116      * a plain socket ignoring any other proxy configuration.</LI>
 117      * <LI>{@code Socket s = new Socket(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("socks.mydom.com", 1080)));}


 181      * the subclass wishes to use on the Socket.
 182      *
 183      * @exception SocketException if there is an error in the underlying protocol,
 184      * such as a TCP error.
 185      * @since   1.1
 186      */
 187     protected Socket(SocketImpl impl) throws SocketException {
 188         this.impl = impl;
 189     }
 190 
 191     /**
 192      * Creates a stream socket and connects it to the specified port
 193      * number on the named host.
 194      * <p>
 195      * If the specified host is {@code null} it is the equivalent of
 196      * specifying the address as
 197      * {@link java.net.InetAddress#getByName InetAddress.getByName}{@code (null)}.
 198      * In other words, it is equivalent to specifying an address of the
 199      * loopback interface. </p>
 200      * <p>
 201      * If the application has specified a client socket implementation
 202      * factory, that factory's {@code createSocketImpl} method is called to
 203      * create the actual socket implementation. Otherwise a system-default
 204      * socket implementation is created.
 205      * <p>
 206      * If there is a security manager, its
 207      * {@code checkConnect} method is called
 208      * with the host address and {@code port}
 209      * as its arguments. This could result in a SecurityException.
 210      *
 211      * @param      host   the host name, or {@code null} for the loopback address.
 212      * @param      port   the port number.
 213      *
 214      * @exception  UnknownHostException if the IP address of
 215      * the host could not be determined.
 216      *
 217      * @exception  IOException  if an I/O error occurs when creating the socket.
 218      * @exception  SecurityException  if a security manager exists and its
 219      *             {@code checkConnect} method doesn't allow the operation.
 220      * @exception  IllegalArgumentException if the port parameter is outside
 221      *             the specified range of valid port values, which is between
 222      *             0 and 65535, inclusive.
 223      * @see        java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory)
 224      * @see        java.net.SocketImpl
 225      * @see        java.net.SocketImplFactory#createSocketImpl()
 226      * @see        SecurityManager#checkConnect
 227      */
 228     public Socket(String host, int port)
 229         throws UnknownHostException, IOException
 230     {
 231         this(host != null ? new InetSocketAddress(host, port) :
 232              new InetSocketAddress(InetAddress.getByName(null), port),
 233              (SocketAddress) null, true);
 234     }
 235 
 236     /**
 237      * Creates a stream socket and connects it to the specified port
 238      * number at the specified IP address.
 239      * <p>
 240      * If the application has specified a client socket implementation
 241      * factory, that factory's {@code createSocketImpl} method is called to
 242      * create the actual socket implementation. Otherwise a system-default
 243      * socket implementation is created.
 244      * <p>
 245      * If there is a security manager, its
 246      * {@code checkConnect} method is called
 247      * with the host address and {@code port}
 248      * as its arguments. This could result in a SecurityException.
 249      *
 250      * @param      address   the IP address.
 251      * @param      port      the port number.
 252      * @exception  IOException  if an I/O error occurs when creating the socket.
 253      * @exception  SecurityException  if a security manager exists and its
 254      *             {@code checkConnect} method doesn't allow the operation.
 255      * @exception  IllegalArgumentException if the port parameter is outside
 256      *             the specified range of valid port values, which is between
 257      *             0 and 65535, inclusive.
 258      * @exception  NullPointerException if {@code address} is null.
 259      * @see        java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory)
 260      * @see        java.net.SocketImpl
 261      * @see        java.net.SocketImplFactory#createSocketImpl()
 262      * @see        SecurityManager#checkConnect
 263      */


 347     public Socket(InetAddress address, int port, InetAddress localAddr,
 348                   int localPort) throws IOException {
 349         this(address != null ? new InetSocketAddress(address, port) : null,
 350              new InetSocketAddress(localAddr, localPort), true);
 351     }
 352 
 353     /**
 354      * Creates a stream socket and connects it to the specified port
 355      * number on the named host.
 356      * <p>
 357      * If the specified host is {@code null} it is the equivalent of
 358      * specifying the address as
 359      * {@link java.net.InetAddress#getByName InetAddress.getByName}{@code (null)}.
 360      * In other words, it is equivalent to specifying an address of the
 361      * loopback interface. </p>
 362      * <p>
 363      * If the stream argument is {@code true}, this creates a
 364      * stream socket. If the stream argument is {@code false}, it
 365      * creates a datagram socket.
 366      * <p>
 367      * If the application has specified a client socket implementation
 368      * factory, that factory's {@code createSocketImpl} method is called to
 369      * create the actual socket implementation. Otherwise a system-default
 370      * socket implementation is created.
 371      * <p>
 372      * If there is a security manager, its
 373      * {@code checkConnect} method is called
 374      * with the host address and {@code port}
 375      * as its arguments. This could result in a SecurityException.
 376      * <p>
 377      * If a UDP socket is used, TCP/IP related socket options will not apply.
 378      *
 379      * @param      host     the host name, or {@code null} for the loopback address.
 380      * @param      port     the port number.
 381      * @param      stream   a {@code boolean} indicating whether this is
 382      *                      a stream socket or a datagram socket.
 383      * @exception  IOException  if an I/O error occurs when creating the socket.
 384      * @exception  SecurityException  if a security manager exists and its
 385      *             {@code checkConnect} method doesn't allow the operation.
 386      * @exception  IllegalArgumentException if the port parameter is outside
 387      *             the specified range of valid port values, which is between
 388      *             0 and 65535, inclusive.
 389      * @see        java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory)
 390      * @see        java.net.SocketImpl
 391      * @see        java.net.SocketImplFactory#createSocketImpl()
 392      * @see        SecurityManager#checkConnect
 393      * @deprecated Use DatagramSocket instead for UDP transport.
 394      */
 395     @Deprecated
 396     public Socket(String host, int port, boolean stream) throws IOException {
 397         this(host != null ? new InetSocketAddress(host, port) :
 398                new InetSocketAddress(InetAddress.getByName(null), port),
 399              (SocketAddress) null, stream);
 400     }
 401 
 402     /**
 403      * Creates a socket and connects it to the specified port number at
 404      * the specified IP address.
 405      * <p>
 406      * If the stream argument is {@code true}, this creates a
 407      * stream socket. If the stream argument is {@code false}, it
 408      * creates a datagram socket.
 409      * <p>
 410      * If the application has specified a client socket implementation
 411      * factory, that factory's {@code createSocketImpl} method is called to
 412      * create the actual socket implementation. Otherwise a system-default
 413      * socket implementation is created.
 414      *
 415      * <p>If there is a security manager, its
 416      * {@code checkConnect} method is called
 417      * with {@code host.getHostAddress()} and {@code port}
 418      * as its arguments. This could result in a SecurityException.
 419      * <p>
 420      * If UDP socket is used, TCP/IP related socket options will not apply.
 421      *
 422      * @param      host     the IP address.
 423      * @param      port      the port number.
 424      * @param      stream    if {@code true}, create a stream socket;
 425      *                       otherwise, create a datagram socket.
 426      * @exception  IOException  if an I/O error occurs when creating the socket.
 427      * @exception  SecurityException  if a security manager exists and its
 428      *             {@code checkConnect} method doesn't allow the operation.
 429      * @exception  IllegalArgumentException if the port parameter is outside
 430      *             the specified range of valid port values, which is between
 431      *             0 and 65535, inclusive.
 432      * @exception  NullPointerException if {@code host} is null.
 433      * @see        java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory)




  72      */
  73     SocketImpl impl;
  74 
  75     /**
  76      * Socket input/output streams
  77      */
  78     private volatile InputStream in;
  79     private volatile OutputStream out;
  80     private static final VarHandle IN, OUT;
  81     static {
  82         try {
  83             MethodHandles.Lookup l = MethodHandles.lookup();
  84             IN = l.findVarHandle(Socket.class, "in", InputStream.class);
  85             OUT = l.findVarHandle(Socket.class, "out", OutputStream.class);
  86         } catch (Exception e) {
  87             throw new InternalError(e);
  88         }
  89     }
  90 
  91     /**
  92      * Creates an unconnected socket, with the
  93      * system-default type of SocketImpl.




  94      *
  95      * @since   1.1
  96      * @revised 1.4
  97      */
  98     public Socket() {
  99         setImpl();
 100     }
 101 
 102     /**
 103      * Creates an unconnected socket, specifying the type of proxy, if any,
 104      * that should be used regardless of any other settings.
 105      * <P>
 106      * If there is a security manager, its {@code checkConnect} method
 107      * is called with the proxy host address and port number
 108      * as its arguments. This could result in a SecurityException.
 109      * <P>
 110      * Examples:
 111      * <UL> <LI>{@code Socket s = new Socket(Proxy.NO_PROXY);} will create
 112      * a plain socket ignoring any other proxy configuration.</LI>
 113      * <LI>{@code Socket s = new Socket(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("socks.mydom.com", 1080)));}


 177      * the subclass wishes to use on the Socket.
 178      *
 179      * @exception SocketException if there is an error in the underlying protocol,
 180      * such as a TCP error.
 181      * @since   1.1
 182      */
 183     protected Socket(SocketImpl impl) throws SocketException {
 184         this.impl = impl;
 185     }
 186 
 187     /**
 188      * Creates a stream socket and connects it to the specified port
 189      * number on the named host.
 190      * <p>
 191      * If the specified host is {@code null} it is the equivalent of
 192      * specifying the address as
 193      * {@link java.net.InetAddress#getByName InetAddress.getByName}{@code (null)}.
 194      * In other words, it is equivalent to specifying an address of the
 195      * loopback interface. </p>
 196      * <p>
 197      * If the application has specified a server socket factory, that
 198      * factory's {@code createSocketImpl} method is called to create
 199      * the actual socket implementation. Otherwise a "plain" socket is created.

 200      * <p>
 201      * If there is a security manager, its
 202      * {@code checkConnect} method is called
 203      * with the host address and {@code port}
 204      * as its arguments. This could result in a SecurityException.
 205      *
 206      * @param      host   the host name, or {@code null} for the loopback address.
 207      * @param      port   the port number.
 208      *
 209      * @exception  UnknownHostException if the IP address of
 210      * the host could not be determined.
 211      *
 212      * @exception  IOException  if an I/O error occurs when creating the socket.
 213      * @exception  SecurityException  if a security manager exists and its
 214      *             {@code checkConnect} method doesn't allow the operation.
 215      * @exception  IllegalArgumentException if the port parameter is outside
 216      *             the specified range of valid port values, which is between
 217      *             0 and 65535, inclusive.
 218      * @see        java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory)
 219      * @see        java.net.SocketImpl
 220      * @see        java.net.SocketImplFactory#createSocketImpl()
 221      * @see        SecurityManager#checkConnect
 222      */
 223     public Socket(String host, int port)
 224         throws UnknownHostException, IOException
 225     {
 226         this(host != null ? new InetSocketAddress(host, port) :
 227              new InetSocketAddress(InetAddress.getByName(null), port),
 228              (SocketAddress) null, true);
 229     }
 230 
 231     /**
 232      * Creates a stream socket and connects it to the specified port
 233      * number at the specified IP address.
 234      * <p>
 235      * If the application has specified a socket factory, that factory's
 236      * {@code createSocketImpl} method is called to create the
 237      * actual socket implementation. Otherwise a "plain" socket is created.

 238      * <p>
 239      * If there is a security manager, its
 240      * {@code checkConnect} method is called
 241      * with the host address and {@code port}
 242      * as its arguments. This could result in a SecurityException.
 243      *
 244      * @param      address   the IP address.
 245      * @param      port      the port number.
 246      * @exception  IOException  if an I/O error occurs when creating the socket.
 247      * @exception  SecurityException  if a security manager exists and its
 248      *             {@code checkConnect} method doesn't allow the operation.
 249      * @exception  IllegalArgumentException if the port parameter is outside
 250      *             the specified range of valid port values, which is between
 251      *             0 and 65535, inclusive.
 252      * @exception  NullPointerException if {@code address} is null.
 253      * @see        java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory)
 254      * @see        java.net.SocketImpl
 255      * @see        java.net.SocketImplFactory#createSocketImpl()
 256      * @see        SecurityManager#checkConnect
 257      */


 341     public Socket(InetAddress address, int port, InetAddress localAddr,
 342                   int localPort) throws IOException {
 343         this(address != null ? new InetSocketAddress(address, port) : null,
 344              new InetSocketAddress(localAddr, localPort), true);
 345     }
 346 
 347     /**
 348      * Creates a stream socket and connects it to the specified port
 349      * number on the named host.
 350      * <p>
 351      * If the specified host is {@code null} it is the equivalent of
 352      * specifying the address as
 353      * {@link java.net.InetAddress#getByName InetAddress.getByName}{@code (null)}.
 354      * In other words, it is equivalent to specifying an address of the
 355      * loopback interface. </p>
 356      * <p>
 357      * If the stream argument is {@code true}, this creates a
 358      * stream socket. If the stream argument is {@code false}, it
 359      * creates a datagram socket.
 360      * <p>
 361      * If the application has specified a server socket factory, that
 362      * factory's {@code createSocketImpl} method is called to create
 363      * the actual socket implementation. Otherwise a "plain" socket is created.

 364      * <p>
 365      * If there is a security manager, its
 366      * {@code checkConnect} method is called
 367      * with the host address and {@code port}
 368      * as its arguments. This could result in a SecurityException.
 369      * <p>
 370      * If a UDP socket is used, TCP/IP related socket options will not apply.
 371      *
 372      * @param      host     the host name, or {@code null} for the loopback address.
 373      * @param      port     the port number.
 374      * @param      stream   a {@code boolean} indicating whether this is
 375      *                      a stream socket or a datagram socket.
 376      * @exception  IOException  if an I/O error occurs when creating the socket.
 377      * @exception  SecurityException  if a security manager exists and its
 378      *             {@code checkConnect} method doesn't allow the operation.
 379      * @exception  IllegalArgumentException if the port parameter is outside
 380      *             the specified range of valid port values, which is between
 381      *             0 and 65535, inclusive.
 382      * @see        java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory)
 383      * @see        java.net.SocketImpl
 384      * @see        java.net.SocketImplFactory#createSocketImpl()
 385      * @see        SecurityManager#checkConnect
 386      * @deprecated Use DatagramSocket instead for UDP transport.
 387      */
 388     @Deprecated
 389     public Socket(String host, int port, boolean stream) throws IOException {
 390         this(host != null ? new InetSocketAddress(host, port) :
 391                new InetSocketAddress(InetAddress.getByName(null), port),
 392              (SocketAddress) null, stream);
 393     }
 394 
 395     /**
 396      * Creates a socket and connects it to the specified port number at
 397      * the specified IP address.
 398      * <p>
 399      * If the stream argument is {@code true}, this creates a
 400      * stream socket. If the stream argument is {@code false}, it
 401      * creates a datagram socket.
 402      * <p>
 403      * If the application has specified a server socket factory, that
 404      * factory's {@code createSocketImpl} method is called to create
 405      * the actual socket implementation. Otherwise a "plain" socket is created.

 406      *
 407      * <p>If there is a security manager, its
 408      * {@code checkConnect} method is called
 409      * with {@code host.getHostAddress()} and {@code port}
 410      * as its arguments. This could result in a SecurityException.
 411      * <p>
 412      * If UDP socket is used, TCP/IP related socket options will not apply.
 413      *
 414      * @param      host     the IP address.
 415      * @param      port      the port number.
 416      * @param      stream    if {@code true}, create a stream socket;
 417      *                       otherwise, create a datagram socket.
 418      * @exception  IOException  if an I/O error occurs when creating the socket.
 419      * @exception  SecurityException  if a security manager exists and its
 420      *             {@code checkConnect} method doesn't allow the operation.
 421      * @exception  IllegalArgumentException if the port parameter is outside
 422      *             the specified range of valid port values, which is between
 423      *             0 and 65535, inclusive.
 424      * @exception  NullPointerException if {@code host} is null.
 425      * @see        java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory)


< prev index next >