I am trying to set up a Python socket between my Raspberry Pi (running Raspbian) and my Macbook Pro (running Mavericks).
Both devices are connected to the same WiFi network in my appt. I run the server code on my RPi and then the client code on my Macbook (I have also tried the reverse). I think that I am missing a set up step because the code I am using I found on multiple sites, so I assume it works. I also checked that my Macbook has firewall turned off.
Server Code:
from socket import *
host = "127.0.0.1"
print host
port = 7777
s = socket(AF_INET, SOCK_STREAM)
print "Socket Made"
s.bind((host,port))
print "Socket Bound"
s.listen(5)
print "Listening for connections..."
q,addr = s.accept()
data = raw_input("Enter data to be sent: ")
q.send(data)
Client Code:
from socket import *
host = "127.0.0.1"
print host
port=4446
s=socket(AF_INET, SOCK_STREAM)
print "socket made"
s.connect((host,port))
print "socket connected!!!"
msg=s.recv(1024)
print "Message from server : " + msg
I get the error:
Traceback (most recent call last): File "TCPclient.py", line 9, in <module> s.connect((host,port)) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py",line 224, in meth return getattr(self._sock,name)(*args) socket.error: [Errno 61] Connection refused
My process for executing the code is:
type "python TCPserver.py" into RPi terminal
type "python TCPclient.py into Macbook terminal
Then I receive the error message on my Macbook, no error on the RPi
My questions are:
- Is 127.0.0.1 the proper input for "host"? (please note I also tried "localhost")
- Does the input for host have to be the same for the client and server code?
- Should the RPi and Macbook both be connected to the same WiFi network?
- Is there any set up that needs to be done on either the RPi or my Macbook in order for this to work (Please note my RPi is Model B, new, and I did not set up anything else on it before this)
- Do you know why I am receiving this error and how to fix it?
Your help is greatly appreciated!!
0.0.0.0as host value. Let me know what happen. Also, the code run in the mac is quite long, or you didn't closed the string?