对于类赋值,我需要使用套接字API来构建文件传输应用程序。对于这个项目,有两个与客户端和服务器的连接,一个称为控件,用于发送错误消息,另一个用于发送数据。我的问题是,在客户端如何保持控制套接字打开并等待从服务器接收任何可能的错误消息,同时不阻止程序的其余部分运行?
示例代码(删除了一些元素)
#Create the socket to bind to the server
clientSocket = socket(AF_INET,SOCK_STREAM)
clientSocket.connect((serverName,portNum))
clientSocket.send(sendCommand) # Send to the server in the control connection contains either the list or get command
(If command is valid server makes a data connection and waits for client to connect)
clientData = socket(AF_INET,SOCK_STREAM)
clientData.connect((serverName,dataport)) #Client connects
recCommand = clientData.recv(2000) #Receive the data from server if command is successful
badCommand = clientSocket.recv(2000) #But if there is an error then I need to skip the clientData.recv and catch the error message in bad Command
答案 0 :(得分:0)
当出现错误时,服务器应关闭数据套接字,因此recv会自动结束。