Code example for ServerSocket

Methods: accept, close, setSoTimeout, sleep

0
        clientSocket.close();
    } 
 
    public void test_ConstructorII() throws IOException { 
        int freePortNumber = Support_PortManager.getNextPort();
        s = new ServerSocket(freePortNumber, 1);
        s.setSoTimeout(2000);
        startClient(freePortNumber);
        sconn = s.accept();
        sconn.close();
        s.close();
    } 
 
    static class SSClient implements Runnable {
        Socket cs;
 
        int port;
 
        public SSClient(int prt) {
            port = prt;
        } 
 
        public void run() { 
            try { 
                // Go to sleep so the server can setup and wait for connection 
                Thread.sleep(1000);
                cs = new Socket(InetAddress.getLocalHost().getHostName(), port);
                // Sleep again to allow server side processing. Thread is 
                // stopped by server. 
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                return; 
            } catch (Throwable e) {
                System.out.println("Error establishing client: " + e.toString());
            } finally { 
                try { 
                    if (cs != null)
                        cs.close();
                } catch (Exception e) {
                } 
            } 
        } 
    } 
 
    public void test_Constructor() throws IOException { 
        ServerSocket ss = new ServerSocket();
        assertEquals(-1, ss.getLocalPort());
        ss.close();
    } 
 
    public void test_ConstructorI() throws Exception { 
        int portNumber = Support_PortManager.getNextPort();
        s = new ServerSocket(portNumber);
        try { 
            new ServerSocket(portNumber);
            fail("IOException was not thrown."); 
        } catch(IOException ioe) {
            //expected 
        } 
        try { 
            startClient(s.getLocalPort());
            sconn = s.accept();
            assertNotNull("Was unable to accept connection", sconn);
            sconn.close();
        } finally { 
            s.close();
        } 
 
        s = new ServerSocket(0);
        try { 
            startClient(s.getLocalPort());
            sconn = s.accept();
            assertNotNull("Was unable to accept connection", sconn);
            sconn.close();
        } finally { 
            s.close();
        } 
    } 
 
    public void test_ConstructorIILjava_net_InetAddress() throws IOException { 
        int freePortNumber = Support_PortManager.getNextPort();
 
        ServerSocket ss = new ServerSocket(freePortNumber, 10, InetAddress.getLocalHost());
        try { 
            new ServerSocket(freePortNumber, 10, InetAddress.getLocalHost());
            fail("IOException was not thrown."); 
        } catch(IOException expected) {
        } 
        ss.close();
 
        try { 
            new ServerSocket(65536, 10, InetAddress.getLocalHost());
            fail("IllegalArgumentException was not thrown."); 
        } catch(IllegalArgumentException expected) {
        } 
    } 
 
    public void test_LocalPort() throws IOException { 
        ServerSocket ss1 = new ServerSocket(4242);
        assertEquals(ss1.getLocalPort(), 4242);
        ss1.close();
 
        ServerSocket ss2 = new ServerSocket();
        ss2.bind(new InetSocketAddress("127.0.0.1", 4343));
        assertEquals(ss2.getLocalPort(), 4343);
        ss2.close();
 
        ServerSocket ss3 = new ServerSocket(0);
        assertTrue(ss3.getLocalPort() != 0);
        ss3.close();
    } 
 
    class MockSocketFactory implements SocketImplFactory {
        public SocketImpl createSocketImpl() {
            return new MockSocketImpl(); 
        } 
    } 
 
    public void test_ConstructorI_SocksSet() throws IOException { 
        // Harmony-623 regression test 
        ServerSocket ss = null;
        Properties props = (Properties) System.getProperties().clone();
        try { 
            System.setProperty("socksProxyHost", "127.0.0.1");
            System.setProperty("socksProxyPort", "12345");
            ss = new ServerSocket(0);
        } finally { 
            System.setProperties(props);
            if (null != ss) {
                ss.close();
            } 
        } 
    } 
 
    public void test_accept() throws IOException { 
        int portNumber = Support_PortManager.getNextPort();
 
        ServerSocket newSocket = new ServerSocket(portNumber);
        newSocket.setSoTimeout(500);
        try { 
            Socket accepted = newSocket.accept();
            fail("SocketTimeoutException was not thrown: " + accepted);
        } catch(SocketTimeoutException expected) {
        } 
        newSocket.close();
 
        ServerSocketChannel ssc = ServerSocketChannel.open();
        ServerSocket ss = ssc.socket();
 
        try { 
            ss.accept();
            fail("IllegalBlockingModeException was not thrown."); 
        } catch(IllegalBlockingModeException ibme) {
            //expected 
        } finally { 
            ss.close();
            ssc.close();
        } 
    } 
 
    public void test_getSoTimeout() throws IOException { 
        ServerSocket newSocket = new ServerSocket();
        newSocket.close();
        try { 
            newSocket.setSoTimeout(100);
            fail("SocketException was not thrown."); 
        } catch(SocketException e) {
            //expected 
        } 
    } 
 
    public void test_setSoTimeoutI() throws IOException { 
        ServerSocket newSocket = new ServerSocket();
        newSocket.close();
        try { 
            newSocket.setSoTimeout(100);
            fail("SocketException was not thrown."); 
        } catch(SocketException se) {
            //expected 
        } 
    } 
 
    public void test_toString() throws Exception { 
        s = new ServerSocket(0);
        int portNumber = s.getLocalPort();
        assertTrue(s.toString().contains("" + portNumber));
        s.close();
    } 
 
    public void test_setReuseAddressZ() throws IOException { 
        ServerSocket newSocket = new ServerSocket();
        newSocket.close();
        try { 
            newSocket.setReuseAddress(true);
            fail("SocketException was not thrown."); 
        } catch(SocketException expected) {
        } 
    } 
 
    public void test_getReuseAddress() throws IOException { 
        ServerSocket newSocket = new ServerSocket();
        newSocket.close();
        try { 
            newSocket.getReuseAddress();
            fail("SocketException was not thrown."); 
        } catch(SocketException e) {
            //expected 
        } 
    } 
 
    public void test_setReceiveBufferSizeI() throws IOException { 
        ServerSocket newSocket = new ServerSocket();
        newSocket.close();
        try { 
            newSocket.setReceiveBufferSize(10);
            fail("SocketException was not thrown."); 
        } catch(SocketException se) {
            //expected 
        } 
    } 
 
    public void test_getReceiveBufferSize() throws IOException { 
        ServerSocket newSocket = new ServerSocket();
        newSocket.close();
        try { 
            newSocket.getReceiveBufferSize();
            fail("SocketException was not thrown."); 
        } catch (SocketException e) {
            //expected 
        } 
    } 
 
    protected void tearDown() { 
        try { 
            if (s != null)
                s.close();
            if (sconn != null)
                sconn.close();
            if (t != null)
                t.interrupt();
        } catch (Exception e) {
        } 
    } 
 
    /** 
     * Sets up the fixture, for example, open a network connection. This method 
     * is called before a test is executed. 
     */ 
    protected void startClient(int port) {
        t = new Thread(new SSClient(port), "SSClient");
        t.start();
        try { 
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            System.out.println("Exception during startClinet()" + e.toString());
        } 
    }