public void testDirectSockets_exceptionIfPortNotOpen() throws IOException {
  final InetSocketAddress serverAddr1=new InetSocketAddress(getPort(0));
  final InetSocketAddress serverAddr2=new InetSocketAddress(getPort(0));
  final ServerSocket ss1=new ServerSocket();
  try {
    ss1.bind(serverAddr1);
    assertTrue(ss1.getChannel() == null);
    try {
      accept(ss1);
      fail("Expected timeout failure");
    }
 catch (    AssertionFailedError afe) {
    }
    final SocketChannel cs1=SocketChannel.open();
    try {
      final boolean immediate1=cs1.connect(serverAddr1);
      if (!immediate1) {
        if (!cs1.finishConnect()) {
          fail("Did not connect.");
        }
      }
    }
  finally {
      cs1.close();
    }
    final SocketChannel cs2=SocketChannel.open();
    try {
      cs1.connect(serverAddr2);
      fail("Expecting " + IOException.class);
    }
 catch (    IOException ex) {
      if (log.isInfoEnabled())       log.info("Ignoring expected exception: " + ex);
    }
 finally {
      cs2.close();
    }
    try {
      accept(ss1);
      fail("Expected timeout failure");
    }
 catch (    AssertionFailedError afe) {
    }
  }
  finally {
    ss1.close();
  }
}

public void testDirectSockets_exceptionIfPortNotOpen ( ) throws IOException { final InetSocketAddress serverAddr1 = new InetSocketAddress ( getPort ( 0 ) ) ; final InetSocketAddress serverAddr2 = new InetSocketAddress ( getPort ( 0 ) ) ; final ServerSocket ss1 = new ServerSocket ( ) ; try { ss1 . bind ( serverAddr1 ) ; assertTrue ( ss1 . getChannel ( ) == null ) ; try { accept ( ss1 ) ; fail ( "Expected timeout failure" ) ; } catch ( AssertionFailedError afe ) { } final SocketChannel cs1 = SocketChannel . open ( ) ; try { final boolean immediate1 = cs1 . connect ( serverAddr1 ) ; if ( ! immediate1 ) { if ( ! cs1 . finishConnect ( ) ) { fail ( "Did not connect." ) ; } } } finally { cs1 . close ( ) ; } final SocketChannel cs2 = SocketChannel . open ( ) ; try { cs1 . connect ( serverAddr2 ) ; fail ( "Expecting " + IOException . class ) ; } catch ( IOException ex ) { if ( log . isInfoEnabled ( ) ) log . info ( "Ignoring expected exception: " + ex ) ; } finally { cs2 . close ( ) ; } try { accept ( ss1 ) ; fail ( "Expected timeout failure" ) ; } catch ( AssertionFailedError afe ) { } } finally { ss1 . close ( ) ; } }
simple test of connecting to a server socket and the failure to connect to a port not associated with a server socket .
