I have a shell script that uses netcat to listen to localhost on port 1111 for web requests. Every time I try accessing localhost:1111/index.html for example I get:

invalid connection to [] from localhost [] 60038

the number at the end (60038) seems to be increasing every time I access localhost.

Any suggestions on what's going wrong? And what is the default localhost directory? Where should I put an index.html so that localhost:1111/index.html would work?


here is the full script:

while true
netcat -vvl localhost -p 1111 -c '
    set -x
    read http_request
    echo HTTP/1.0 200 OK
    echo "Received HTTP request: $http_request"
  • The number at the end is the source port, which is randomly chosen; that's the port used by your browser to open the outgoing request. Don't worry about it. Can you post your netcat command line? Is the 'invalid connection' message coming from netcat or another tool? – mrb Aug 8 '12 at 12:10
  • I just added the full script. I'm not sure if netcat is giving the message, it may be set. I'm not sure what set even does though! – MinaHany Aug 8 '12 at 13:43
  • That script is the listening side. Can you also post the client side? – jw013 Aug 8 '12 at 14:02

Your original script requires that the connection comes from a host named localhost, but for some reason that filtering is failing. Unusual, because it matches exactly the name listed in the error: invalid connection to [] from localhost [] 60038

This command will listen on the localhost network interface (and will ignore requests from other interfaces, like your LAN):

netcat -vvl -s localhost -p 1111 -c '
    set -x
    read http_request
    echo HTTP/1.0 200 OK
    echo "Received HTTP request: $http_request"

If you want to listen for requests on all interfaces, you can drop the -s part altogether:

netcat -vvl -p 1111 -c '...'

On my system, if I want to do the same kind of source host filtering without -s, I need to use either or localhost.localdomain:

netcat -vvl localhost.localdomain -p 1111 -c '...'

netcat -vvl -p 1111 -c '...'

In any case, one of the above options should work for you:

$ netcat -vvl -p 1111 -c '
quote>     set -x
quote>     read http_request
quote>     echo HTTP/1.0 200 OK
quote>     echo
quote>     echo "Received HTTP request: $http_request"
quote> '
listening on [any] 1111 ...
connect to [] from localhost.localdomain [] 35368
+ read http_request
+ echo HTTP/1.0 200 OK
+ echo
+ echo Received HTTP request: GET / HTTP/1.1
  • Thanks a lot! changing localhost to seemed to do the trick on firefox. Chrome is handling things differently I guess. – MinaHany Aug 9 '12 at 9:42

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.