J
J
JORIKUSS2022-01-16 17:36:01
Python
JORIKUSS, 2022-01-16 17:36:01

How to set up a connection between two PCs (socket library)?

I wrote something like a chat using the socket library. There are two files server and client. I launched the server, and asked my friend to run the client. But nothing happens. Checked on PC and laptop, everything worked. I think due to the fact that the PC and laptop are on the same wifi network. Tell me, please, is it possible to somehow make it work without a single wifi network?
server file:

import socket

ip='мой ipv4'

sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.bind((ip,8080))
sock.listen(5)

conn,addr=sock.accept()
print('connected ',addr)
conn.send('You are connected'.encode('utf-8'))

while True:
  data=conn.recv(1024)
  print(data.decode('utf-8'))
  conn.send(input('...').encode('utf-8'))

client file:
import socket

ip='мой ipv4'

sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.connect((ip,8080))

while True:
  data=sock.recv(1024)
  print(data.decode('utf-8'))
  sock.send(input('...').encode('utf-8'))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2022-01-16
@Vindicar

The server must have an external (white) IP, either directly or through a configured NAT (that is, the external IP goes to the router, and it already forwards connections from the outside to where it needs to be).
If the server is behind NAT, breaking it is no longer quite trivial, although it is possible. But you may need an intermediary server that has a white IP.
There is an option to "ask politely" for UPnP, but again it will work with your router - but not with the provider's hardware.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question