linuxraspberry-pilanpython-socketspeer

How do I create a socket connection from Macbook M1 to raspberry pi on my local network


I am new to socket coding. I just want to send messages from client to server across my local network. I have a Macbook M1 and a Pi4. I put this socket client on the MacBook and give it the IP of the pi4.

import socket

SERVER = "192.168.1.136" # Pi4 IP
PORT = 8080
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((SERVER, PORT))
client.sendall(bytes("This is from Client", "UTF-8"))
while True:
    in_data = client.recv(1024)
    print("From Server :", in_data.decode())
    out_data = input()
    client.sendall(bytes(out_data, "UTF-8"))
    if out_data == "bye":
        break
client.close()

I run this server script on the pi4.

import socket
LOCALHOST = "127.0.0.1"
PORT = 8080
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((LOCALHOST, PORT))
server.listen(1)
print("Server started")
print("Waiting for client request..")
clientConnection, clientAddress = server.accept()
print("Connected clinet :", clientAddress)
msg = ""
while True:
in_data = clientConnection.recv(1024)
msg = in_data.decode()
if msg == "bye":
break
print("From Client :", msg)
out_data = "recieved"
clientConnection.send(bytes(out_data, "UTF-8"))
print("Client disconnected....")
clientConnection.close()

I get the error:

client.connect((SERVER, PORT))
ConnectionRefusedError: [Errno 61] Connection refused

Client and server scripts work as expected if both are run on either the Mac or the pi4.

I've followed a number of tutorials and scoured the web for solutions. the above code is based on https://net-informations.com/python/net/socket.htm.

I have installed telnet on the Mac and the Pi4 to try to narrow down the problem, as suggested by a number of forums. Being new to socket theory I can't be sure I've fully understood some of the jargon.

I was simply hoping to see a connection working between two wirelessly connected devices on my local network in the same way the connection works when server and client are run on the same device

I also have a Pi zero W. I've tried all permutations of client and server between the three devices, taking care to give the client the server device IP in each case. All return the 'connection refused' error.


Solution

  • You are binding your server socket to localhost:

    LOCALHOST = "127.0.0.1"
    PORT = 8080
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.bind((LOCALHOST, PORT))
    

    That means that you cannot connect to your Pi from anywhere other than the Pi itself. If you want to be able to connect to your Pi from somewhere else on the network, then bind to INADDR_ANY (0.0.0.0):

    PORT = 8080
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.bind(('0.0.0.0', PORT))
    

    You can also bind to a specific address on your Pi, as in:

    PORT = 8080
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.bind(('192.168.1.136', PORT))
    

    This is useful if your Pi has multiple IP addresses and you only want to offer the service on a specific address. Using 0.0.0.0 as the bind address makes the service available on any/all addresses.