c++ socket error 10038错误
socket error 10038错误是说socket无效或者被kill掉了。
原因:
错误代码:
-
int RecvThread(SOCKET* lpParameter,py::function caminfocall) {
-
SOCKET *sockClient = lpParameter;
-
while (1) {
-
RecStruct data_recv;
-
int ret;
-
memset(&data_recv, '0', sizeof(struct RecStruct));
-
ret = recv(*sockClient, (char *)&data_recv, sizeof(struct RecStruct), 0); //第二个参数使用强制类型,为一个数据包
调用代码:
-
std::thread thread1(RecvThread, sockClient, caminfocall);
-
thread1.detach();
正确做法:直接传sockClient,别传指针。
DWORD WINAPI RecvThread(LPVOID lpParameter){
SOCKET sockClient = (SOCKET)lpParameter;
if (connect(sockClient, (struct sockaddr*)&addrSrv, sizeof(addrSrv)) == -1)
return -2;
//throw "连接失败";
if (SOCKET_ERROR == sockClient) {
printf("Socket() error:%d", WSAGetLastError());
return -3;
}
g_sockClient = sockClient;
HANDLE h_thread = CreateThread(NULL, 0, RecvThread, (LPVOID)&sockClient, 0, NULL);
//CloseHandle(h_thread);
原因:sockClient本身就是指针,结果把指针地址传进去了,正确做法:
把HANDLE h_thread = CreateThread(NULL, 0, RecvThread, (LPVOID)sockClient, 0, NULL);
/*
*每个客户端创建一个接收线程
*/
hThread = CreateThread(NULL, 0, ClientThread,
(LPVOID)sClient, 0, &dwThreadId);
if (hThread == NULL)
{
printf("CreateThread() failed: %d/n", GetLastError());
break;
}
文章来源: blog.csdn.net,作者:网奇,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/jacke121/article/details/79409762
- 点赞
- 收藏
- 关注作者
评论(0)