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
Kieran
  • 133
  • 2
  • 2
  • 12
  • 3
    Do you have a firewall that could be blocking the connection? Is the port open? – Zach Gates May 22 '15 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. – James Parsons May 22 '15 at 18:22
  • what are the ip addresses of your hosts? – matcheek May 22 '15 at 18:23
  • possible duplicate of [Errno 10061 : No connection could be made because the target machine actively refused it ( client - server )](http://stackoverflow.com/questions/12993276/errno-10061-no-connection-could-be-made-because-the-target-machine-actively-re) – Zach Gates May 22 '15 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 – Kieran May 22 '15 at 18:25

2 Answers2

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).

Dr. Jan-Philip Gehrcke
  • 33,287
  • 14
  • 85
  • 130
  • 1
    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. – Dr. Jan-Philip Gehrcke May 22 '15 at 18:40
  • 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. – Kieran May 22 '15 at 19:05
-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,753
  • 72
  • 57
  • 129