1

I am using multithreading sockets to communicate between a python application and a C++ application and am running into issues when starting a second thread after pressing a button, while the first thread is running.

Python application code:

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 Server Code:

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()   

The first time I instantiate the GetJointsFromPSR it works perfectly and starts the first thread, but when I instantiate SendJointValuesToPSR and try to go back to the first thread, I get the error (which I am assuming means that the thread has been closed somewhere). I am only closing thread # 2 so I am unsure how thread # 1 is being affected.

Any tips would be appreciated, thanks!

C++ application code: I don't think the problem lies here, but here it is anyway.

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); 
}
akivjh
  • 19
  • 6
  • If you have problems with a Qt GUI (PySide, or PyQt) application, you should probably edit your question to include relevant tags. – J.J. Hakala Jul 19 '16 at 15:57

0 Answers0