granted07@kolkata:~/p2pchat$ ./p2pchat
p2pchat
A chat between two computers with nothing in the middle. No account, no server. The two dots above are the whole architecture.
scroll
Most chat apps have a computer in the middle. You send to it, and it sends to them.
Here, you type and the words go straight to the other screen.
The hard part is getting in. Your router hides everything behind it, so a stranger cannot just knock. To be reachable, the program has to ask the router to open one door.
upnp->add_port_mapping(my_port);
Routers understand that request through a protocol called UPnP. Not every router does.
now make the call
Two people, two routers, and one of them is late. Take UPnP away, or make the peer slow, and see what the program says.
It tries 30 times, two seconds apart. Shown here at 8× speed.
granted07@you:~$ p2pchat
Every line the program prints in that terminal is a real line from main.cpp.
Chatting is two jobs at once: waiting for you to type, and waiting for them to write. So the program does them side by side.
std::thread receiver([&] {
while (running) { inbound.receive(); sleep(100ms); }
});
while (getline(cin, message)) outbound.send(message);Paste something very long into the chat above. Then go find out why the other side gets it in pieces.
where to look
what it does not claim
!It runs on Windows only. The sockets are Winsock.
!Nothing is encrypted. And a long message is read in pieces, so it can arrive as several.
!The door stays open. The code asks the router for a lease of 0, meant to last until someone closes it. A function to close it exists. main never calls it.