Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to socket communication between raspberry pie and cloud server

Shulou Source: shulou.com Published: 2022-06-01 00:34:53 09月27日 Update

This article mainly introduces the socket communication between raspberry pie and cloud server, which has certain reference value. Interested friends can refer to it. I hope you will learn a lot after reading this article. Let's take a look at it.

First agree on the port number for socket communication: 7000

Open the corresponding port through the server console

Note: the port must be opened in the console, opening the port in the server is not valid!

Server side:

The ip address should be written as the private network ip of the server

The data received is an byte array, so you can use decode to decode, or you can specify a character set to decode. The default is utf-8.

Import socketHOST = 'server's private network ip'PORT = 7000 s = socket.socket (socket.AF_INET, socket.SOCK_STREAM) # bind sockets.bind ((HOST, PORT)) # start listening on s.listen (1) print (' Listening at port:',PORT) conn, addr = s.accept () print ('Connected by', addr) while True: data = conn.recv (1024) data = data.decode () if not data: break print (' Received message:') Data) conn.close () s.close ()

Raspberry pie:

Note that the ip address is written to the server public network ip

Import socketHOST = 'server public network ip' PORT = 7000BUFSIZ = 1024ADDR = (HOST,PORT) tcpCliSock = socket (AF_INET SOCK_STREAM) tcpCliSock.connect (ADDR) while True: data1 = input ('>') # data = str (data) if not data1: break tcpCliSock.send (data1.encode ()) data1 = tcpCliSock.recv (BUFSIZ) if not data1: break print (data1.decode ('utf-8')) tcpCliSock.close () Thank you for reading this article carefully I hope the article "how to communicate with socket between raspberry pie and cloud server" shared by the editor will be helpful to you. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Server service article raspberry communication port address console public network control value interest slogan simultaneous character set data array more friends Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia MariaDB MySQL Xiaomi Microsoft