c++ Winsock 非阻塞接收蓝牙数据
//接收数据
void CMyBlueT::RecvMsg()
{
char Buffer[BUFFERSIZE] = { 0 }; // 文件缓冲区
DeleteFile(m_strPath);
CFile file;
file.Open(m_strPath, CFile::modeCreate | CFile::modeWrite);
{
file.Seek(0, CFile::begin);
memset(&Buffer, 0, BUFFERSIZE);
int size = 0;
int nTimes = 0;
int nRecvTimes = 0;
BOOL bRecv = FALSE;
//设置非阻塞
unsigned long ul = 1;
int ret;
ret = ioctlsocket(m_client_socket, FIONBIO, (unsigned long *)&ul); //设置成非阻塞模式
if (ret == SOCKET_ERROR) //设置失败
{
SetTips(L"设置非阻塞模式失败。");
return;
}
while (1)
{
size = recv(m_client_socket, Buffer, BUFFERSIZE, 0);
if (size > 0)
{
bRecv = TRUE;
file.Write(Buffer, size);
//清空缓存区以便下一次接收
memset(&Buffer, 0, BUFFERSIZE);
}
else
{
nTimes++;
if (bRecv)
{
nRecvTimes++;
}
}
if (nRecvTimes > 10 || nTimes > 100)
{
break;
}
Sleep(50);
}
//阻塞
ul = 0;
ret = ioctlsocket(m_client_socket, FIONBIO, (unsigned long *)&ul); //设置成非阻塞模式
if (ret == SOCKET_ERROR) //设置失败
{
SetTips(L"设置阻塞模式失败。");
return;
}
file.Close();;
}
}
本文地址:https://blog.csdn.net/weixin_37999268/article/details/108772901