In UDP communication, there is accually no server or client.
A computer binds its own port and use recvfrom(), the others computer just sendto the computer IP and port without bind().
- initialize a socket
- bind its own port and IP(INADDR_ANY)
- recvfrom via its own port(can get clientaddr)
- initialize a socket
- assign server IP and port
- sendto the above server IP and port
In a PC whose IP is 192.168.1.126, run
g++ server.cc -o server
./server
In a PC whose IP is another, run
g++ client.cc -o client -std=c++14 -pthread
./client
You can use lsof -i:port
in client PC to verify if the port output from server.cc is consistent.