Blockchain By Example
上QQ阅读APP看书,第一时间看更新

Changing the ports

To operate, Bitcoin uses two ports on which the client will listen to establish connections, namely:

  • p2p port (by default, 8333 in mainnet and 18333 in testnet)
  • RPC port (by default, 8332 in mainnet and 18332 in testnet)

We will define different ports for both mainnet and testnet, in order to be able to run your altcoin alongside a Bitcoin client without port overlap. Remember, mainnet and testnet are abbreviations for the main and test public networks.

Get your favourite text editor and open up chainparams.cpp and chainparamsbase.cpp located in the src/ folder. Find the network parameters, including the ports, and then change the following variables:

  • For mainnet (CMainParams and CBaseMainParams classes):
chainparams.cpp: nDefaultPort = 9333;
chainparamsbase.cpp: nRPCPort = 9332;
  • For testnet (CTestNetParams and CBaseTestNetParams classes):
chainparams.cpp: nDefaultPort = 19333;
chainparamsbase.cpp: nRPCPort = 19332;

To make modifications consistent with the whole code, change the port occurrences in all files using the following Linux commands:

find ./ -type f -readable -writable -exec sed -i "s/8332/9332/g" {} ";"
find ./ -type f -readable -writable -exec sed -i "s/8333/9333/g" {} ";"

Feel free to choose any port you like, but avoid the mainstream ones (below 1000,such as 80 for HTTP).

Great! Remember that you can always change either the Bitcoin port or your altcoin's port just by providing the options -rpcport and -port in the command line or in the readercoin.conf file without editing the Bitcoin code base.