# How to distinguish TCP connections with TCP IN/OUT nodes?

**URL:** <https://discourse.nodered.org/t/how-to-distinguish-tcp-connections-with-tcp-in-out-nodes/83801>\
**Category:** General\
**Created:** [18 December 2023 09:32 UTC](https://discourse.nodered.org/t/how-to-distinguish-tcp-connections-with-tcp-in-out-nodes/83801 "2023-12-18T09:32:28Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kreeker](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@Kreeker](https://discourse.nodered.org/u/Kreeker)\
**Post date:** [18 December 2023 09:32 UTC](https://discourse.nodered.org/t/how-to-distinguish-tcp-connections-with-tcp-in-out-nodes/83801/1 "2023-12-18T09:32:28Z")

</div>

Hello Node-Red community!

I have a problem with distinguishing TCP connections.

I have multiple devices connected to me using TCP IN node via port XXX. If i send any information using TCP OUT node, all the devices receive that info, while i need to send it only to a specific device.

Even if i move each device to a diffrent port, diffrent TCP IN and TCP OUT nodes, sending commands via one of TCP OUT nodes still causes all of the devices to receive that command.

Please note that devices are redirected to me from the router's external ip, so all the devices have ip of 192.168.1.1.

Any tips?

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [18 December 2023 09:45 UTC](https://discourse.nodered.org/t/how-to-distinguish-tcp-connections-with-tcp-in-out-nodes/83801/2 "2023-12-18T09:45:39Z")

</div>

Could you post a picture of a network diagram of your setup?

---

<div class="post-metadata">

**Author:** ![Kreeker](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@Kreeker](https://discourse.nodered.org/u/Kreeker)\
**Post date:** [18 December 2023 09:53 UTC](https://discourse.nodered.org/t/how-to-distinguish-tcp-connections-with-tcp-in-out-nodes/83801/3 "2023-12-18T09:53:09Z")

</div>

![network](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/d/edf5d7669e7b5dad20cdec291783fb880038d211.png)

It would be something like this i assume.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [18 December 2023 09:53 UTC](https://discourse.nodered.org/t/how-to-distinguish-tcp-connections-with-tcp-in-out-nodes/83801/4 "2023-12-18T09:53:09Z")

</div>

If you add debug nodes and set them to complete message object, you will see the \_session object received.  
e.g.

```auto
{"type":"tcp","id":"4069fb619d89bbf9"}

```

If you do not overwrite this object and make sure it gets to the out node, it should only reply to the incoming tcp connection.

You can read about this in the out nodes help text.

---

<div class="post-metadata">

**Author:** ![Kreeker](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@Kreeker](https://discourse.nodered.org/u/Kreeker)\
**Post date:** [18 December 2023 09:57 UTC](https://discourse.nodered.org/t/how-to-distinguish-tcp-connections-with-tcp-in-out-nodes/83801/5 "2023-12-18T09:57:11Z")

</div>

I had already tried assigning the devices to diffrent ports and separate nodes like in the uploaded picture.  
 ![message](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/0/d05870793b25b5b43dc0a8e487a587bd6a61aaa2.png)

Injecting messages to port 999 will cause the upper connection (port 998) to receive the message too. I also tried moving them to separate flows, but that also didnt do anything.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [18 December 2023 09:59 UTC](https://discourse.nodered.org/t/how-to-distinguish-tcp-connections-with-tcp-in-out-nodes/83801/6 "2023-12-18T09:59:49Z")

</div>

Again add a debug to the tcp in node, set to full message object and look at the \_session object. The \_session object has to make it to the out node. It has nothing to do with port numbers.

---

<div class="post-metadata">

**Author:** ![Kreeker](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@Kreeker](https://discourse.nodered.org/u/Kreeker)\
**Post date:** [18 December 2023 11:21 UTC](https://discourse.nodered.org/t/how-to-distinguish-tcp-connections-with-tcp-in-out-nodes/83801/7 "2023-12-18T11:21:59Z")

</div>

I think i have found the mistake that i was making:  
Injecting a msg directly into TCP Output without any session info resulted in it being sent to every active connection.

I have made a crude program that catches msg.\_session and sends a message using that session information, and it seems to work now - BUT i have a problem with extracting all the sessions and redirecting them to diffrent routes, so i can handle them individually.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [18 December 2023 12:17 UTC](https://discourse.nodered.org/t/how-to-distinguish-tcp-connections-with-tcp-in-out-nodes/83801/8 "2023-12-18T12:17:26Z")

</div>

If you post a export example flow, we can then give you some pointers.

[How to import/export a flow](https://nodered.org/docs/user-guide/editor/workspace/import-export)

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote `````)

````auto
``` 
   code goes here 
```

````

You can edit and correct your post by clicking the pencil ✏ icon.

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<div class="post-metadata">

**Author:** ![Kreeker](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@Kreeker](https://discourse.nodered.org/u/Kreeker)\
**Post date:** [18 December 2023 13:42 UTC](https://discourse.nodered.org/t/how-to-distinguish-tcp-connections-with-tcp-in-out-nodes/83801/9 "2023-12-18T13:42:53Z")

</div>

No need, i will manage it somehow.  
Thank you for your time and putting me on the right path.

---

<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:** [1 January 2024 13:43 UTC](https://discourse.nodered.org/t/how-to-distinguish-tcp-connections-with-tcp-in-out-nodes/83801/10 "2024-01-01T13:43:21Z")

</div>

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