Code example for ServerSocket

Methods: closegetInetAddressgetLocalPortgetLocalSocketAddressgetSoTimeoutisBoundisClosed

0
     */ 
    public void testSocket_Block_BeforeClose() throws Exception { 
        assertTrue(this.serverChannel.isOpen());
        assertTrue(this.serverChannel.isBlocking());
        ServerSocket s1 = this.serverChannel.socket();
        assertFalse(s1.isClosed());
        assertSocketNotAccepted(s1);
        ServerSocket s2 = this.serverChannel.socket();
        // same 
        assertSame(s1, s2);
 
        // socket close makes the channel close 
        s1.close();
        assertFalse(this.serverChannel.isOpen());
 
    } 
 
    public void testSocket_NonBlock_BeforeClose() throws Exception { 
        assertTrue(this.serverChannel.isOpen());
        this.serverChannel.configureBlocking(false);
        ServerSocket s1 = this.serverChannel.socket();
        assertFalse(s1.isClosed());
        assertSocketNotAccepted(s1);
        ServerSocket s2 = this.serverChannel.socket();
        // same 
        assertSame(s1, s2);
 
        // socket close makes the channel close 
        s1.close();
        assertFalse(this.serverChannel.isOpen());
 
    } 
 
    public void testSocket_Block_Closed() throws Exception { 
        this.serverChannel.close();
        assertFalse(this.serverChannel.isOpen());
        assertTrue(this.serverChannel.isBlocking());
        ServerSocket s1 = this.serverChannel.socket();
        assertTrue(s1.isClosed());
        assertSocketNotAccepted(s1);
        ServerSocket s2 = this.serverChannel.socket();
        // same 
        assertSame(s1, s2);
    } 
 
    public void testSocket_NonBlock_Closed() throws Exception { 
        this.serverChannel.configureBlocking(false);
        this.serverChannel.close();
        assertFalse(this.serverChannel.isBlocking());
        assertFalse(this.serverChannel.isOpen());
        ServerSocket s1 = this.serverChannel.socket();
        assertTrue(s1.isClosed());
        assertSocketNotAccepted(s1);
        ServerSocket s2 = this.serverChannel.socket();
        // same 
        assertSame(s1, s2);
    } 
 
    private void assertSocketNotAccepted(ServerSocket s) throws IOException {
        assertFalse(s.isBound());
        assertNull(s.getInetAddress());
        assertEquals(-1, s.getLocalPort());
        assertNull(s.getLocalSocketAddress());
        try { 
            assertEquals(0, s.getSoTimeout());
        } catch (IOException expected) {
            // Android doesn't cache the timeout, so the getsockopt(2) fails and throws. 
        } 
    } 
 
Connect your IDE to all the code out there  Get Codota for Java