简体   繁体   中英

Cannot send UDP message between 2 local computers with node.js

I have a UDP server on one machine and a client on the another. I am not able to receive a message sent by a client on the server machine over the local network or through the internet. My ultimate goal is to have the server on my internal network and the client on a hosted virtual server in the cloud.

When I run the same server and client on the same machine they talk perfectly. The firewall on the ubuntu server hosting the UDP server is inactive.

sudo ufw status 
Status: inactive

The port(31091) appears to be listening on the UDP server

(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      -
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -
tcp6       0      0 :::21                   :::*                    LISTEN      -
tcp6       0      0 :::22                   :::*                    LISTEN      -
udp        0      0 127.0.0.53:53           0.0.0.0:*                           -
udp        0      0 192.168.10.23:68        0.0.0.0:*                           -
udp        0      0 127.0.0.1:31091         0.0.0.0:*                           2351/node /home/ubu
udp6       0      0 fe80::ba27:ebff:fea:546 :::*                                -

Does it need to be bound to the machine internal ip instead of 127.0.0.1?

This is the code I am using to send a test...again if the server is on the same machine this program works perfectly by just changing the address to localhost.

var dgram = require('dgram');
var s = dgram.createSocket('udp4');
s.send(Buffer.from('this is a test'), 31091, '192.168.10.23');

On the server side the program is larger, but I think this all all the code relative to the UDP server

//UDP Server
var PORT = 31091;
var HOST = '127.0.0.1';
var dgram = require('dgram');
var server = dgram.createSocket('udp4');

server.on('listening', function() {
  var address = server.address();
 console.log('UDP Server listening on ' + address.address + ':' + address.port);
});


server.bind(PORT, HOST);
//END UDP Server

I feel like I am missing something simple, but I am still learning how this all works together. Any help is much appreciated.

Changing the UDP server address to 0.0.0.0 instead of 127.0.0.1 did the trick.

//UDP Server
var PORT = 31091;
var HOST = '0.0.0.0';
var dgram = require('dgram');
var server = dgram.createSocket('udp4');

server.on('listening', function() {
  var address = server.address();
 console.log('UDP Server listening on ' + address.address + ':' + address.port);
});


server.bind(PORT, HOST);
//END UDP Server

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM