# One tcp requests with multiply source port

**URL:** https://discourse.nodered.org/t/one-tcp-requests-with-multiply-source-port/50629
**Category:** General
**Created:** [5 September 2021 09:09 UTC](https://discourse.nodered.org/t/one-tcp-requests-with-multiply-source-port/50629 "2021-09-05T09:09:03Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![bar-shiratech](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bar-shiratech/32/47204_2.png) [@bar-shiratech](https://discourse.nodered.org/u/bar-shiratech)
#### Post date: [5 September 2021 09:09 UTC](https://discourse.nodered.org/t/one-tcp-requests-with-multiply-source-port/50629/1 "2021-09-05T09:09:03Z")

</div>

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

```auto
[{"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!

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [5 September 2021 09:50 UTC](https://discourse.nodered.org/t/one-tcp-requests-with-multiply-source-port/50629/2 "2021-09-05T09:50:39Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![bar-shiratech](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bar-shiratech/32/47204_2.png) [@bar-shiratech](https://discourse.nodered.org/u/bar-shiratech)
#### Post date: [5 September 2021 10:15 UTC](https://discourse.nodered.org/t/one-tcp-requests-with-multiply-source-port/50629/3 "2021-09-05T10:15:12Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [5 September 2021 10:50 UTC](https://discourse.nodered.org/t/one-tcp-requests-with-multiply-source-port/50629/4 "2021-09-05T10:50:22Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![PiMo131](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pimo131/32/34978_2.png) [@PiMo131](https://discourse.nodered.org/u/PiMo131)
#### Post date: [5 September 2021 14:46 UTC](https://discourse.nodered.org/t/one-tcp-requests-with-multiply-source-port/50629/5 "2021-09-05T14:46:13Z")

</div>

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

> [@How Setup For Multiple Port TCP Request](https://discourse.nodered.org/t/how-setup-for-multiple-port-tcp-request/16189/2):
>
> Rizky, did you happen to find a solution? I have the exact same issue

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. [https://www.redhat.com/sysadmin/getting-started-socat](https://www.redhat.com/sysadmin/getting-started-socat)

On windows you couls use USR vcom or Lantronix CPR

---

<div class="post-metadata">

### Author: ![bar-shiratech](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bar-shiratech/32/47204_2.png) [@bar-shiratech](https://discourse.nodered.org/u/bar-shiratech)
#### Post date: [6 September 2021 09:13 UTC](https://discourse.nodered.org/t/one-tcp-requests-with-multiply-source-port/50629/6 "2021-09-06T09:13:43Z")

</div>

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

---

<div class="post-metadata">

### Author: ![bar-shiratech](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bar-shiratech/32/47204_2.png) [@bar-shiratech](https://discourse.nodered.org/u/bar-shiratech)
#### Post date: [6 September 2021 09:18 UTC](https://discourse.nodered.org/t/one-tcp-requests-with-multiply-source-port/50629/7 "2021-09-06T09:18:12Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [6 September 2021 09:31 UTC](https://discourse.nodered.org/t/one-tcp-requests-with-multiply-source-port/50629/8 "2021-09-06T09:31:11Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![bar-shiratech](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bar-shiratech/32/47204_2.png) [@bar-shiratech](https://discourse.nodered.org/u/bar-shiratech)
#### Post date: [6 September 2021 09:47 UTC](https://discourse.nodered.org/t/one-tcp-requests-with-multiply-source-port/50629/9 "2021-09-06T09:47:22Z")

</div>

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...

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [6 September 2021 12:10 UTC](https://discourse.nodered.org/t/one-tcp-requests-with-multiply-source-port/50629/10 "2021-09-06T12:10:40Z")

</div>

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 ....

---

<div class="post-metadata">

### Author: ![PiMo131](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pimo131/32/34978_2.png) [@PiMo131](https://discourse.nodered.org/u/PiMo131)
#### Post date: [6 September 2021 21:03 UTC](https://discourse.nodered.org/t/one-tcp-requests-with-multiply-source-port/50629/11 "2021-09-06T21:03:21Z")

</div>

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-client/blob/master/README.md)
> 
> [https://github.com/tiagordc/node-red-contrib-tcp-server](https://github.com/tiagordc/node-red-contrib-tcp-server)

---

<div class="post-metadata">

### Author: ![bar-shiratech](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bar-shiratech/32/47204_2.png) [@bar-shiratech](https://discourse.nodered.org/u/bar-shiratech)
#### Post date: [12 September 2021 07:10 UTC](https://discourse.nodered.org/t/one-tcp-requests-with-multiply-source-port/50629/12 "2021-09-12T07:10:44Z")

</div>

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

---

<div class="post-metadata">

### Author: ![bar-shiratech](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bar-shiratech/32/47204_2.png) [@bar-shiratech](https://discourse.nodered.org/u/bar-shiratech)
#### Post date: [12 September 2021 08:55 UTC](https://discourse.nodered.org/t/one-tcp-requests-with-multiply-source-port/50629/13 "2021-09-12T08:55:41Z")

</div>

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

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [27 September 2021 07:37 UTC](https://discourse.nodered.org/t/one-tcp-requests-with-multiply-source-port/50629/14 "2021-09-27T07:37:11Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
