2016-03-06 76 views
0

对于类分配,我需要使用套接字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

尝试使用线程 – Cosinux

回答

0

当有错误时,数据插座应当由服务器关闭,因此recv的自动结束。