Search code examples
networkingudpp2pportsmultipeer-connectivity

In my peer to peer application, should I use multiple ports?


I am building a simple peer to peer application where about 8 participants all connect to each other (n*n). I will be using UDP with a reliability and ordering protocol layered on top. Each peer will be broadcasting a few KB of data per second.

It occurred to me that there are two ways of configuring the ports on each peer:

  1. Each peer takes one port and all messages are received via that
  2. Each peer takes a port for each other peer, and only communicates with a peer using its corresponding port

What are the advantages and disadvantages of each approach?


Solution

  • Creating only one port to communicate with each peers is the best option. You create only one socket and use only one port to send/receive data with as many peers as you want. You can distinguish received data by seeing source address of each packet. This way has Less complicated code and is more resource efficient.

    Creating multiple port has absolutely no advantage. It will complicate your code and will use much more resource with no benefit. Resource consumption will grow with more peers.