I'm trying to build a test setup which test my server's performance under high load. In order to do that I want to open thousands of tcp connection. The problem is that I only have two hosts, one is for the server and the other is for the load generator.

Is there a way to do that?

  • The cross-post has been closed on SF. – terdon Mar 19 '15 at 15:11

Ok it is going to take two files.

1st file is a small script you will run on the server, it just opens a ton of listening ports using netcat

2nd file is a small script you will run on client. Make sure you change serverIP to the actual IP of your server.

Now keep in mind this is something I put together really fast. The port list isn't perfect, you should run both as root as some of the ports are reserved and a few will be in use but most will be valid. I ran it on my system and about 20 of the 2000 were in use already. So you can adjust but you get the idea. You can also increase the load by increasing the number of ports or even reading and writing data but this seems like what you wanted.

1st file named runoserver

for port in `seq 50000 52000`;
                netcat -l $port &

2nd file named runonclient

for port in `seq 50000 52000`;
                netcat serverIP $port &

EDIT- to respond to your comment question

It is not possible to have more than one connection open on the same port at the same time. If you are thinking about the way everyone accesses web servers on port 80 there is a little magic there. You access Google's web page by going to google and opening a connection to port 80. Google basically responds saying:

I'm Google-
This was a valid request-
Continue all communication on port X- 
Then your computer and Google's server opens port X for reading and writing.

Port X would be a non-reserved, not in use, high numbered port. ex: 25643

So you are not connected to port 80 for more than a second, through load balancing and other TCP/IP magic they are able to service millions of requests a second. Keep in mind that this is an overly simple explanation, it leaves out a number of details and steps but kinda explains how it seems there can be more than one connection to a port at once.

  • thanks, that's great. I wonder if I can trick the os to open many connection on the same port which will be closer to a real life scenario. – Ezra Mar 19 '15 at 15:53
  • while true ; do nc -l 80<<<httpData; done This would mimic an http server, in that the client would connect to prt 80, server will output the data in httpData, disconnect and reopen on port on 80 waiting for someone to connect and do the same thing until you kill the process. As usual if this question or any other questions worked for you please accept the answer so future members know what resolved your issue. – Dylan Mar 19 '15 at 16:22
  • I was thinking about your comment about tricking the OS into opening many connections on the same port. I edited my answer to explain what happens and what I think you meant. Let me know if that's what you wanted. – Dylan Mar 20 '15 at 7:56
  • you can use tcpsvd or tcpserver to accept many client connections at the same time on a single port. You cannot do this with netcat. – Michael Martinez Jul 1 at 20:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.