How do I check how many connections are open currently on a specific TCP port?

I'm doing some comet benchmarks and would like to see how many open connections I have.

Actually I use netstat:

netstat -ant | grep 8080 | grep EST | wc -l

But it needs around 4-6 minutes to list the number, is there any tool that can do show it in real time? The number of open connections is between 100'000 - 250'000.


Don't know if lsof is better, but give this a try:

lsof -ni:8080 -sTCP:ESTABLISHED | wc -l

If you just need to see connecton statistics, try ss utility from iproute suite:

# ss -s
Total: 1788 (kernel 3134)
TCP:   1638 (estab 1409, closed 162, orphaned 0, synrecv 0, timewait 127/0), ports 0

Transport Total     IP        IPv6
*         3134      -         -        
RAW       0         0         0        
UDP       74        69        5        
TCP       1476      1444      32       
INET      1550      1513      37       
FRAG      0         0         0     

You also can view detailed information on all established connections like this:

ss -n state established

…or ssh connections only:

ss -n state established '( dport = :ssh or sport = :ssh )'

Some numbers section at the bottom of this page may also interest you.


Another option would be to read /proc/net/tcp directly. To see all established TCP connections on, 8080, you would do something like

$ printf %04X 8080
1F90
$ grep :1F90 /proc/net/tcp | grep ' 01 ' | wc -l

If you want to do it in a single process (less IO overhead) and handle corner cases, the following tells you how many ESTABLISHED TCP connections have local port 8080:

$ perl -anle '
          $F[1] =~ /:1F90/ and $F[3] =~ /01/ and $cnt++;
          END { print 0+$cnt }
         '  /proc/net/tcp

If the software on your machine listening on 8080 has IPv6 support, you'll need to read /proc/net/tcp6 also; if the program's using IPv6 sockets, connections will show up there even if they're using IPv4.