Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1002def start_p2p_listener(network_thread, listener):1003listen_addr = ""1004listen_port = 01006def on_listen_done(addr, port):1007nonlocal listen_addr1008nonlocal listen_port1009listen_addr = addr1010listen_port = port1012# Use port=0 to let the OS assign an available port. This1013# avoids "address already in use" errors when tests run1014# concurrently or ports are still in TIME_WAIT state.1015network_thread.listen(1016addr="127.0.0.1",1017port=0,1018p2p=listener,1019callback=on_listen_done)1021# Wait until the callback has been called.1022wait_until_helper_internal(lambda: listen_port != 0)1024return listen_addr, listen_port
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.