我编写了一个嵌入在 C++ 中的 Python 脚本,用于套接字编程。我有两个可以发送和接收的终端系统。发送和接收操作发生在不同的 C++ 线程中。程序构建良好,但运行 .exe 文件时遇到一个奇怪的错误。
最初我怀疑这是由于发送和接收套接字是相同的。我也绑了不同的插座,但徒劳无功。
谁能告诉我这里的问题是什么?
代码如下所示:
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;
}
提前致谢