Can i not send and receive UDP packets in a single python embedded C++ program in which send and receive scripts run in different threads? I get an unhandled exception when the exe file is run. When one of the send or receive command is commented out i.e either PyRun_SimpleString(sendPy) or PyRun_SimpleString(recPy) then the program works all fine. What is the mistake here?
The code is given below:
DWORD WINAPI sendPack(LPVOID iValue)
{
while(1){
const char* sendPy = "UDPSockSend.sendto('10707',('10.107.35.167',21567))";
PyRun_SimpleString(sendPy);
}
return 0;
}
DWORD WINAPI receive(LPVOID iValue){
while(1){
Py_Initialize();
recPy = "data,addr = UDPSockRcv.recvfrom(99000)";
PyRun_SimpleString(recPy);
}
return 0;
}
int threads()
{
HANDLE sendPackThread, receiveThread;
DWORD dwGenericThread;
char lszThreadParam[4];
receiveThread =
CreateThread(NULL,0,receive,&lszThreadParam,0,&dwGenericThread);
if(receiveThread == NULL){
DWORD dwError = GetLastError();
return 0;
}
sendPackThread =
CreateThread(NULL,0,sendPack,&lszThreadParam,0,&dwGenericThread);
if(sendPackThread == NULL){
DWORD dwError = GetLastError();
std::cout<<"SCM:Error in Creating send sample thread"<<dwError<<"\n" ;
return 0;
}
return 1;
}
int main(int argc, char* argv[])
{
using namespace std;
Py_Initialize();
const char * initPy = "import socket;
UDPSockSend = socket.socket(socket.AF_INET,socket.SOCK_DGRAM);
UDPSockRcv = socket.socket(socket.AF_INET,socket.SOCK_DGRAM);
listen_addr = ('',2000);UDPSockRcv.bind(listen_addr)";
PyRun_SimpleString(initPy);
int thd = threads();
system("pause");
return 0;
}
Thanks in advance