[infinispan-dev] simulating connection timeout

Michal Linhard mlinhard at redhat.com
Wed Jan 18 16:05:07 EST 2012


Hi all,

probably stupid question from the area of java server programming and 
networking if anyone's interrested:
(otherwise sorry for spam)

is there a simple way of creating a server in java that would always 
give me connection timeout ? (not connection refused, nor socket timeout 
during read)
is it even possible to control establishing tcp connection from java ?

what layer does control this ?
it seems that once you bind a socket the connections are automatically 
established.
probably on some JVM level ?
so when I get

java.net.ConnectException: Connection timed out
	at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
	at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:567)
	at sun.nio.ch.SocketAdaptor.connect(SocketAdaptor.java:100)
	at org.infinispan.client.hotrod.impl.transport.tcp.TcpTransport.<init>(TcpTransport.java:80)


this means that server JVM or OS networking layer or whatever was so 
stressed that it didn't have time to respond with SYN,ACK flagged packet 
to establish the connection ?

m.


----------- my test code:
public class EchoServer {
    public static void main(String[] args) {
       try {
          ServerSocketChannel serverChannel = ServerSocketChannel.open();
          InetSocketAddress isa = new InetSocketAddress("localhost", 9090);
          serverChannel.socket().bind(isa);
          Thread.sleep(20000);
       } catch (Exception e) {
          e.printStackTrace();
       }

    }
}

public class EchoClient {
    public static void main(String[] args) {
       long startTime = System.currentTimeMillis();
       try {
          SocketAddress serverAddress = new 
InetSocketAddress("localhost", 9090);
          SocketChannel socketChannel = SocketChannel.open();
          Socket socket = socketChannel.socket();
          socket.connect(serverAddress, 5000);
          socket.setSoTimeout(5000);
          BufferedInputStream socketInputStream = new 
BufferedInputStream(socket.getInputStream(), socket.getReceiveBufferSize());
          BufferedOutputStream socketOutputStream = new 
BufferedOutputStream(socket.getOutputStream(), socket.getSendBufferSize());
          PrintWriter out = new PrintWriter(socketOutputStream);
          out.println("Hello");
          out.flush();
          BufferedReader in = new BufferedReader(new 
InputStreamReader(socketInputStream));
          System.out.println(in.readLine());
       } catch (Exception e) {
          System.out.println("Exception occured after " + 
(System.currentTimeMillis() - startTime) + " ms");
          e.printStackTrace();
       }
    }
}

-- 
Michal Linhard
Quality Assurance Engineer
JBoss Enterprise Datagrid

Red Hat Czech s.r.o.
Purkynova 99 612 45 Brno, Czech Republic
phone: +420 532 294 320 ext. 62320
mobile: +420 728 626 363



More information about the infinispan-dev mailing list