import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class Foo implements Runnable { public int turn = SERVER; public static final int SERVER = 1; public static final int CLIENT = 2; public static void main(String[] args) throws Exception { ServerSocket server = null; Socket client = null; try{ server = new ServerSocket(4444); } catch (IOException e) { System.out.println("Server: Could not listen on port 4444"); System.exit(-1); } Foo foo = new Foo(); new Thread(foo).start(); System.out.println("Server: Socket bound..."); foo.handOff(CLIENT); foo.waitFor(SERVER); try{ System.out.println("Server: Now Accepting the connection..."); client = server.accept(); System.out.println("Server: Accept succeeded"); } catch (IOException e) { System.out.println("Server: Accept failed: " + e); System.exit(-1); } System.out.println("Server: Setting TCP NO_DELAY"); // this will throw EINVAL on solaris client.setTcpNoDelay(false); // on all other OS's you will see a connection reset error here client.getInputStream().read(); server.close(); } public synchronized void waitFor(int who) throws InterruptedException { while (turn != who) wait(); } public synchronized void handOff (int who) throws InterruptedException { turn = who; notify(); } public void run() { try { Socket socket = new Socket("localhost", 4444); System.out.println("Client: connected !"); waitFor(CLIENT); System.out.println("Client: Sending RST!"); socket.setSoLinger(true, 0); socket.close(); System.out.println("Client: Close Done"); handOff(SERVER); } catch (Exception e) { throw new RuntimeException(e); } } }