Node.js net.Socket().connect() 挂起并且无法连接到服务器
我使用 Node.js 的 net
模块设置了服务器和客户端。
这是我的服务器代码:
const server = net.createServer(function(socket) {
socket.on("data", function(data) {
// Do stuff with the connection
socket.end();
});
});
server.listen(this.port);
这是我的客户端代码:
const client = new net.Socket();
client.connect(this.port, this.host, function() {
client.write("Test Message");
});
当在同一台计算机上运行时,两者都工作正常,但是当我尝试从另一台计算机连接时,客户端挂在 client.connect() 永远(直到我终止进程)。当我尝试在命令提示符中使用 telnet 命令时,我没有收到服务器的响应,但客户端不仅仅说连接被拒绝。这是怎么回事?
I have a server and a client set up using the net
module for Node.js.
Here is my server's code:
const server = net.createServer(function(socket) {
socket.on("data", function(data) {
// Do stuff with the connection
socket.end();
});
});
server.listen(this.port);
Here is my client's code:
const client = new net.Socket();
client.connect(this.port, this.host, function() {
client.write("Test Message");
});
When run on the same machine, both of these work fine, but when I try to connect from a different computer, the client hangs at client.connect()
forever (until I terminate the process). When I tried to use the telnet
command in command prompt, I don't get a response from the server, but the client doesn't just say connection refused. What's going on here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论