<p>All nodes are listening on port 18333 for inbound connections.  Any inbound connection will use this port.  So addrlocal will show localIP:18333.  </p>

<p>Any outbound connection will use a random port so addrlocal will show localIP:xxxxx</p>

<p>So every connection is either random &lt;----> 18333  OR 18333 &lt;----> random.  The node that initiated the connection (out) will always use a random port.</p>

<p>This is how all tcp/ip web services with known ports work.  Web servers listen on port 80 or port 443, browsers use a random local port to connect to them. </p>

<p>Edit: Should note that port 18333 is for testnet nodes. Would be 8333 for main net.  Also if on linux <code>netstat -ntlp</code> or <code>netstat -ntp</code> will show all tcp connections</p>

CC BY-SA 3.0
2016-01-07T06:38:08.023Z
Is there a possibility to make it 18333 <---> 18333 ? Like passing an option to system call and telling OS to use specified instead of random source port?
Guhan S
CC BY-SA 3.0
2016-01-07T07:03:34.027Z
@GuhanS You can do this on Linux with `sysctl net.ipv4.ip_local_port_range="18333 18333"` (Note: This will make it so you can only have one outbound connection at once.)
Nick ODell
CC BY-SA 3.0
2016-01-07T07:05:00.533Z
@GuhanS If you're talking about modifying Bitcoin to choose that port automatically, [this page](http://stackoverflow.com/questions/4711608/linux-how-to-send-tcp-packet-from-specific-port) will probably be helpful.
Nick ODell
CC BY-SA 3.0
2016-01-07T07:15:41.180Z
Yes exactly what I wanted. You guys are amazing!
Guhan S
CC BY-SA 3.0
2016-01-07T06:08:03.673Z
2016-01-07T14:48:54.857Z
2016-01-07T14:48:54.857Z
Dustin Butler
<p>I connected a node <code>A</code> to bitcoin <code>testnet</code> (Port 18333). The node discovery happens and I see couple of connections between my node <code>A</code> and the seed nodes. All of the sending and receiving happens between nodes ONLY via Port 18333 (I checked this in <code>ThreadSocketHandler</code>)</p>

<p>I add another node <code>B</code> (different laptop hence different IP) to the bitcoin <code>testnet</code>. I initiate a <code>addnode &lt;lappyB_IP&gt;:18333 onetry</code> connection from <code>A</code> and it gets accepted. But now the port <code>A</code> uses to send data is chosen randomly by OS and NOT 18333. This can be checked by
<code>getpeerinfo</code> </p>

<p>We should note that both are <strong>outbound</strong> connections from <code>A</code> -- namely the first one is we connected to seednode and send one is we connected to another node <code>B</code>. How come in the former data is getting sent via 18333 (default port) but in the later OS decides to chose a different port?</p>

CC BY-SA 3.0
2016-01-07T06:04:02.050Z
Can you show the getpeerinfo?  The addrlocal port should be random for all outbound connections and 18333 for all inbound connections
Dustin Butler
CC BY-SA 3.0
2016-01-07T06:36:27.820Z
The addrlocal is empty. So the `rpcnet.cpp` logic suppresses it in `getpeerinfo`
Guhan S
<bitcoind><bitcoin-core><client><tcp>
Why isnt a random port chosen for SEED node sockets?
Guhan S
# Why isnt a random port chosen for SEED node sockets?

(Asked by: username_0 on 2016-01-07)

I connected a node `A` to bitcoin `testnet` (Port 18333). The node discovery happens and I see couple of connections between my node `A` and the seed nodes. All of the sending and receiving happens between nodes ONLY via Port 18333 (I checked this in `ThreadSocketHandler`)

I add another node `B` (different laptop hence different IP) to the bitcoin `testnet`. I initiate a `addnode <lappyB_IP>:18333 onetry` connection from `A` and it gets accepted. But now the port `A` uses to send data is chosen randomly by OS and NOT 18333. This can be checked by
`getpeerinfo`

We should note that both are **outbound** connections from `A` -- namely the first one is we connected to seednode and send one is we connected to another node `B`. How come in the former data is getting sent via 18333 (default port) but in the later OS decides to chose a different port?

**Tags:** bitcoind, bitcoin-core, client, tcp

> **Comments:**
> **username_1**: Can you show the getpeerinfo?  The addrlocal port should be random for all outbound connections and 18333 for all inbound connections  
> **username_0**: The addrlocal is empty. So the `rpcnet.cpp` logic suppresses it in `getpeerinfo`  

---

## 1 answers

### Answer by username_1 on 2016-01-07. Score: 4 | Accepted answer

All nodes are listening on port 18333 for inbound connections. Any inbound connection will use this port. So addrlocal will show localIP:18333.

Any outbound connection will use a random port so addrlocal will show localIP:xxxxx

So every connection is either random <----> 18333 OR 18333 <----> random. The node that initiated the connection (out) will always use a random port.

This is how all tcp/ip web services with known ports work. Web servers listen on port 80 or port 443, browsers use a random local port to connect to them.

Edit: Should note that port 18333 is for testnet nodes. Would be 8333 for main net. Also if on linux `netstat -ntlp` or `netstat -ntp` will show all tcp connections

> **Comments:**
> **username_0**: Is there a possibility to make it 18333 <---> 18333 ? Like passing an option to system call and telling OS to use specified instead of random source port?  
> **username_2**: @username_0 You can do this on Linux with `sysctl net.ipv4.ip_local_port_range="18333 18333"` (Note: This will make it so you can only have one outbound connection at once.)  
> **username_2**: @username_0 If you're talking about modifying Bitcoin to choose that port automatically, [this page](http://stackoverflow.com/questions/4711608/linux-how-to-send-tcp-packet-from-specific-port) will probably be helpful.  
> **username_0**: Yes exactly what I wanted. You guys are amazing!  

---

