Host issues when connecting two computers with socket-py

Asked

Viewed 48 times

-2

Hello!

I’ve been trying to learn about python socket. My first code would be a program to connect a client to a server and then send data. I put the python file on the server side on another computer in my house.

Here’s part of the client-side:

import socket
HOST = "localhost"
PORT = 5000
tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
dest = (HOST, PORT)
print("Conectando...")
tcp.connect(dest)

And here’s the server-side part:

HOST = socket.gethostname()
print(HOST)
PORT = 5000
threads = {}
tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
orig = (HOST, PORT)
tcp.bind(orig)
tcp.listen(5)
con, cliente = tcp.accept()

Here is the error I get. I believe this is related to the host:

tcp.connect(dest) Connectionrefusederror: [Winerror 10061] No connection could be made because the destination machine actively refused them

I tried to put in the client-side host variable the ip of my machine - server - and apparently there is no answer.

1 answer

0

Your client is pointing to localhost while your server is picking up the machine name with socket.gethostname()

On the server use:

HOST = "localhost"

or

HOST = "127.0.0.1"

or

HOST = "0.0.0.0"

The latter will do the bind both on localhost and on the IP(s) of the network card(s)

  • Using HOST = "0.0.0.0" and on the client side using the server ip: No server response. Already when using HOST = "localhost" on the client side says the server refused.

  • Using on the server HOST = "localhost" and on the customer HOST = "localhost" server refused. Already using client HOST = "ip do servidor" there was no response.

  • It looks like a permission issue in the firewall. If you are using Windows (it seems to be due to the error), open a cmd e rode o seu server; depois abra outro cmdcomo administrador, e rodenetstat -a -b`; check that the TCP 5000 port is LISTENING.

  • https://prnt.sc/10r4f87 That’s right, it’s in Listening.

  • Agita has to see the firewall...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.