59 class ServerSocket implements java.io.Closeable { 60 /** 61 * Various states of this socket. 62 */ 63 private boolean created = false; 64 private boolean bound = false; 65 private boolean closed = false; 66 private Object closeLock = new Object(); 67 68 /** 69 * The implementation of this Socket. 70 */ 71 private SocketImpl impl; 72 73 /** 74 * Are we using an older SocketImpl? 75 */ 76 private boolean oldImpl = false; 77 78 /** 79 * Package-private constructor to create a ServerSocket associated with 80 * the given SocketImpl. 81 */ 82 ServerSocket(SocketImpl impl) { 83 this.impl = impl; 84 impl.setServerSocket(this); 85 } 86 87 /** 88 * Creates an unbound server socket. 89 * 90 * @exception IOException IO error when opening the socket. 91 * @revised 1.4 92 */ 93 public ServerSocket() throws IOException { 94 setImpl(); 95 } 96 97 /** 98 * Creates a server socket, bound to the specified port. A port number 99 * of {@code 0} means that the port number is automatically 100 * allocated, typically from an ephemeral port range. This port 101 * number can then be retrieved by calling {@link #getLocalPort getLocalPort}. 102 * <p> 498 * connection. 499 * @exception SecurityException if a security manager exists and its 500 * {@code checkAccept} method doesn't allow the operation. 501 * @exception SocketTimeoutException if a timeout was previously set with setSoTimeout and 502 * the timeout has been reached. 503 * @exception java.nio.channels.IllegalBlockingModeException 504 * if this socket has an associated channel, the channel is in 505 * non-blocking mode, and there is no connection ready to be 506 * accepted 507 * 508 * @return the new Socket 509 * @see SecurityManager#checkAccept 510 * @revised 1.4 511 * @spec JSR-51 512 */ 513 public Socket accept() throws IOException { 514 if (isClosed()) 515 throw new SocketException("Socket is closed"); 516 if (!isBound()) 517 throw new SocketException("Socket is not bound yet"); 518 Socket s = new Socket((SocketImpl) null); 519 implAccept(s); 520 return s; 521 } 522 523 /** 524 * Subclasses of ServerSocket use this method to override accept() 525 * to return their own subclass of socket. So a FooServerSocket 526 * will typically hand this method an <i>empty</i> FooSocket. On 527 * return from implAccept the FooSocket will be connected to a client. 528 * 529 * @param s the Socket 530 * @throws java.nio.channels.IllegalBlockingModeException 531 * if this socket has an associated channel, 532 * and the channel is in non-blocking mode 533 * @throws IOException if an I/O error occurs when waiting 534 * for a connection. 535 * @since 1.1 536 * @revised 1.4 537 * @spec JSR-51 538 */ 539 protected final void implAccept(Socket s) throws IOException { 540 SocketImpl si = null; 541 try { 542 if (s.impl == null) 543 s.setImpl(); 544 else { 545 s.impl.reset(); 546 } 547 si = s.impl; 548 s.impl = null; 549 si.address = new InetAddress(); 550 si.fd = new FileDescriptor(); 551 getImpl().accept(si); 552 SocketCleanable.register(si.fd); // raw fd has been set 553 554 SecurityManager security = System.getSecurityManager(); 555 if (security != null) { 556 security.checkAccept(si.getInetAddress().getHostAddress(), 557 si.getPort()); 558 } 559 } catch (IOException e) { 560 if (si != null) 561 si.reset(); 562 s.impl = si; 563 throw e; 564 } catch (SecurityException e) { 565 if (si != null) 566 si.reset(); 567 s.impl = si; 568 throw e; 569 } 570 s.impl = si; | 59 class ServerSocket implements java.io.Closeable { 60 /** 61 * Various states of this socket. 62 */ 63 private boolean created = false; 64 private boolean bound = false; 65 private boolean closed = false; 66 private Object closeLock = new Object(); 67 68 /** 69 * The implementation of this Socket. 70 */ 71 private SocketImpl impl; 72 73 /** 74 * Are we using an older SocketImpl? 75 */ 76 private boolean oldImpl = false; 77 78 /** 79 * Creates a server socket with a user-specified SocketImpl. 80 * 81 * @param impl the user-specified SocketImpl 82 */ 83 protected ServerSocket(SocketImpl impl) { 84 this.impl = impl; 85 impl.setServerSocket(this); 86 } 87 88 /** 89 * Creates an unbound server socket. 90 * 91 * @exception IOException IO error when opening the socket. 92 * @revised 1.4 93 */ 94 public ServerSocket() throws IOException { 95 setImpl(); 96 } 97 98 /** 99 * Creates a server socket, bound to the specified port. A port number 100 * of {@code 0} means that the port number is automatically 101 * allocated, typically from an ephemeral port range. This port 102 * number can then be retrieved by calling {@link #getLocalPort getLocalPort}. 103 * <p> 499 * connection. 500 * @exception SecurityException if a security manager exists and its 501 * {@code checkAccept} method doesn't allow the operation. 502 * @exception SocketTimeoutException if a timeout was previously set with setSoTimeout and 503 * the timeout has been reached. 504 * @exception java.nio.channels.IllegalBlockingModeException 505 * if this socket has an associated channel, the channel is in 506 * non-blocking mode, and there is no connection ready to be 507 * accepted 508 * 509 * @return the new Socket 510 * @see SecurityManager#checkAccept 511 * @revised 1.4 512 * @spec JSR-51 513 */ 514 public Socket accept() throws IOException { 515 if (isClosed()) 516 throw new SocketException("Socket is closed"); 517 if (!isBound()) 518 throw new SocketException("Socket is not bound yet"); 519 520 Socket s = null; 521 Class<?> cls = impl.getClass(); 522 if (cls.getName().contains("RdmaSocketImpl")) { 523 try { 524 Constructor<?> c = cls.getConstructor(); 525 s = new Socket((SocketImpl)c.newInstance()); 526 } catch (NoSuchMethodException | InstantiationException | 527 IllegalAccessException | InvocationTargetException e) { 528 throw new AssertionError(e); 529 } 530 } else { 531 s = new Socket((SocketImpl) null); 532 } 533 implAccept(s); 534 return s; 535 } 536 537 /** 538 * Subclasses of ServerSocket use this method to override accept() 539 * to return their own subclass of socket. So a FooServerSocket 540 * will typically hand this method an <i>empty</i> FooSocket. On 541 * return from implAccept the FooSocket will be connected to a client. 542 * 543 * @param s the Socket 544 * @throws java.nio.channels.IllegalBlockingModeException 545 * if this socket has an associated channel, 546 * and the channel is in non-blocking mode 547 * @throws IOException if an I/O error occurs when waiting 548 * for a connection. 549 * @since 1.1 550 * @revised 1.4 551 * @spec JSR-51 552 */ 553 protected final void implAccept(Socket s) throws IOException { 554 SocketImpl si = null; 555 try { 556 if (s.impl == null) 557 s.setImpl(); 558 else { 559 s.impl.reset(); 560 } 561 si = s.impl; 562 s.impl = null; 563 si.setAddress(new InetAddress()); 564 si.setFileDescriptor(new FileDescriptor()); 565 getImpl().accept(si); 566 SocketCleanable.register(si.fd); // raw fd has been set 567 568 SecurityManager security = System.getSecurityManager(); 569 if (security != null) { 570 security.checkAccept(si.getInetAddress().getHostAddress(), 571 si.getPort()); 572 } 573 } catch (IOException e) { 574 if (si != null) 575 si.reset(); 576 s.impl = si; 577 throw e; 578 } catch (SecurityException e) { 579 if (si != null) 580 si.reset(); 581 s.impl = si; 582 throw e; 583 } 584 s.impl = si; |