Python sockets won't connect

I am trying to run the server and client on two separate Windows 7 machines on the same network using sockets in Python 2.7. At first I just try to connect them before I try to do anything.

My server is currently:

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:

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 am getting:

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

      

I have looked through many other questions, but none of the answers solved my problem. Any help is appreciated.

When I use the server IP (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

      

+3


source to share


1 answer


You say they are two separate machines . You cannot communicate with one machine to another by connecting to 127.0.0.1

or localhost

.

Listening 0.0.0.0

is OK, which means the listening socket is accessible from all interfaces including the LAN.



However, in order to connect to your server, you obviously need to use the IP address (or hostname if you've configured your local nameserver correctly) of your server computer on the local network.

In your comment, the local IP address of your server computer 10.0.63.40

. This means that you must call s.connect("10.0.63.40", 12345)

.

+2


source







All Articles