C++ Socket如何设置Accept和Recv的非阻塞 50
C++Socket如何设置Accept和Recv的非阻塞,也就是说有连接才接受,没链接继续往下走,同时在接受了之后,有消息就接收,没消息就继续下一步,求源码...
C++ Socket如何设置Accept和Recv的非阻塞,也就是说有连接才接受,没链接继续往下走,同时在接受了之后,有消息就接收,没消息就继续下一步,求源码
展开
1个回答
展开全部
void* CTCPClient::AUReceive(void *aInstance)
{
struct timeval tv_out;
CTCPClient *pInstance = (CTCPClient *)aInstance;
fd_set sockfd;
pInstance->m_IsExit = false;
char ReceiveDataInfo[1024]={0};
char Temp[4] = {0};
while(pInstance->m_IsExit == false)
{
if(pInstance->m_socket == SOCKETERROR)
{
FD_ZERO(&sockfd);
}
else
{
FD_ZERO(&sockfd);
FD_SET(pInstance->m_socket,&sockfd);
}
fd_set mySet = sockfd;
memset(ReceiveDataInfo,0,1024);
int Max_ID = pInstance->m_socket;
int position=0;
tv_out.tv_sec = 0;
tv_out.tv_usec = 1000;
if(select(Max_ID+1,&mySet,NULL,NULL,&tv_out)>0) //主要这一句
{
long nBytesRead = 0;
unsigned long nBytesToRecv = pInstance->mreceivebuflen -pInstance->hasrecvlen;
pInstance->recvsignal.Wait();
if(pInstance->m_socket == SOCKETERROR)
{
FD_ZERO(&sockfd);
pInstance->recvsignal.Release();
continue;
}
nBytesRead = recv(pInstance->m_socket,(char *)pInstance->mreceivebuf + pInstance->hasrecvlen, nBytesToRecv, 0);
pInstance->recvsignal.Release();
if(nBytesRead == -1 || nBytesRead == 0)
{
pInstance->m_CSocket.SocketClose(pInstance->m_socket);
FD_ZERO(&sockfd);
pInstance->m_socket = SOCKETERROR;
continue;
}
pInstance->hasrecvlen += nBytesRead;
pInstance->m_CLog->ddprintf("CTCPClient","AUReceive",1,"recv a package!");
pInstance->m_CLog->ddprintf("CTCPClient","AUReceive",1,"recvlen is %ld",nBytesRead);
if((pInstance->FindCompletePackage(pInstance->receive,nBytesRead,pInstance->mreceivebuf))==false)
{
continue;
}
//printf("validlen is %d\n",pInstance->validlen);
pInstance->hasrecvlen =0;
if(pInstance->validlen < 8)
{
pInstance->m_CLog->ddprintf("CTCPClient","AUReceive",1,"receive data is error(len is error)!");
continue;
}
for(int i =0 ; i< pInstance->validlen; i++)
{
sprintf(Temp,"%2x-", pInstance->receive[i]);
strcat(ReceiveDataInfo, Temp);
}
pInstance->m_CLog->ddprintf("CTCPClient","AUReceive",1,"recv data is:%s",ReceiveDataInfo);
unsigned int recvSN = Char2Int(pInstance->receive);
position+=4;
unsigned int recvCMD = Char2Short(pInstance->receive + position);
position+=2;
unsigned long buflen = Char2Short(pInstance->receive + position);
position+=2;
if(pInstance->validlen - position != buflen)
{
pInstance->m_CLog->ddprintf("CTCPClient","AUReceive",1,"receive data is error(len error)!");
continue;
}
stCommand newReceiveCommand;
newReceiveCommand.CmdSN = recvSN;
//newReceiveCommand.DataBuffer = "";
bool rest = pInstance->FindSentCommand(newReceiveCommand);
if(rest == false)
{
pInstance->m_CLog->ddprintf("CTCPClient","AUReceive",1,"receive data is error(SN can not find)!");
continue;
}
if(newReceiveCommand.CmdCode != recvCMD)
{
pInstance->m_CLog->ddprintf("CTCPClient","AUReceive",1,"receive data is error(CMD is error)!");
continue;
}
char *RecvData = new char[buflen];
memcpy(RecvData,pInstance->receive+position,buflen);
pInstance->UpdateSentCommand(recvSN,buflen,RecvData);
newReceiveCommand.WaitEvent->Release();
pInstance->m_CLog->ddprintf("CTCPClient","AUReceive",1,"send a signal of recv!");
delete[] RecvData;
}
else
Csleep(100);
}
//pInstance->m_ThdRecv.ThreadExit();
return NULL;
}
这个是recv设置非阻塞的方式,accept也是差不多
追问
编译不过
追答
。。。。我只是给你提供一个方式,这段代码当然编译不过,里面有太多的我自己的东西,很多自己定义的变量
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询