# How does a TCP out node work when it is set to listen to a port?

**URL:** https://discourse.nodered.org/t/how-does-a-tcp-out-node-work-when-it-is-set-to-listen-to-a-port/14909
**Category:** General
**Created:** [29 August 2019 13:44 UTC](https://discourse.nodered.org/t/how-does-a-tcp-out-node-work-when-it-is-set-to-listen-to-a-port/14909 "2019-08-29T13:44:26Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Gnamra](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gnamra](https://discourse.nodered.org/u/Gnamra)
#### Post date: [29 August 2019 13:44 UTC](https://discourse.nodered.org/t/how-does-a-tcp-out-node-work-when-it-is-set-to-listen-to-a-port/14909/1 "2019-08-29T13:44:26Z")

</div>

I am currently trying to understand a system designed in Node-RED. There is a "master" raspberry pi and a "slave" raspberry pi. In the "master" portion of the system, I have identified a TCP in node that points to the slave at tries to connect to it. In the slave portion, the only reference to the port that the master is trying to connect to that I can find is a TCP out node that is set to listen to that port. There is a function node connected to the TCP out node and tries to feed input to the TCP out node. This does not make any sense to me, how does the TCP out node work when it is set to listen to a port?

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [29 August 2019 13:59 UTC](https://discourse.nodered.org/t/how-does-a-tcp-out-node-work-when-it-is-set-to-listen-to-a-port/14909/2 "2019-08-29T13:59:18Z")

</div>

Ok, I am not the best person to reply, but I feel I should contribute something back as I am good at asking questions.

I kind of get what you are saying, but would like to ask you:  
What is it you are _sending_? As in: is it a message, or are you wanting to control programs on the other machine?

My take is that the TCP nodes allow you to send _commands_ (not really the right term/word, but for now it will suffice for the sake of what is being said now) to a program.

So, say you have a program running and it has a TCP port on port 25.

On the other machine you connect to the remote machine and send stuff to port 25 of the other machine.

That program _replies_ on port 80.  
So on the other machine (the one which sends stuff out on port 25) then gets are TCP IN node and _listens_ to port 80 for replies.

I'm not sure you are using the right nodes to do what you want. Though you didn't really say.

Does that help you?

---

<div class="post-metadata">

### Author: ![Gnamra](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gnamra](https://discourse.nodered.org/u/Gnamra)
#### Post date: [29 August 2019 14:13 UTC](https://discourse.nodered.org/t/how-does-a-tcp-out-node-work-when-it-is-set-to-listen-to-a-port/14909/3 "2019-08-29T14:13:09Z")

</div>

I have to be careful with what I say, because I am not allowed to divulge too much information about how the system works.

> What is it you are _sending_ ?

0 or 1 is being sent. This is being used to decide when the "master" should send something to the "slave" physically.

> My take is that the TCP nodes allow you to send _commands_ (not really the right term/word, but for now it will suffice for the sake of what is being said now) to a program.  
> That program _replies_ on port 80

Shouldn't the tcp out be set to "Reply to TCP" in that case?

Let me try to be more clear on what is happening:  
Master has a TCP in node that is set to "Connect to port 8260" on host "192.168.250.63"  
Slave has a TCP out node that is set to "Listen on port 8260"

What is confusing me is, why would an output node listen to anything. Isn't the job of an output node to output things? Not receive? Or is it listening to that port so that it can establish a connection to the TCP in node on the master?

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [29 August 2019 14:23 UTC](https://discourse.nodered.org/t/how-does-a-tcp-out-node-work-when-it-is-set-to-listen-to-a-port/14909/4 "2019-08-29T14:23:02Z")

</div>

There are two separate ideas here:

- whether a node sends or receives data.
- whether a node connects to a remote port, or waits for something to connect to it.

A `TCP In` node will send a message into your flow for each packet it receives from a tcp connection.  
A `TCP Out` node will send a TCP packet over a tcp connection for each message you pass to it.

The question is then whether the tcp connection those nodes use was created by the node connecting to a remote port, or by a remote system connecting to the local node-red.

---

<div class="post-metadata">

### Author: ![Gnamra](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gnamra](https://discourse.nodered.org/u/Gnamra)
#### Post date: [30 August 2019 06:11 UTC](https://discourse.nodered.org/t/how-does-a-tcp-out-node-work-when-it-is-set-to-listen-to-a-port/14909/5 "2019-08-30T06:11:29Z")

</div>

Thanks! That makes things a bit more clear.

So the `TCP Out` is set to "Listen to" because the person who made this system just wants it to make a connection to the master then?

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [31 August 2019 00:13 UTC](https://discourse.nodered.org/t/how-does-a-tcp-out-node-work-when-it-is-set-to-listen-to-a-port/14909/6 "2019-08-31T00:13:42Z")

</div>

Can I suggest you run an mqtt broker on one of your PIs and just use mqtt to communicate between them. It is so much easier than handling this yourself. If you don't know what MQTT is, don't be afraid, it is very simple and it will greatly simplify your intercommunications I promise 🤞.

Just have a look around, once you "get it" you will no longer wish to implement custom communication channels like you seem to have here.

---

<div class="post-metadata">

### Author: ![guysqr](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/guysqr/32/11551_2.png) [@guysqr](https://discourse.nodered.org/u/guysqr)
#### Post date: [31 August 2019 01:27 UTC](https://discourse.nodered.org/t/how-does-a-tcp-out-node-work-when-it-is-set-to-listen-to-a-port/14909/7 "2019-08-31T01:27:19Z")

</div>

The TCP Out node is going to listen for **inbound** connections on that port. When a client connects to your Out node on that port, that client will receive messages you send to that Out node (in exactly the same way that you receive messages from the server you are connecting to with your TCP In 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: [16 December 2019 17:36 UTC](https://discourse.nodered.org/t/how-does-a-tcp-out-node-work-when-it-is-set-to-listen-to-a-port/14909/9 "2019-12-16T17:36:08Z")

</div>

??? - the in port can only listen on one port at a time - so you need two in nodes... so then it's easy to wire each to their own out/reply-to node - which will keep them separate.
