Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4151

Networking and servers • Re: Socket programming on rpi4 or rpi5

$
0
0
Sockets are actually pretty easy to use, and I've done pretty much exactly what you ask before. There's some complexity around whether or not you want ONE connection or many, but the basics are very straight forward. Note - this is untested pseudocode, but should be close if not functional, sorry!

Server side:

Code:

import socketclass socketServer:def __init__(self):# create an INET, STREAMing socket to listen on        self.serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)        self.serversocket.settimeout(1000)# you'll want to handle this better                # bind the socket to public host and port        self.serversocket.bind(("0.0.0.0", 1234))#1234 is the port used here, but whatever you like        #become a server socket, for 10 connections max        self.serversocket.listen(10)                  while 1:        # do something with that connection        (clientsocket, address) = self.serversocket.accept()        self.process(clientsocket, address)                def process(self, clientsocket, address):        # receive info from client        msgLen = 0        payload = ""                while 1:# you probably want to limit this so it doesn't block forever                chunk = clientsocket.recv(1024).decode("utf-8")                if chunk == "" or msgLen >= 1024:  # we're done... max 1024 bytes in this case                    break                else:                    payload = payload + chunk                    msgLen += len(chunk)                          # we now have info from the client        response = self.doSomething(payload)                # send response        clientsocket.sendall(response.encode("utf-8"))                clientsocket.close()                def doSomething(payload):        return "this is the response to the client"        if __name__ == "__main__":a = socketServer()# this will block
client side:

Code:

import socketdef talkToServer(message):# get a socket connection to the serverconnection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)        connection.connect(("192.168.1.234", 1234))# IP address and port of the server                # send the message        connection.send(message.encode("utf-8"))        # shut down the send, so the server knows we're done talkingconnection.shutdown(socket.SHUT_WR)# get the response from the servermsgLen = 0        payload = ""                while 1:# you probably want to limit this so it doesn't block forever                chunk = clientsocket.recv(1024).decode("utf-8")                if chunk == "" or msgLen >= 1024:  # we're done... max 1024 bytes in this case                    break                else:                    payload = payload + chunk                    msgLen += len(chunk)connection.close()return payload, msgLenif __name__ == "__main__":response, msgLen = talkToServer("Hello Dolly!")print (response)

Statistics: Posted by MadCow42 — Tue Jan 30, 2024 10:00 pm



Viewing all articles
Browse latest Browse all 4151

Trending Articles