Take the 2-minute tour ×

I've had a bug in our software that occurs when I receive a connection timeout. These errors are very rare (usually when my connection gets dropped by our internal network). How can I generate this kind of effect artificially so I can test our software?

If it matters the app is written in C++/MFC using CAsyncSocket classes.


I've tried using a non-existant host, and I get the socket error:

WSAEINVAL (10022) Invalid argument

My next attempt was to use Alexander's suggestion of connecting to a different port, e.g. 81 (on my own server though). That worked great. Exactly the same as a dropped connection (60 second wait, then error). Thank you!

share|improve this question

14 Answers 14

up vote 73 down vote accepted

Connect to an existing host but to a port that is blocked by the firewall that simply drops TCP SYN packets. For example, www.google.com:81.

share|improve this answer
emu's response below works better. –  Ryan Detzel May 27 at 18:50
This answer is simple and works just like @emu's answer below. I understand this answer didn't intend to suggest using google.com:81 but the point here is to use a different port that is blocked. So you can always use <your-own-ip>:<blocked-port>. –  James Selvakumar Aug 14 at 3:58

Connect to a non-routable IP address, such as

share|improve this answer
This worked for me. –  Jesse Wilson Jul 21 '10 at 18:04
Idem, and I guess this is a better answer since google.com:81 might be reachable one day. –  Gui13 Jan 27 '12 at 15:00
... and because sending random packets to other people's servers from your unit tests is rude. –  Glenn Maynard Oct 7 '12 at 20:45
This won't always work. For instance, with Python's urllib, this will return a 'No route to host' exception. FYI –  Mike Feb 22 '13 at 22:35
May be it isn't the most universal but the simplest solution! –  Konstantin Smolyanin Aug 31 '13 at 13:16

How about a software solution:

Install SSH server on the application server. Then, use socket tunnel to create a link between your local port and the remote port on the application server. You can use ssh client tools to do so. Have your client application connect to your mapped local port instead. Then, you can break the socket tunnel at will to simulate the connection timeout.

share|improve this answer

I would like to point everybody's attention to pathod

With a config (taken from their examples) of 200:b@100:dr you'll get a connection that randomly drops.

share|improve this answer

There are a couple of tactics I've used in the past to simulate networking issues;

  1. Pull out the network cable
  2. Switch off the switch (ideally with the switch that the computer is plugged into still being powered so the machine maintains it's "network connection") between your machine and the "target" machine
  3. Run firewall software on the target machine that silently drops received data

One of these ideas might give you some means of artifically generating the scenario you need

share|improve this answer

You might install Microsoft Loopback driver that will create a separate interface for you. Then you can connect on it to some service of yours (your own host). Then in Network Connections you can disable/enable such interface...

share|improve this answer

Despite it isn't completely clear which one the OP wants to test: there's a difference between attempting a connection to a non-existent host/port and a timeout of an already established connection. I would go with Rob and wait until the connection is working and then pull the cable. Or - for convenience - have a virtual machine working as the test server (with bridged networking) and just deactivating the virtual network interface once the connection is established.

share|improve this answer

You can use the Python REPL to simulate a timeout while receiving data (i.e. after a connection has been established successfully). Nothing but a standard Python installation is needed.

Python 2.7.4 (default, Apr  6 2013, 19:54:46) [MSC v.1500 32 bit (Intel)] on win32
>>> import socket
>>> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)        
>>> s.bind(('localhost', 9000))
>>> (clientsocket, address) = s.accept()

Now it waits for an incoming connection. Connect whatever you want to test to localhost:9000. When you do, Python will accept the connection and accept() will return it. Unless you send any data through the clientsocket, the caller's socket should time out during the next recv().

share|improve this answer

Plug in your network cable into a switch which has no other connection/cables. That should work imho.

share|improve this answer

Depending on what firewall software you have installed/available, you should be able to block the outgoing port and depending on how your firewall is setup it should just drop the connection request packet. No connection request, no connection, timeout ensues. This would probably work better if it was implemented at a router level (they tend to drop packets instead of sending resets, or whatever the equivalent is for the situation) but there's bound to be a software package that'd do the trick too.

share|improve this answer

You can try to connect to one of well-known Web sites on a port that may not be available from outside - 200 for example. Most of firewalls work in DROP mode and it will simulate a timeout for you.

share|improve this answer

I had issues along the same lines you do. In order to test the software behavior, I just unplugged the network cable at the appropriate time. I had to set a break-point right before I wanted to unplug the cable.

If I were doing it again, I'd put a switch (a normally closed momentary push button one) in a network cable.

If the physical disconnect causes a different behavior, you could connect your computer to a cheap hub and put the switch I mentioned above between your hub and the main network.

-- EDIT -- In many cases you'll need the network connection working until you get to a certain point in your program, THEN you'll want to disconnect using one of the many suggestions offered.

share|improve this answer

I like to use CurrPorts to manipulate network connections when debugging. Sometimes, just unplugging the network cable will do just as fine (as posted by others).

share|improve this answer
With CurrPorts, I seem to only be able to close the connection (which causes the next recv() to fail immediately), but could not find a way to simulate a timeout (i.e. no more data is transferred, but the connection stays open). –  hheimbuerger Apr 16 '13 at 21:55

The easiest thing would be to drop your connection using CurrPorts.

However, in order to unit test your exception handling code, perhaps you should consider abstracting your network connection code, and write a stub, mock or decorator which throws exceptions on demand. You will then be able to test the application error-handling logic without having to actually use the network.

share|improve this answer

Your Answer


