UDP Out Node is not bound to correct port number

#1

Hi all,

I am new to this. So please forgive me if I ask a silly question.

I created a simple UDP ping-pong function:
1. Endpoint A (port 3000) sends a Ping to Endpoint B (port 3001)
2. Endpoint B (port 3001) replies a Pong to Endpoint A (port 3000)

[{"id":"725b7ffb.59aa","type":"inject","z":"e559e2e2.79d9a","name":"Send Ping","topic":"","payload":"Ping","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":400,"y":1760,"wires":[["c9181d5.3ccece"]]},{"id":"6fe3ee5a.ffadf","type":"udp out","z":"e559e2e2.79d9a","name":"Send from port 3000","addr":"","iface":"","port":"","ipv":"udp4","outport":"3000","base64":false,"multicast":"false","x":920,"y":1700,"wires":[]},{"id":"71434c52.25bea4","type":"udp in","z":"e559e2e2.79d9a","name":"Listen on port 3000","iface":"","port":"3000","ipv":"udp4","multicast":"false","group":"","datatype":"utf8","x":110,"y":1600,"wires":[["8cdf279f.2a9148","ed3ba85b.cea8b8"]]},{"id":"8cdf279f.2a9148","type":"debug","z":"e559e2e2.79d9a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":390,"y":1560,"wires":[]},{"id":"ed3ba85b.cea8b8","type":"delay","z":"e559e2e2.79d9a","name":"Delay 5s","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":400,"y":1640,"wires":[["c9181d5.3ccece"]]},{"id":"ea6dce7b.5d07d","type":"inject","z":"e559e2e2.79d9a","name":"Send Pong","topic":"","payload":"Pong","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":400,"y":2200,"wires":[["7ddf4466.e25d7c"]]},{"id":"93f14deb.fb968","type":"udp out","z":"e559e2e2.79d9a","name":"Send from port 3001","addr":"","iface":"","port":"","ipv":"udp4","outport":"3001","base64":false,"multicast":"false","x":840,"y":2140,"wires":[]},{"id":"f002937.fa9307","type":"udp in","z":"e559e2e2.79d9a","name":"Listen on port 3001","iface":"","port":"3001","ipv":"udp4","multicast":"false","group":"","datatype":"utf8","x":110,"y":2020,"wires":[["63d71c31.13bf34","ef120942.f7ef88"]]},{"id":"63d71c31.13bf34","type":"debug","z":"e559e2e2.79d9a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":390,"y":1960,"wires":[]},{"id":"ef120942.f7ef88","type":"delay","z":"e559e2e2.79d9a","name":"Delay 5s","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":400,"y":2080,"wires":[["7ddf4466.e25d7c"]]},{"id":"c9181d5.3ccece","type":"function","z":"e559e2e2.79d9a","name":"To Endpoint B on port 3001","func":"msg.ip = \"localhost\";\nmsg.port = 3001;\nreturn msg;","outputs":1,"noerr":0,"x":640,"y":1700,"wires":[["6fe3ee5a.ffadf"]]},{"id":"7ddf4466.e25d7c","type":"function","z":"e559e2e2.79d9a","name":"Reply with Pong","func":"let message = {\"ip\":msg.ip, \"port\": msg.port, \"payload\": \"Pong\"}\nreturn message;","outputs":1,"noerr":0,"x":600,"y":2140,"wires":[["93f14deb.fb968"]]},{"id":"2c812ae9.c673d6","type":"comment","z":"e559e2e2.79d9a","name":"Endpoint A","info":"","x":80,"y":1560,"wires":[]},{"id":"a21bd587.ca1008","type":"comment","z":"e559e2e2.79d9a","name":"Endpoint B","info":"","x":80,"y":1980,"wires":[]}]

I expect Endpoint A receives a Pong from Endpoint B (port 3001) which is specified in Endpoint B's UDP Out. However, Endpoint A actually shows Pong is from port 3000 which is wrong. What did I do wrong?

Node Red: 0.18.7
Nodejs: 8.11.2
OS: Ubuntu 16.04

Thanks for help.

Cheers,
Paul

#2

Hello Paul

I have some know-how in networking. In my view it is not useful to set the sending port in the UDP out nodes. Leave it to the OS to decide, therefore set the config to ‘random’.

I have crappy know-how in Javascript. The first function node seems to work. The Ping gets out. But the second function node ‘eats’ the incoming message, nothing gets out. Therefore I strongly suggest to just copy the code from the one above, since it works.
BTW: the change node does the same thing, you do not need a function node for what you want to achieve.

Kind regards,

Urs.

1 Like
#3

Indeed, it is like Urs said. The "ping" string in msg.payload gets lost as it is not reconfigured by the first function node. The "pong" keeps circulating forever. Amending the function code fixes the issue. Therefore nothing seems wrong with UDP port assignment and use.

msg.ip = "localhost";
msg.port = 3001;
msg.payload = "ping";
return msg;

Before:

After (click the picture to enlarge) :

#4

Hi Urs and Andrei,

Thanks a lot for your reply.

Andrei, I made the changes you mentioned. But unlike yours, my node red still shows that Pong message is sent from port 3000 (which should be 3001).

Can I ask what is your OS, nodejs and node-red version?

Thanks a lot!

Cheers,
Paul

#5

Hello Paul,

Really interesting (and puzzling). Perhaps something specific to the OS ?

Here my configuration:

14 Jun 00:13:01 - [info] Node-RED version: v0.18.4
14 Jun 00:13:01 - [info] Node.js version: v8.11.1
14 Jun 00:13:01 - [info] Windows_NT 10.0.17134 x64 LE

