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

Viewed 26

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(('0.0.0.0', 20001))
while 1:
    a = sock.recvfrom(5)
    print(a)

and this for sending:

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

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!

0 Answers
Related