7

我试图通过 node.js 中的两个 NAT 打一个 TCP 洞。问题是我不知道如何选择连接应该使用的本地端口?

4

2 回答 2

5

创建与公共服务器的连接后,您还需要侦听用于建立该连接的完全相同的本地 (!!) 端口。

我将您的测试代码扩展为完整的 tcp 打孔概念证明:

// server.js
var server = require('net').createServer(function (socket) {
    console.log('> Connect to this public endpoint with clientB:', socket.remoteAddress + ':' + socket.remotePort);
}).listen(4434, function (err) {
    if(err) return console.log(err);
    console.log('> (server) listening on:', server.address().address + ':' + server.address().port)
});

// clientA.js
var c = require('net').createConnection({host : 'PUBLIC_IP_OF_SERVER', port : 4434}, function () {
    console.log('> connected to public server via local endpoint:', c.localAddress + ':' + c.localPort);

    // do not end the connection, keep it open to the public server
    // and start a tcp server listening on the ip/port used to connected to server.js
    var server = require('net').createServer(function (socket) {
        console.log('> (clientA) someone connected, it\s:', socket.remoteAddress, socket.remotePort);
        socket.write("Hello there NAT traversal man, this is a message from a client behind a NAT!");
    }).listen(c.localPort, c.localAddress, function (err) {
        if(err) return console.log(err);
        console.log('> (clientA) listening on:', c.localAddress + ':' + c.localPort);
    });
});

// clientB.js
// read the server's output to find the public endpoint of A:
var c = require('net').createConnection({host : 'PUBLIC_IP_OF_CLIENT_A', port : PUBLIC_PORT_OF_CLIENT_A},function () {
    console.log('> (clientB) connected to clientA!');

    c.on('data', function (data) {
        console.log(data.toString());
    });
});

有关在服务器上发生信号的更完整版本,我在这里参考我的代码:https ://github.com/SamDecrock/node-tcp-hole-punching

于 2016-02-21T17:43:38.093 回答
3

套接字被分配了一个本地端口。要重用相同的端口,您可以使用用于与服务器通信的相同套接字连接到客户端。这对您有用,因为您正在进行 TCP 打孔。但是,您不能自己选择端口。

这是一些测试代码:

// server.js
require('net').createServer(function(c) {
    c.write(c.remotePort.toString(10));
}).listen(4434);


//client.js
var c = require('net').createConnection({host : '127.0.0.1', port : 4434});
c.on('data', function(data) {
    console.log(data.toString(), c.localPort);
    c.end();
});

c.on('end', function() {
    c.connect({host : '127.0.0.1', port : 4434});
});
于 2014-02-10T18:58:18.760 回答