2

I'm making a chat app using sockets in python, but when I try to connect from a different computer then it says:

C:\Users\James\OneDrive\Documents\Python\Projects\Gui Chat\client.pyw [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

This is the server code for the socket:

host = socket.gethostbyname(hostname)
port = 55555
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((host, port))
print(f"IP: {server.getsockname()[0]}\nPORT: {server.getsockname()[1]}")
server.listen()

I also have a while True loop accepting all requests:

while True:
   client, address = server.accept()
   print(f"Connected with {str(address)}")

On the client end this is the socket code:

IP = simpledialog.askstring("IP", "Enter IP address", parent=root)  # "192.168.1.252"  # input("Enter IP: ")
nickname = simpledialog.askstring("Nickname", "Choose a nickname", parent=root)

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
    client.connect((IP, 55555))
except Exception as e:
    print(e)

The programme asks for the IP address each time, and when I put in the correct IP for the server computer, it comes up with the error above. It works if I try to connect from the same computer, and they are both on the same network. It was working recently, and now it has just stopped working.

UPDATE: I have set the server ip to 0.0.0.0, I have set up a port forwarding rule, I have checked the firewall and allowed incoming and outgoing connections, and I have run nmap with these results:

nmap results

1
  • 2
    Try binding to 0.0.0.0 instead of socket.gethostbyname(hostname), whatever hostname is, unless you have a good reason otherwise, which isn't stated here. Commented Aug 29, 2021 at 11:09

1 Answer 1

2

Code issues

First try binding server on localhost or 127.0.0.1.

FireWall/Ports issues

Check if your computer's default computer/antivurus
firewall (where server is hosted) allow connections
on your port 55555.

And if computer with client is outside your home network
point to router public IP address and make sure you have
port forwarding setup on router.

Address issues

Are you sure that IP you are writing in client is correct.
Go to your computer with server and check that IP.

Windows: Go to cmd or Power Shell and type ipconfig, then find
section IPv4 Address and look that address you habe there.

Linux / MacOS Go to your terminal and type ifconfig -a, and
it should be somewhere there, but I don't have those systems,
so I can't test it for you. If it does't work try to search how to
find that out.

Sign up to request clarification or add additional context in comments.

7 Comments

I have the right IP address, and I have changed the IP to 127.0.0.1 . It still doesn't work though. Please also explain how to set up port forwarding, I have tried to but not managed. Thanks
It depens on router, but let me explain general contept. Type in borwser your default gateway (for you it should be 192.168.1.1) and login to router. Then go to advenced settings and find Port Forwanding, select what port it need to redirect and point ip address at your server, so now if some defice from outside network ask on this port router redirect it to your laptop and can communicate, remeber to path that hole up if you are not using it, because in some cases hackers can use that hole to hack into your system, but I need to end explaning because of comment limit, say if it help.
I've done that before and it never worked so I gave up. I also checked the firewall and it allows connections. Any more suggestions for me?
There is one way to check whats wrong, did you heard of tool called nmap? It is VERY powerfull. Download it and try scanning for your port from client computer using this command nmap -sT -p 55555 (your server ip address), BUT if router isn't yours make sure you have privilages from owner, otherwise it can lead to some consecuences. So use it at your own risk. Then you will skan check if it say is it open (port) or not so that will define is it code or still firewall/netwrking issue. PS: did you do port forward as I tell? It is very important to setup for connectin like that.
I've done what you said, I have put the results in my original question, but it still doesn't work
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.