One tcp requests with multiply source port

Hi All,

I wrote a flow that requires me to send multiple tcp requests (because i need a different source port for each request).
For example, I have attached a flow that simulates what I need (not my own).
In the attached flow, you will see two TPC request nodes, but in my flow, I will not know how many TCP requests I will need to send.
The number may be 3, or 100, so I can't put a fixed node of TCP request.
In addition, in my flow the server side will be out of the node-red.
I would appreciate some assistance with this issue.

Node-RED version: v2.0.5
Node.js version: v14.16.1

[{"id":"fd6b4ec4ec65ba97","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"94b9a9fa27c8ba18","type":"tcp in","z":"fd6b4ec4ec65ba97","name":"","server":"server","host":"","port":"230","datamode":"stream","datatype":"buffer","newline":"","topic":"","base64":false,"x":190,"y":380,"wires":[["426a7c1866b113fc","a78e5b92d3a63ed3"]]},{"id":"9b2685d01514d819","type":"tcp request","z":"fd6b4ec4ec65ba97","server":"localhost","port":"230","out":"sit","splitc":" ","name":"","x":450,"y":140,"wires":[["900e21e0716caa6a"]]},{"id":"bcc1f4e097dc02a3","type":"inject","z":"fd6b4ec4ec65ba97","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[49,50,51]","payloadType":"bin","x":190,"y":120,"wires":[["9b2685d01514d819"]]},{"id":"8fd1844d1d984120","type":"debug","z":"fd6b4ec4ec65ba97","name":"","active":true,"tosidebar":true,"console":true,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":590,"y":380,"wires":[]},{"id":"8daf933281d13657","type":"inject","z":"fd6b4ec4ec65ba97","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[50,51,52,53,54,55]","payloadType":"bin","x":190,"y":160,"wires":[["9b2685d01514d819"]]},{"id":"be2cfbe0a52200b9","type":"tcp request","z":"fd6b4ec4ec65ba97","server":"localhost","port":"230","out":"sit","splitc":" ","name":"","x":450,"y":220,"wires":[["900e21e0716caa6a"]]},{"id":"9a30bffe493dd97d","type":"inject","z":"fd6b4ec4ec65ba97","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[97,98,118,102,102,100]","payloadType":"bin","x":190,"y":220,"wires":[["be2cfbe0a52200b9"]]},{"id":"5ac5c5c72696ca49","type":"tcp out","z":"fd6b4ec4ec65ba97","host":"","port":"","beserver":"reply","base64":false,"end":false,"name":"","x":690,"y":460,"wires":[]},{"id":"2443e5a47c870746","type":"function","z":"fd6b4ec4ec65ba97","name":"","func":"msg.payload[0] = 7;\nconsole.log(\"payload\"+JSON.stringify(msg));\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":530,"y":460,"wires":[["5ac5c5c72696ca49"]]},{"id":"426a7c1866b113fc","type":"delay","z":"fd6b4ec4ec65ba97","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"x":370,"y":460,"wires":[["2443e5a47c870746"]]},{"id":"c0454b5e2f0c4cd7","type":"debug","z":"fd6b4ec4ec65ba97","name":"","active":true,"tosidebar":true,"console":true,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":850,"y":180,"wires":[]},{"id":"a78e5b92d3a63ed3","type":"function","z":"fd6b4ec4ec65ba97","name":"Request print","func":"console.log(\"Request\");\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":390,"y":380,"wires":[["8fd1844d1d984120"]]},{"id":"900e21e0716caa6a","type":"function","z":"fd6b4ec4ec65ba97","name":"Response print","func":"console.log(\"Response\");\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":650,"y":180,"wires":[["c0454b5e2f0c4cd7"]]},{"id":"649b8a2da645ceb8","type":"comment","z":"fd6b4ec4ec65ba97","name":"Server","info":"","x":190,"y":320,"wires":[]},{"id":"14e92ddbacbe33e1","type":"comment","z":"fd6b4ec4ec65ba97","name":"Client","info":"","x":190,"y":80,"wires":[]}]

Thanks for your help!

As long as you close the connection each time I think the tcp client should pick a new random port each time. If not It is down deep inside the nodejs tcp implementation so would need to get exposed somehow.

Hi thanks for the quick reply.
I can't close the connection, I need several connections connected at the same time. Are you saying that the only way is to enter to the code of tcp request node?

Yes, by default each connection assigns a random local port. There seems to be some debate as to whether specifying the socket localport actually works or not. Even if so you would need one node per connection if you need to leave them open.

I had similar issues with multiple TCP ports.
In the past it seemed fixed:

But most recently i have started using virtual com software, which makes the code less hard to duplicate when switching ipadresses.

If you are running linux you could use socat. Getting started with socat, a multipurpose relay tool for Linux

On windows you couls use USR vcom or Lantronix CPR

Ok, Thanks I will try to edit the code of the TCP node.
Another question: How can I initiate a TCP request disconnect.

Hi, thanks for the reply.
I'm working with Windows, However, I cannot understand how USR vcom or Lantronix CPR would help me with my problem.

depends what you mean... currently the end user has the choice of waiting for a character, a number of characters, a timeout, never or immediately. If you are the programmer then if you are a client then you can .close() the socket or if the server then you may need .destroy() instead.

i'm on "never-keep connection open" in the tcp request, and I want to initiate the disconnection from the client not from the server.. how can i use .close() to close the connection? i didn't know that tcp request have .close()..
I only know that there is a .close in TCP client node but it didn't work for me...

the node doesn't have a disconnect or close capability from the outside msg as currently implemented... I thought you said you were going to edit the code ....

Sorry you are right I misread and thought you just wanted to open multiple connections. But as i understand now you want to change the port with incoming messages.

Maybe you can use cgywin and socat, and control it through exec nodes. But it is a bit of a work around.

I found 2 nodes that might work, not a request but a client ans server. They look more configurable than the standard tcp nodes.

links to github

https://github.com/tiagordc/node-red-contrib-tcp-client/blob/master/README.md

https://github.com/tiagordc/node-red-contrib-tcp-server

Yes i will edit the code, I thought there was a built-in disconnect.

Thanks but I try tcp client and server nodes and they did not work. I will check the first option you suggest.