Code example for ServerSocket

Methods: getLocalPort, sleep

0
    public void testSoapClient() throws Exception 
    { 
        // 
        try 
        { 
            _server = new ServerSocket(0);
            _port = _server.getLocalPort();
            assertTrue("Server is not yet bound to a port", _port != -1);
            assertNotNull(_server);
        } 
        catch (Exception ex)
        { 
            ex.printStackTrace();
            fail("Unable to start the socket server");
        } 
 
        Thread _socketAcceptor = new Thread()
        { 
            public void run() 
            { 
                try 
                { 
                    Socket socket = _server.accept();
                    new ConnectionHandler(socket);
                } 
                catch (IOException e)
                { 
                    e.printStackTrace();
                } 
            } 
        }; 
        _socketAcceptor.start();
        // Sleep for 1 second to allow the ServerSocket readiness 
        Thread.sleep(1000);
        SoapClient sc = getSoapClient();
        assertNotNull(sc);
        sc.sendSOAPMessage();
 
        _socketAcceptor.join(2000);