c++ socket编程中的多线程处理问题? 70
WORDwVersionRequested;WSADATAwsaData;interr;wVersionRequested=MAKEWORD(1,1);err=WSASt...
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(1, 1);
err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0) {
return 0;
}
if (LOBYTE(wsaData.wVersion) != 1 ||
HIBYTE(wsaData.wVersion) != 1) {
WSACleanup();
return 0;
}
SOCKET sockSrv = socket(AF_INET, SOCK_STREAM, 0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(30000);
bind(sockSrv, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
listen(sockSrv, 5);
SOCKADDR_IN addrClient;
int len = sizeof(SOCKADDR);
SOCKET sockConn = accept(sockSrv, (SOCKADDR*)&addrClient, &len);
char recvBuf[50];
int state = 0;
//上面的代码可忽略
while (1)
{
memset(recvBuf, 0, sizeof(recvBuf));
recv(sockConn, recvBuf, 50, 0);
if (!strcmp(recvBuf, "wup")){
state = 1;
fun(state); //example
}
...
}
我想另开一个线程 响应state, 主线程可以根据客户端发来的字符串信息改变state,另一个线程完成相应动作,请问另一个线程代码怎么写
假设有这种情况 传入state=1时,另一个线程会重复a操作,但一旦传入state=2,a操作就停止了,执行其他操作,请问代码怎么写 展开
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(1, 1);
err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0) {
return 0;
}
if (LOBYTE(wsaData.wVersion) != 1 ||
HIBYTE(wsaData.wVersion) != 1) {
WSACleanup();
return 0;
}
SOCKET sockSrv = socket(AF_INET, SOCK_STREAM, 0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(30000);
bind(sockSrv, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
listen(sockSrv, 5);
SOCKADDR_IN addrClient;
int len = sizeof(SOCKADDR);
SOCKET sockConn = accept(sockSrv, (SOCKADDR*)&addrClient, &len);
char recvBuf[50];
int state = 0;
//上面的代码可忽略
while (1)
{
memset(recvBuf, 0, sizeof(recvBuf));
recv(sockConn, recvBuf, 50, 0);
if (!strcmp(recvBuf, "wup")){
state = 1;
fun(state); //example
}
...
}
我想另开一个线程 响应state, 主线程可以根据客户端发来的字符串信息改变state,另一个线程完成相应动作,请问另一个线程代码怎么写
假设有这种情况 传入state=1时,另一个线程会重复a操作,但一旦传入state=2,a操作就停止了,执行其他操作,请问代码怎么写 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询