Creating a simple cryptocurrency: part 7
The repository now includes changes to server.js. There is a new object:ip
(IPv4 address). sr
is upgraded with TCP capability.
The ip object (IPv4 address)
Servers are identified primarily by their public keys but to send them messages we need IPv4 addresses and port numbers. Theip
property has been added to the server object (the elements of the cr.servers
array). It is a 6-byte buffer containing a packed version of the server's IPv4 address (4-bytes) and port-number (2-bytes). The ip
object has function properties for packing and unpacking along with a function for determining the external address of the current server. The latter is for future use. For now the ip
property of each server is manually set at the beginning of ip.set
.
The TCP server
The TCP server uses the same port as the UDP server. Both are always listening for connections, the former from other servers and the latter from clients.
The sr.send
function property opens a connection with another server, sends a set of messages, then closes the connection. The sr.sendAll
function property sends a set of messages to all other servers.
At this stage of development, for the purpose of testing, a simple message is sent to all other servers by the beta server at the beginning of the beta period.
Testing
To test TCP communication between servers, first modify the key
properties of cr.servers
to match your servers' public keys, then modify the beginning of ip.set
to match your servers' port numbers.
Start three different servers in three different directories containing seed files, with three different port numbers, in three different console windows. You should see each server send a message to the other two servers when it first becomes the beta server.