Script to create multiple socket connections to multiple servers fast
<p>I have a list of server address in a file as below:</p>

<pre><code>192.168.1.100
192.168.1.101
192.168.1.102
...
192.168.1.200
</code></pre>

<p>I want to write a program which create multiple socket connections from one PC client to all these servers (using the same source IP, source port and destination port) in order to make my modem's NAT table full. </p>

<p>Could anyone suggest me the most efficient way to do that ? Because if I have a list of 7K server IP address, I expect the number of socket connection should be increase up to 7k in a fast way, for example, after 5 minutes (I just want to simulate a TCP attack). I wrote a python script but it's very slow regarding my expectation</p>

<p>You should be able to issue the 7K connects in a non-blocking fashion, then wait on them. Assuming they all succeed, the wait time of all of them will be overlapped. That should result in a much smaller overall delay.</p>

<p>In other words, try something like this:</p>

<pre><code>for (i = 0; i &lt; 7000; ++i) {
  // Create socket
  sock_array[i] = socket(PF_INET, SOCK_STREAM, 0);
  // Set socket non-blocking
  flags = fcntl(sock_array[i], F_GETFL, 0);
  fcntl(sock_array[i], F_SETFL, flags | O_NONBLOCK);
  // Do the connect
  connect(sock_array[i], &amp;sock_addr[i], sizeof sock_addr[i]);
}
for (i = 0; i &lt; 7000; ++i) {
  // Find out if connect completed.
  getsockopt(s, SOL_SOCKET, SO_ERROR, &amp;err, &amp;len);
  // Assuming err == 0, your connect to i-th host is done
}
</code></pre>

<p>In actuality, during the first loop, you would add each socket to an <code>fd_set</code> and use <code>select</code> to determine when each <code>connect</code> had completed (either successfully or unsuccessfully). The time required to create all the connections should be bounded by the connection that takes the longest to establish. (Add error handling, etc. to taste.)</p>

java python shell tcp network-programming