tags: Distributed Systems Network protocol Parallel Computing Blockchain
Recently I am studying P2P technology, but there are not many related materials. I figured it out myself, share some principles of learning P2P, and how to create a P2P chat application.
P2P here refers topeer to peer, Point-to-point technology, each client is a server, there is no central server, not websocket push messages for a connection.
The first solution is the problem of intranet penetration. The common underlying protocols tcp and udp have their own advantages and disadvantages. Briefly explain. tcp: Need to deal with sticky packets, duplex flow channel, is a reliable link. udp: Each time it sends a data packet, there is no sticky packet problem, but the connection is unreliable and only a small amount of data can be transmitted
More detailed please Google
Choose udp protocol here, simpler.
The next step is intranet penetration, first to say the conclusion:Two nodes in different internal networks can never discover the mutual existence between them, Even if you want to hit him along the network cable.
All intranet penetration principles require nothing more than an intermediary server with a public IP, including virtual currencies like bitcoin, etc., so firstGenesis Node
In NodeJS, creating udp service is also very simple
const dgram = require("dgram");
const udp = dgram.createSocket("udp4");
udp.bind(1090, callback)
If the service is deployed on the public network, all other nodes can access it. Through the transit server, two nodes can establish a connection

We are going to build such a P2P network

If there are only 3 nodes now:Genesis Node, Node B, Node C, Genesis node has public IP
In the form of a dialogue, I elaborate on the process of establishing a link:
Node B: hey, the genesis node, I want to join the P2P network and tell other brothers, I have come to the genesis node: Brothers, just now a node called B has joined the network, you also go to tell other nodes other nodes : I just received a notification from the "Genesis Node" that a fresh meet has joined the network, called "B"
… At this point, everyone knows that Node B has joined the network, which records the relevant information of Node B, including the IP address, including the udp port number
At this point, node C also wants to join the network and wants to talk to node B:
Node C: hey, the genesis node, I want to join the P2P network, and I want to talk to the genesis node with B: Brothers, just now a node called B has joined the network, you also go to tell other nodes, by the way See if there are other nodes in the B node: I just received a notification from the "Genesis node", there is a fresh meet to join the network, called "C", you also see if there are other nodes in the B node 2: received the notification , I heard that a node called C is looking for a node B, and I have its information here, ip is xxx.xxxx.xxx.xxxx, port 10086 B node: There is a C guy (ip: xxxx.xxxx.xxxx. xxxx, port 1000)
At this point, B got C's information, including IP and port, and C got B's information.
Thus, the two of them can establish communication. Message flow: B <----> C. No server in the middle
Use a picture to describe:

In the design, the function of each node is the same. If you need to join the network, you do n’t have to link to the genesis node
Suppose there are already existing nodes: the genesis node, nodes A, B, and C. At this time, a node D wants to join the network.
Then node D does not necessarily have to be linked to the genesis node, it can be linked to any one of nodes A, B, and C, and then the node broadcasts to other nodes saying "Hey, there is a new person called D who joined the network".
In this way, everyone knows that there is a node called D, you can communicate with it, and at the same time, D node and the existing node will be synchronized. In this way, node D also knows the existence of other nodes.
When the TCP connection pool performs a rlpx handshake with a node to establish an encrypted connection, it will trigger the addpeer pipe of the server.run function to pass the message, capture the me...
First select 2 PCs, two Loptops (notebook type), and two switches to construct the topology map Use a crossover cable between two devices of the same type, and use a straight-through cable between two...
(1) Build a peer-to-peer network topology as the picture shows: Set up a peer-to-peer network consisting of two computers. Configure IP addresses for PC0 and Laptop. (2) Test the connectivity from pin...
Preparation: Server-side deployment with nodejs and socket.io Directly on the code, simply: app.js : Js script file socket.io.js: webRtc.html: Two mobile phones open hotspots, under Firefox brow...
The edge of the internet The part at the edge of the Internet is all the hosts connected to the Internet. These hosts are also calledEndsystem. The owner of the end system can be an individual, a unit...
For a local area network with a small number of computers and low network security requirements, it is wasteful to take out a separate computer to make a server. You can use a peer-to-peer network str...
First you have to prepare a WANem Server. 2 machines are on the same network segment (it is recommended to be on the same network segment as the WANem server). The two machines do the following: 1) On...
First establish a connection and comment Use different color rectangular boxes to distinguish the Ethernet network of floor one and floor two and configure ip Click command promote to test the connect...
WCF supports the creation of P2P applications use netPeerTcpBinding binding. This binding allows a plurality of portions on one peer communications transport protocol. It also defines how the nodes in...
Structured and unstructured network Unstructured P2P network refers to the absence organizational relationships between network nodes, or the like is entirely between nodes, such as the first generati...