0

I am trying to establish a socket connection, therefore I coded and tried this example code with server.py and client.py:

server.py:

import socket

port = 5674
ip = socket.gethostbyname(socket.gethostname())

s = socket.socket()
s.bind((ip, port))
s.listen(2)
print('Server is ready')

while True:
    client, address = s.accept()
    print('New connection to {}'.format(address))
    client.send('You are connected to the server!'.encode('utf-8'))

client.py:

import socket

ip = ''  # enter ip address of server
port = 5674

s = socket.socket()
s.connect((ip, port))

msg = s.recv(1024).decode('utf-8')
print(msg)

Trying to run this on one machine will work absolutely fine.

Running on different machines in different networks will give the following error-message in client.py:

Traceback (most recent call last): File "D:/Python/01programs/SocketTest/client.py", line 7, in s.connect((ip, port)) TimeoutError: [WinError 10060] Ein Verbindungsversuch ist fehlgeschlagen, da die Gegenstelle nach einer >bestimmten Zeitspanne nicht richtig reagiert hat, oder die hergestellte Verbindung war fehlerhaft, da >der verbundene Host nicht reagiert hat

-> shortened in English: try to connect failed because the connected host did not respond in time

What is going wrong with this socket connection? I would say it seems like client is not able to find server and is not able to connect because of this.

What part is wrong and how to fix? Thanks for help!

PS: I already read many questions related to this problem but none of them seemed to help in my case.

FinnK
  • 210
  • 4
  • 10

1 Answers1

0

If you trying to accesses your server not from the same host, you can't. you need to run the server not in local-host mode, change the server ip bind to "0.0.0.0" and it should work.