If this is done in Java code, you could use byteman to change the behavior.
Intercepting and changing the data flow in the TCP/IP stack itself gets
more tricky; you have to insert a packet filter and then drop/reorder
packets. In Java, you might be able to use jnetpcap, which allows you to
get access to the individual packets.
Maybe iptables will also allow you to do this, but it's rather static.
On 1/18/12 10:05 PM, Michal Linhard wrote:
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();
}
}
}
--
Bela Ban
Lead JGroups (
http://www.jgroups.org)
JBoss / Red Hat