Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python sockets will not connect

Tags:

sockets

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
like image 261
Kieran Avatar asked May 22 '15 18:05

Kieran


1 Answers

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

like image 171
Dr. Jan-Philip Gehrcke Avatar answered Oct 21 '22 19:10

Dr. Jan-Philip Gehrcke