Python Socket在PC和RPi之间通信



我正在尝试制作一个小游戏,其中一部分是通过笔记本电脑向树莓派发送国家名称。现在我只是发送一个字符串,但稍后该字符串将用于从连接到RPi的笔绘图仪中绘制图像。

我在使用Python套接字发送和接收数据时遇到麻烦(完全初学者)。我要做的是使服务器不断发送国家名称,直到客户端接收到它,然后从客户端发送一个信号回服务器,它已经收到了国家名称,使服务器停止发送它,并切换到监听模式。客户端做一些处理,现在只是打印(绘图国家{country_name}"),然后向服务器发送信号,表示完成。

不幸的是,两个程序都在运行,但什么也没发生。我可以从RPi ping笔记本电脑,反之亦然。此外,这个链接中的程序为我工作:两台计算机之间使用python套接字的通信

我的服务器端和客户端代码如下:-

服务器端(笔记本电脑)

#Import the necessary libraries
from socket import socket, gethostbyname, AF_INET, SOCK_DGRAM
import sys
import time
#Designate a port over which the communication will happen
PORT_NUMBER = 5000
#Protocol ?
SIZE = 1024
#Get the name of the host, this is our laptop
SERVER_NAME = gethostbyname('')
#Create the server socket, this is our laptop
Server_Socket = socket( AF_INET, SOCK_DGRAM )
Server_Socket.bind( (SERVER_NAME, PORT_NUMBER) )
#Set the client IP, this is our raspberry pi
CLIENT_IP = "192.168.88.244"
print ("Test server listening on port {0}n".format(PORT_NUMBER))
#Create a variable that will become True if the client responds
client_response = False
#Start of while loop to keep sending data until client responds
while client_response == False:
#Create a delay so the client is not overwhelmed
time.sleep(3)
Server_Socket.sendto(b"Country_Name", (CLIENT_IP, PORT_NUMBER))
(client_response,addr) = Server_Socket.recvfrom(SIZE)
print(client_response)
#End of while loop to keep sending data until client responds
#Set client_response again to false
client_response = False
#Start of while loop to listen until client has done the task, this is our raspberry pi finishing the drawing
while client_response == False:
(client_response,addr) = Server_Socket.recvfrom(SIZE)
#End of while loop to listen until client has done the task, this is our raspberry pi finishing the drawing
#Close the server socket
Server_Socket.close()
print("Done")
sys.ext()

客户端(树莓派)

#Import the necessary libraries
from socket import socket, gethostbyname, AF_INET, SOCK_DGRAM
import sys
import time
#Set the server IP, this is our laptop
SERVER_IP = '192.168.88.250'
#Designate a port over which the communication will happen
PORT_NUMBER = 5000
#Protocol ?
SIZE = 1024

#Create the cleint socket, this is our raspberry pi
Client_Socket = socket( AF_INET, SOCK_DGRAM )
Client_Socket.bind( (SERVER_IP, PORT_NUMBER) )
#Set a variable that becomes True if the data sent by server is received
server_data_rcvd = False
#Start of while loop to listen until data from server is received
while server_data_rcvd == False:
#Create a delay so the client is not overwhelmed
time.sleep(1)
#Store country name
(country_name, addr) = Client_Socket.recv(size)
print("country_name")
#Start of if statement to check if country name is received
if type(country) == str:
server_data_rcvd = True
#Send response to the server
Client_Socket.sendto(True, (SERVER_IP, PORT_NUMBER))
#End of if statement to check if country name is received
#End of while loop to listen until data from server is received
#Draw the country
print(f"Drawing country {country_name}")
Client_Socket.sendto(True, (SERVER_IP, PORT_NUMBER))
#Close the socket
Client_Socket.close()
sys.exit()

我犯了什么错误?我怎样才能使代码更高效、更易读呢?

提前感谢!

客户端必须使用socket.connect连接到服务器,而不是socket.bind

#Create the cleint socket, this is our raspberry pi
Client_Socket = socket( AF_INET, SOCK_DGRAM )
Client_Socket.connect( (SERVER_IP, PORT_NUMBER) )

除了几个拼写错误之外,还有许多问题可能导致您的问题(例如:size vs size, country vs country_name, sys。Ext vs sys。退出等).

UDP vs TCP

首先,您使用的是SOCK_DGRAM它建议UDP作为协议而不是TCP。只要这是你想要的就行。UDP是无连接的,TCP要求你的客户端connect

到你的服务器。 <<h2>字节/h2>如果您正在使用Python3,recv(buffersize[, flags]) -> data返回字节,而不是数据的元组和地址。如果您更新该行以接受国家,那么您将需要通过country.decode('utf-8')

将其转换为字符串如果你使用Python3,sendto(data[, flags], address) -> count需要数据(不是布尔值)。您可以通过b'True''True'.encode('utf-8')发送字节。

阻塞recv

我看到的最大的问题,可能是什么导致你最麻烦的是你的while循环是有问题的。不仅你的服务器中的while循环永远不会退出,而且recv是一个阻塞动作,所以如果你要启动服务器,然后启动你的rpi,它们都将被困在recv,两者都不会发送。在您当前的设置中,您必须首先启动rpi,以便它可以侦听,然后启动服务器,以便它可以将国家/地区发送给rpi。否则,它们都处于死锁状态,因为它们都在等待另一端发送消息。

最新更新