sockets - About UDP port in p2p application -


i'm doing small demo project of dht (p2p routing only, chord, pastry, etc.), , i'm confused detail of transportation in p2p network.

assuming peers communicate using udp , port 10050, fixed one, consider 2 typical cases:

  1. a may send "join" message b.
  2. also, b may send "join" message (or other messages) a according p2p nature.

in case 1, destination port of message must 10050;

in case 2, destination port of message must 10050;

i wonder what's source port of messages in both cases? if it's random port decided system calls, dht protocol stack need process datagram on 10050 , random port, normal behavior in dht based application (e.g., emule, bittorrent)?

i wonder what's source port of messages in both cases? if it's random port decided system calls

you use socket bound (posix bind()) specific local port. local port function both source port outgoing messages , destination port incoming messages @ same time. note udp want use posix sendmsg() or sendto() calls , not send().

this important dht protocols because other nodes infer port you're listening on based on source port , store in routing tables.

as specific port number, can in principle use fixed port number dht, means other nodes don't have infer listening port because know it's fixed.

but better have flexibility port clashes can avoided. node should free choose port , send and receive operations on port.


additionally, since ipv6 becoming more common want bind socket 1 specific address on host, because v6 comes multiple addresses per host , listening on multiple interfaces , sending messages different ips make appear unreliable other dht nodes (constantly changing ip addresses). i.e. not bind any-local address (0.0.0.0 or ::0) if can avoid it.

figure out local address used default route , bind that.


Comments