0
votes

I was recently trying to model a simple peer to peer network. In my concept, peers try to both open a port and establish a connection with as many peers as possible using a third party "bootstrap" peer source to find a peer. I quickly noticed a problem:

  1. Peer A opens a port 6013 and announces his existence
  2. Peer B opens a port 31235 and announces his existence
  3. Peer A receives the announcement of B and connects to peer B, using 3111 as its source port
  4. Peer B receives the announcement of A and connects to peer B, using 7777 as its source port

At this moment we have both A connecting to the server socket of B and B connecting to the server socket of A without a way to be sure that these are actually the same hosts talking to each other - the source ports of client connections do not suggest which server port they have open. Moreover, I can't identify peers by their IP addresses because they might be behind some kind of a NAT.

What can I do to avoid such situation? Doubling the connections shouldn't really cause de-synchronization problems in my case, but this redundancy would probably lead to overhead I don't want.

1

1 Answers

1
votes

Each node should introduce itself with a unique GUID that it generates. Then when A and B establish connections with each other, they can decide who will be the master by simply comparing their GUIDs - the node with larger GUID becomes a master and keeps its connection (i.e. connection it initiated). The node with smaller GUID close initiated connection and keeps the received one.