Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reproduce a silently dropped TCP/IP connection?

I have a situation where a Java program establishes a long-running TCP/IP connection to a server and said connection is functioning exactly as documented, except for one thing in an environment outside my control.

Every Saturday that server is rebooted, but for some reason this is not communicated properly to my client so the connection just hangs waiting for a response for ever and ever. This is exactly the same behaviour seen with stale JDBC-connections, where some router in between server and client has found the connection to be idle and dropped it without notification.

I need to be able to reproduce this scenario to be able to create a proper fix.

Hence I need to know if there is a good way to simulate a router silently dropping connections under my control?

I can put a Linux box or a Mac between my development environment and the server. I can run the JVM in a virtual machine (vmware player) if I can do the dropping there.

Suggestions are very welcome.


EDIT 2012-12-18: The solution that worked for me was to use a Java-based SOCKS5-proxy locally which could be suspended with Ctrl-Z at will, and tell my application to go through it.

like image 361
Thorbjørn Ravn Andersen Avatar asked May 24 '11 20:05

Thorbjørn Ravn Andersen


People also ask

How do I keep my TCP IP connection alive?

When two hosts are connected over a network via TCP/IP, TCP Keepalive Packets can be used to determine if the connection is still valid, and terminate it if needed. Most hosts that support TCP also support TCP Keepalive. Each host (or peer) periodically sends a TCP packet to its peer which solicits a response.

How do I fix TCP connection failed?

Error message : SQL server Exception : The TCP/IP connection to the host port has failed. Verify the connection properties.. Make sure that the instance of SQL server is running on the host and accepting TCP/IP connnectionss at the port . Make sure that the TCP connecstions to the port are not blocked by a firewall.

How do you establish a TCP connection to terminate?

The common way of terminating a TCP connection is by using the TCP header's FIN flag. This mechanism allows each host to release its own side of the connection individually. Suppose that the client application decides it wants to close the connection. (Note that the server could also choose to close the connection).

How does TCP detect packet loss?

In TCP, the sender detects packet loss by receiving three duplicate acknowledgments or the expiration of retransmission timeouts and treats every loss as an indication of network congestion.


2 Answers

You should be able to temporarily apply a rule using iptables to drop all packets coming from the remote server (a.b.c.d). Something along the lines of :

iptables -A INPUT -s a.b.c.d -j DROP 

When you want to turn the filter off

iptables -D INPUT -s a.b.c.d -j DROP 

You can play with those rules to drop other specific packets as well, but I think this will accurately model what your client software would see if the server simply disappeared.

Note though, that when you turn the connection back on, the remote server may still have the old connection alive, so this won't perfectly simulate a reboot.

like image 177
hallsa Avatar answered Sep 25 '22 12:09

hallsa


Use Socat to forward your connection (it's very easy to set up as a TCP forwarder/proxy) even on the same machine if you want (by introducing a new port for the proxy to operate on and pointing it to the 'official' endpoint)

Check out the simple TCP forwarder example

Then when you want to test outages you can either kill it (and the socket will be closed) or potentially try stopping the process (CTRL Z linux) to see how your destination handles stalled connections.

Any questions? I'll try to answer... Good luck!

like image 28
HaveAGuess Avatar answered Sep 26 '22 12:09

HaveAGuess