
[英]How to connect to tcp server via node.js/socket.io from client?
[英]how to connect a ruby TCP server through a node net socket?
这是我的ruby服务器:
require "socket"
server=TCPServer.open(2000)
loop{
puts "wait for connect"
client=server.accept
puts "connect"
client.puts(Time.now.ctime)
client.close
}
我写了一个很好的Ruby客户端:
require "socket"
s=TCPSocket.open("localhost",2000)
while line=s.gets
puts line.chop
end
s.close
但是当我想改用节点套接字时,出现Error: connect ECONNREFUSED
。
这是我的节点代码:
var client, net;
net = require("net");
client = net.createConnection(2000);
console.log("connected");
client.on("data", function (data) {
console.log(data);
});
client.on("end", function () {
return console.log("client closed");
});
我做错了什么吗?
您无法两次打开端口,如果运行服务器,则无法在该端口上连接到localhost。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.