执行此代码:
var fs = require('fs');
var Socket = require('net').Socket;
var socket = new Socket();
console.log('connecting to: ' + server.host + ':' + server.port );
socket.connect( server.host, server.port );
socket.on('error', function(err) {
console.log(arguments);
});
socket.on('connect', function() {
console.log('connected');
});
socket.on('end', function() {
console.log('socket ended');
});
始终抛出此错误:
{ '0': { [Error: connect ENOENT] code: 'ENOENT', errno: 'ENOENT', syscall: 'connect' } }
我使用基于CloudLinux(x64)的SSH访问共享主机。
答案 0 :(得分:15)
你的主机和端口倒退了。根据{{3}},您应该这样做:
socket.connect(server.port, server.host);
答案 1 :(得分:2)
服务器端: -
var net = require('net');
var server = net.createServer(functi`on (socket){
socket.write("hi\n");
socket.write("you there\n");
socket.on("data", function(dd) {
console.log(data);
});
});
server.listen(8001);
客户端: -
var fs = require('fs');
var Sock = require('net');
var socket = Sock.Socket();
socket.connect(8001,"127.0.0.1", function() {
console.log('connecting to: ' + server.host + ':' + server.port );
});
socket.on('connect', function() {
console.log('connected');
});
socket.on('error', function(err) {
console.log(arguments);
});
socket.on('end', function() {
console.log('socket ended');
});