Netcat Server Connection Refused
Asked Answered
B

2

5

Using netcat, I need to create a simple server to respond with simple JSON data.

First I am trying with an HTML page:

while true ; do nc -l 8888  < index.html ; done

or even more simply:

netcat -l 8888 < index.html

And then in my browser I do this:

http://localhost:8888/index.html

I also try to connect using netcat:

nc -vz 127.0.0.1 8888

Each of these yields a 'connection refused' result:

localhost [127.0.0.1] 8888 (ddi-tcp-1): Connection refused

How do I resolve this error?

Bevel answered 27/4, 2015 at 10:10 Comment(1)
which os? BSD and GNU/Linux netcats are quite differentGobert
B
11

The problem was resolved.

I had to use

netcat -l -p 8888 < index.html

This way I can listen to a localport. Now I it is working.

Bevel answered 27/4, 2015 at 11:10 Comment(3)
This solved a big issue for me when two android devices wouldnt communicate with each other. Adding the -p fixed it.Rosiorosita
But it is an error to use -l in conjunction with -p: linux.die.net/man/1/ncBowse
@MarvinH. there is a ton of different implementations of nc which have incompatible command line arguments.Stead
T
0

Try it without the index.html in your browser

$ netcat -l 8888 < index.html

In your browser use http://localhost:8888

If this does not work, try to telnet

$ telnet localhost 8888
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
 test
Connection closed by foreign host.

My index.html just contains the string 'test'. If a connection cannot be established the problem is a network problem.

Tweeny answered 27/4, 2015 at 10:15 Comment(4)
Hi. That was what I tried. It doesn't work. It's a simple thing, can't figure why it doesn't work...Thanks anywayBevel
You ommited the index.html in the browser and it didn't work? It works on my machine. It could be your environment, firewall, network setting ...Tweeny
Yes, i've tried it without index.html. What could be the problem then?Bevel
Thanks, I can't really see what is happening wrong. I will try to see if I can resolve this...If anyone else has some idea, I would appreciate.Bevel

© 2022 - 2024 — McMap. All rights reserved.