How do p2p calls and messaaging via simple devices (phone, laptop) work?


This Content is from Stack Overflow. Question asked by Alexey_Chebotarev

I am trying to create a python app to connect two devices via p2p udp connection. I am not professional developer and I don’t know much about this topic, so my question may seem stupid. Sorry, hope you answer it anyway.

I wrote this code for receiving:

import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('', 20001))
while 1:
    a = sock.recvfrom(5)

and this for sending:

import socket
import time
rendezvous = ('localhost', 20001)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('', 50001))
while 1:
    sock.sendto("hello", rendezvous)

And my code works for the one local computer.
But when i’m trying to change localhost to the my computer’s ip in the randezvous in the sending program, and to run sender on the other computer, that’s all doesn’t work. And when I tried to run receiver on my server and sender on my laptop, it worked.
So I thought – maby problem is just, that it’s not possible to connect two not-server devices without any server? But I know, there are applications that have the p2p call function (Telegram as example).

So can you please explain to me, how do this p2p calls in Telegram work and how can I do something similar? Thank you!


This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?