What happens if you fix the destination ports in both UDP output nodes ? Like below for instance ?

the code I test (just in case):

[{"id":"1b03db00.66de75","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"c9229d.46030d6","type":"inject","z":"1b03db00.66de75","name":"Send Ping","topic":"","payload":"Ping","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":422.50001525878906,"y":290.00000762939453,"wires":[["53f754c1.c89fbc"]]},{"id":"4394e591.ad717c","type":"udp out","z":"1b03db00.66de75","name":"Send from port 3000","addr":"","iface":"","port":"","ipv":"udp4","outport":"3000","base64":false,"multicast":"false","x":942.5000152587891,"y":230.00000762939453,"wires":[]},{"id":"865c646e.e911d8","type":"udp in","z":"1b03db00.66de75","name":"Listen on port 3000","iface":"","port":"3000","ipv":"udp4","multicast":"false","group":"","datatype":"utf8","x":132.50001525878906,"y":130.00000762939453,"wires":[["c9b498fe.0b81f8","5c9bc1e6.6d33f"]]},{"id":"c9b498fe.0b81f8","type":"debug","z":"1b03db00.66de75","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":412.50001525878906,"y":90.00000762939453,"wires":[]},{"id":"5c9bc1e6.6d33f","type":"delay","z":"1b03db00.66de75","name":"Delay 5s","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":373.50000762939453,"y":164.00001430511475,"wires":[["53f754c1.c89fbc"]]},{"id":"d68c1e8d.fdd5a","type":"inject","z":"1b03db00.66de75","name":"Send Pong","topic":"","payload":"Pong","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":422.50001525878906,"y":730.0000076293945,"wires":[["ddce1a7b.0987d8"]]},{"id":"357fdcf3.b41de4","type":"udp out","z":"1b03db00.66de75","name":"Send from port 3001","addr":"","iface":"","port":"","ipv":"udp4","outport":"3001","base64":false,"multicast":"false","x":862.5000152587891,"y":670.0000076293945,"wires":[]},{"id":"c812eac6.7eb388","type":"udp in","z":"1b03db00.66de75","name":"Listen on port 3001","iface":"","port":"3001","ipv":"udp4","multicast":"false","group":"","datatype":"utf8","x":132.50001525878906,"y":550.0000076293945,"wires":[["f2f3978.792d468","189ce106.e5af9f"]]},{"id":"189ce106.e5af9f","type":"debug","z":"1b03db00.66de75","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":412.50001525878906,"y":490.00000762939453,"wires":[]},{"id":"f2f3978.792d468","type":"delay","z":"1b03db00.66de75","name":"Delay 5s","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":422.50001525878906,"y":610.0000076293945,"wires":[["ddce1a7b.0987d8"]]},{"id":"53f754c1.c89fbc","type":"function","z":"1b03db00.66de75","name":"To Endpoint B on port 3001","func":"msg.ip = \"localhost\";\nmsg.port = 3001;\nmsg.payload = \"ping\";\nreturn msg;","outputs":1,"noerr":0,"x":662.5000152587891,"y":230.00000762939453,"wires":[["4394e591.ad717c"]]},{"id":"ddce1a7b.0987d8","type":"function","z":"1b03db00.66de75","name":"Reply with Pong","func":"let message = {\"ip\":msg.ip, \"port\": msg.port, \"payload\": \"pong\"}\nreturn message;","outputs":1,"noerr":0,"x":622.5000152587891,"y":670.0000076293945,"wires":[["357fdcf3.b41de4"]]},{"id":"d07d24f8.f12dc8","type":"comment","z":"1b03db00.66de75","name":"Endpoint A","info":"","x":102.50001525878906,"y":90.00000762939453,"wires":[]},{"id":"a4e9e294.cc4f8","type":"comment","z":"1b03db00.66de75","name":"Endpoint B","info":"","x":102.50001525878906,"y":510.00000762939453,"wires":[]}]
#6

Paul,

I broght back to life an SBC, configured with Ubuntu ( 14.04.3), that was unused for nearly 3 years (just for testing UDP).

As you can imagine the Node-RED and the Node.JS are quite old.

14 Jun 14:40:49 - [info] Node-RED version: v0.13.1
14 Jun 14:40:49 - [info] Node.js version: v4.2.6

I tested your flow, using only hard-coded ports in the configuration node (therefore not using msg.ip and msg.port for that purpose). The results are a surprise for me. The communication simply does not occur when I configure the UDP output bound to the same port as the UDP input node, like pictured below:

When I bound a different UDP port for each flow (therefore 4 different UDP ports) it worked well.

Honestly speaking I don't know very well how the port binding works in linux but I thought it was legitimate to use the same UDP port for listening and for sending messages.

The very same config that is not working in Ubuntu is working on windows. I got an warning in the debug side panel (like below) but the communication works.

udp-04

#7

Thee have been several fixes to do with port binding since version 0.13
Please upgrade

2 Likes
#9

Hi all,

I have done some test using Andrei’s code with different version Node-Red, here is the result:

  • v0.18.7: buggy, Endpoint B sends messages from port 3000 (3001 expected)
  • v0.18.6: buggy, As Above
  • v0.18.5: buggy, As Above
  • v0.18.4: No Issue, Endpoint B sends messages from port 3001
  • v0.18.3: No Issue, As Above

Probably introduced in 0.18.5. I suspect was this issue: https://github.com/node-red/node-red/pull/1686

My envitonment:
Nodejs: 8.11.3
OS: Ubuntu 16.04

I will create a issue in github for this.

Thanks for all help!

Cheers,
Paul

#10

yes please. Does indeed look like a bug.