Code example for ServerSocket

Methods: getInetAddress, getLocalPort, setReuseAddress, setSoTimeout

0
import java.net.ServerSocket; 
import java.net.Socket; 
 
public class ServerSocketTest extends junit.framework.TestCase {
    public void testTimeoutAfterAccept() throws Exception { 
        final ServerSocket ss = new ServerSocket(0);
        ss.setReuseAddress(true);
        // On Unix, the receive timeout is inherited by the result of accept(2). 
        // Java specifies that it should always be 0 instead. 
        ss.setSoTimeout(1234);
        final Socket[] result = new Socket[1];
        Thread t = new Thread(new Runnable() {
            public void run() { 
                try { 
                    result[0] = ss.accept();
                } catch (IOException ex) {
                    ex.printStackTrace();
                    fail();
                } 
            } 
        }); 
        t.start();
        new Socket(ss.getInetAddress(), ss.getLocalPort());
        t.join();
        assertEquals(0, result[0].getSoTimeout());
    } 
}