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
-
3Do you have a firewall that could be blocking the connection? Is the port open?Zach Gates– Zach Gates2015年05月22日 18:22:18 +00:00Commented 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.James Parsons– James Parsons2015年05月22日 18:22:54 +00:00Commented May 22, 2015 at 18:22
-
what are the ip addresses of your hosts?matcheek– matcheek2015年05月22日 18:23:08 +00:00Commented 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 )Zach Gates– Zach Gates2015年05月22日 18:24:27 +00:00Commented 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 privilegesKieran– Kieran2015年05月22日 18:25:27 +00:00Commented May 22, 2015 at 18:25
2 Answers 2
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).
2 Comments
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]