我正在使用多线程套接字在python应用程序和C ++应用程序之间进行通信,并且在按下按钮后启动第二个线程时遇到问题,而第一个线程正在运行。
Python应用程序代码:
def GetJointsFromPSR(self):
if (self.PSRRecvThread == 0):
self.PSRRecvThread = ServerPSR.StartThread()
while True:
j = self.PSRRecvThread.receiveJointData()
if (self.PSRRecvThread.status == 0):
break
QApplication.processEvents()
self.UpdateJointFields(j)
self.MovePSRModel()
def SendJointValuesToPSR(self):
self.PSRSendThread = ServerPSR.StartThread()
PSRjoints = self.GetJoints(self.PSRTargetJointFields)
self.PSRSendThread.sendJointData(PSRjoints)
QApplication.processEvents()
self.PSRSendThread.stopAcq()
if (self.PSRRecvThread != 0):
self.GetJointsFromPSR()
Python服务器代码:
def receiveJointData(self):
self.sock.sendall('Send Data\0')
data = self.sock.recv(4096)
self.checkStatus(data)
if (self.status == 1):
return self.createJointArr(data)
def sendJointData(self, joints):
self.sock.sendall('Receive Data\0')
data = self.sock.recv(4096)
self.checkStatus(data)
sendbuf = " ".join(map(str, joints))
self.sock.sendall(sendbuf)
return
def stopAcq(self): #not connected to socket
self.status = 0
self.sock.close()
我第一次实例化GetJointsFromPSR它完美地运行并启动第一个线程,但是当我实例化SendJointValuesToPSR并尝试返回第一个线程时,我得到错误(我假设意味着线程已经关闭)某处)。我只关闭线程#2,所以我不确定线程#1是如何受到影响的。
任何提示都将不胜感激,谢谢!
C ++应用程序代码:我不认为这里存在问题,但无论如何它都在这里。
unsigned __stdcall MyClient::StartConnection(void *arg)
{
cout << "Start Connection\n";
int argc = 2;
// Validate the parameters
if (argc != 2) {
printf("usage: %s server-name\n", "client");
return 1;
}
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed with error: %d\n", iResult);
return 1;
}
ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// Resolve the server address and port
iResult = getaddrinfo("localhost", DEFAULT_PORT, &hints, &result);
if ( iResult != 0 ) {
printf("getaddrinfo failed with error: %d\n", iResult);
WSACleanup();
return 1;
}
// Attempt to connect to an address until one succeeds
for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {
// Create a SOCKET for connecting to server
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
ptr->ai_protocol);
if (ConnectSocket == INVALID_SOCKET) {
printf("socket failed with error: %ld\n", WSAGetLastError());
//MessageBox(0, (L"The result is %ld", WSAGetLastError()), L"Status Box", MB_OK);
WSACleanup();
return 1;
}
// Connect to server.
iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR) {
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
continue;
}
break;
}
// non-blocking mode (only for sending data to python)
if (iMode == 1)
ioctlsocket(ConnectSocket, FIONBIO, &iMode);
freeaddrinfo(result);
if (ConnectSocket == INVALID_SOCKET) {
printf("Unable to connect to server!\n");
WSACleanup();
return 1;
}
return 1;
}
void MyClient::StartClientThread()
{
isConnected = 1;
hth = (HANDLE)_beginthreadex(NULL,0,StartConnection,NULL,CREATE_SUSPENDED,&uiThreadID);
DWORD dwExitCode;
ResumeThread(hth);
WaitForSingleObject(hth,INFINITE);
}
void MyClient::SendRecvJoints(double *joints)
{
j = parseJSON(0,0,0,0,0,0,0);
iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
if (iResult == 10)
SendJoints(joints);
else if (iResult == 13)
RecvJoints(joints);
//else
//MessageBox(0, L"No data received", L"Status Box", MB_OK);
}
void MyClient::SendJoints (double *joints)
{
j = parseJSON(joints[0],joints[1],joints[2],joints[3], \
joints[4],joints[5],joints[6]);
int x = send(ConnectSocket, j, strlen(j), 0);
iResult = 0;
}
void MyClient::RecvJoints(double *joints)
{
status = 1;
int x = send(ConnectSocket, j, strlen(j), 0);
iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
CreateBuffer(recvbuf);
}