0

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?

2
  • Can you show the getpeerinfo? The addrlocal port should be random for all outbound connections and 18333 for all inbound connections Jan 7, 2016 at 6:04
  • The addrlocal is empty. So the rpcnet.cpp logic suppresses it in getpeerinfo
    – Guhan S
    Jan 7, 2016 at 6:36

1 Answer 1

4

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

4
  • 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
    Jan 7, 2016 at 6:38
  • 1
    @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
    Jan 7, 2016 at 7:03
  • 1
    @GuhanS If you're talking about modifying Bitcoin to choose that port automatically, this page will probably be helpful.
    – Nick ODell
    Jan 7, 2016 at 7:05
  • Yes exactly what I wanted. You guys are amazing!
    – Guhan S
    Jan 7, 2016 at 7:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.