6

I'm trying to run a server and a client on two separate Windows 7 machines on the same network using sockets in Python 2.7. First I'm just trying to get them to connect before trying to do anything.

Currently my server is:

import socket 
host = '0.0.0.0' #Also tried '', 'localhost', gethostname()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, 12345))
s.listen(5)
cs, addr = s.accept()
print "Connected."

My client is:

import socket
host = '127.0.0.1' #Also tried 'localhost', gethostname()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(host, 12345)
print "Connected."

The error I get is:

socket.error: [Errno 10061] No connection could be made because the target machine actively refused it. 

I've looked through lots of other questions but none of the answers solved my problem. Any help is appreciated.

When I use the IP address of the server (10.0.63.40) as the host for the client, I get

[Errno 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
asked May 22, 2015 at 18:19
5
  • 3
    Do you have a firewall that could be blocking the connection? Is the port open? Commented May 22, 2015 at 18:22
  • Are you running with elevated privileges. I have had this happen in cases where I didn't have admin access. This is true for many languages and programs. In fact, the only language that did not reject the socket was Ruby. Commented May 22, 2015 at 18:22
  • what are the ip addresses of your hosts? Commented May 22, 2015 at 18:23
  • possible duplicate of Errno 10061 : No connection could be made because the target machine actively refused it ( client - server ) Commented May 22, 2015 at 18:24
  • I don't have a firewall blocking it and the port is open. The IP of the server is 10.0.63.40, the client's is 10.0.63.141. I'm not sure how to check if I'm running with elevated privileges Commented May 22, 2015 at 18:25

2 Answers 2

10

You are saying these are two separate machines. You cannot reach the one machine from the other by connecting to 127.0.0.1 or localhost.

Listening on 0.0.0.0 is fine, this means that the listening socket is reachable from all interfaces, including the local network.

However, for connecting to your server, you obviously need to use the IP address (or hostname, if you have properly set up a local name server) of your server machine in the local network.

Per your comment, the local IP address of your server machine is 10.0.63.40. That means you should end up calling s.connect("10.0.63.40", 12345).

answered May 22, 2015 at 18:29
Sign up to request clarification or add additional context in comments.

2 Comments

The error you are showing now indicates a firewall issue. The host does not seem to be reachable from the outside on port 12345. As this is Windows, you need to get control of your custom firewall application or the Windows firewall. Both can surely be disabled temporarily, so that it is easier for you to debug the issue.
Ah turns out I had disabled the firewall for the wrong network. Now that the firewall is off everything is running fine. Sorry about that, and thanks.
-1

I had the same problem when I tried to connect my client code with server one. It got resolved my by using this command:

socket.gethostbyname(socket.gethostname()) 

: note[ I run it locally not uploaded it into a live server]

Brian Tompsett - 汤莱恩
5,92772 gold badges64 silver badges135 bronze badges
answered Jan 23, 2022 at 9:43

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.