Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to socket communication between raspberry pie and cloud server

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report