Code example for ServerSocket

Methods: accept, bind, close, getLocalPort, getLocalSocketAddress, isBound, setPerformancePreferences

0
                Integer.MAX_VALUE);
    } 
 
    void performancePreferenceTest(int connectionTime, int latency,
            int bandwidth) throws Exception {
        ServerSocket theSocket = new ServerSocket();
        theSocket.setPerformancePreferences(connectionTime, latency, bandwidth);
 
        InetSocketAddress theAddress = new InetSocketAddress(InetAddress
                .getLocalHost(), 0);
        theSocket.bind(theAddress);
        int portNumber = theSocket.getLocalPort();
        assertTrue( 
                "Returned incorrect InetSocketAddress(2):" 
                        + theSocket.getLocalSocketAddress().toString()
                        + "Expected: " 
                        + (new InetSocketAddress(InetAddress.getLocalHost(),
                                portNumber)).toString(), theSocket
                        .getLocalSocketAddress().equals(
                                new InetSocketAddress(InetAddress
                                        .getLocalHost(), portNumber)));
        assertTrue("Server socket not bound when it should be:", theSocket
                .isBound());
 
        // now make sure that it is actually bound and listening on the 
        // address we provided 
        Socket clientSocket = new Socket();
        InetSocketAddress clAddress = new InetSocketAddress(InetAddress
                .getLocalHost(), portNumber);
        clientSocket.connect(clAddress);
        Socket servSock = theSocket.accept();
 
        assertEquals(clAddress, clientSocket.getRemoteSocketAddress());
        theSocket.close();
        servSock.close();
        clientSocket.close();
    } 
 
    public void test_ConstructorII() throws IOException {