Code example for ServerSocket

Methods: bind, setReuseAddress, setSoTimeout

0
    public void unlockWiFi() { 
        wifiLock.release();
    } 
 
    public void initializeServerSocket() throws IOException { 
        serverSocket = new ServerSocket();
        serverSocket.setReuseAddress(true);
        serverSocket.bind(new InetSocketAddress(PORT));
        serverSocket.setSoTimeout(500);
        Log.d(TAG, "Server socket created");
    } 
 
    public Socket acceptConnection() throws IOException, GameEndedException {
        while(!Thread.currentThread().isInterrupted()) {
            try { 
                return serverSocket.accept();
            } catch (SocketTimeoutException ex) {
                //intentionally empty 
            } 
        } 
        throw new GameEndedException();