Netcat 服务器连接被拒绝

Netcat Server Connection Refused

使用 netcat,我需要创建一个简单的服务器来响应简单的 JSON 数据。

首先,我尝试使用 HTML 页面:

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

或者更简单:

netcat -l 8888 < index.html

然后在我的浏览器中我这样做:

http://localhost:8888/index.html

我也尝试使用 netcat 连接:

nc -vz 127.0.0.1 8888

其中每一个都会产生一个 'connection refused' 结果:

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

如何解决此错误?

在浏览器中去掉 index.html 试试看

$ netcat -l 8888 < index.html

在您的浏览器中使用http://localhost:8888

如果这不起作用,请尝试远程登录

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

我的 index.html 只包含字符串 'test'。如果无法建立连接,则问题是网络问题。

问题已解决。

我不得不使用

netcat -l -p 8888 < index.html

这样我就可以收听本地端口了。现在我正在工作。