Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raspberry PI Server/Client Socket in Python

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:

  1. Is 127.0.0.1 the proper input for "host"? (please note I also tried "localhost")
  2. Does the input for host have to be the same for the client and server code?
  3. Should the RPi and Macbook both be connected to the same WiFi network?
  4. 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)
  5. Do you know why I am receiving this error and how to fix it?

Your help is greatly appreciated!!

like image 278
user3476808 Avatar asked Mar 16 '26 16:03

user3476808


1 Answers

127.0.0.1 is a special IP address for local machine.
You must set the real IP address (on your LAN) of you mac in the client code.
You should also bind on this IP on the server, or on 0.0.0.0 to bind on all available IP addresses.
You must also use the same port number on both client and server.

And to reply to your questions:

Is 127.0.0.1 the proper input for "host"? (please note I also tried "localhost")
127.0.0.1 is the same than localhost, it means the local machine. This will work if you run the client and the server on the same machine, else you need the real IP address of your mac. Try ifconfig in a console.

Does the input for host have to be the same for the client and server code?
Yes and no. On the server you bind to a port and an address, so you'll wait for connections on this port and address. You can use the IP address, or 0.0.0.0.

Should the RPi and Macbook both be connected to the same WiFi network?
Yes and no. It will work with the same WiFi network, but it will also work with two different WiFi networks if they are connected together directly or with a IP router. Most of the time though they are connected to the internet through a NAT (network address translator), and then it will not work.

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)
I don't know much about the RPi but it looks like standard TCP sockets, that should work out of the box.

Do you know why I am receiving this error and how to fix it?
As I stated at the beginning: You try to connect to the RPi (127.0.0.1) on the wrong port.

like image 63
Nicolas Defranoux Avatar answered Mar 18 '26 06:03

Nicolas Defranoux