#nodejs #tcp
Пытаюсь создать простейший TCP server (node.js)
Собственно с кодом вроде проблем нет:
var net = require('net');
var HOST = '0.0.0.0';
var PORT = 6969;
// Create a server instance, and chain the listen function to it
// The function passed to net.createServer() becomes the event handler for the 'connection'
event
// The sock object the callback function receives UNIQUE for each connection
net.createServer(function(sock) {
// We have a connection - a socket object is assigned to the connection automatically
console.log('CONNECTED: ' + sock.remoteAddress +':'+ sock.remotePort);
// Add a 'data' event handler to this instance of socket
sock.on('data', function(data) {
console.log('DATA ' + sock.remoteAddress + ': ' + data);
// Write the data back to the socket, the client will receive it as data
from the server
sock.write('You said "' + data + '"');
});
// Add a 'close' event handler to this instance of socket
sock.on('close', function(data) {
console.log('CLOSED: ' + sock.remoteAddress +' '+ sock.remotePort);
});
}).listen(PORT, HOST);
console.log('Server listening on ' + HOST +':'+ PORT);
Проблема в том, что это надо разместить на VPS (Ubuntu 14 (64-bit). Все попытки послать
пакет по адресу x.x.x.x:6969 не дают результата.
Сервер стартую командой nodejs file.js. Печатает подтверждение (Server listening
on...) и тишина.
Ответы
Ответ 1
iptables -I INPUT -p tcp -m tcp --dport 6969 -j ACCEPT решило проблему
Комментариев нет:
Отправить комментарий