码迷,mamicode.com
首页 > 其他好文 > 详细

TCP粘包和半包的处理方法

时间:2015-05-29 17:28:13      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:

先把处理的方法的代码放这里:

解析数据帧的代码:

bool CSocket::findData(byte* buff, int& len)
{

	for (int i = 0; i <= len - 4; i++)
	{
		int dataLen;
		if(buff[i] == 0xAA && buff[i + 1] == 0xBB)
		{
			short slen;
			slen = (short)(byte(buff[i+3]));
			slen |=(short)((short)(byte(buff[i+2])) << 8);
			dataLen = slen;
			if(slen < 0)
				return false;

			if(i+4 +dataLen + 4 <= len)
			{
				if(buff[i + 4 + dataLen] == 0x0DD && buff[i + 4 + dataLen + 1] == 0xEE && buff[i + 4 + dataLen + 2] == 0xDD && buff[i + 4 + dataLen + 3] == 0xEE)
				{
					MsgType type;
					memcpy(&type, buff + i + 4 , sizeof(type));

					if(type == ID_LASER)
					{}
					else if(type == ID_PATH)
					{}
					else if(type == ID_POSE)
					{}
					len = len - i - 4 - dataLen - 4;
					if( len > 0)
					{
						memcpy(swapBuff, buff + i + 4 + dataLen + 4, len);
						memcpy(buff, swapBuff, len);
					}
					return true;
				}
				else
				{
					len = len - i -4;
					if(len > 0)
					{					
						memcpy(swapBuff, buff + i + 4, len);
						memcpy(buff, swapBuff, len);
						return false;
					}
				}
			}//if
			else if(i != 0)
			{
				len = len - i;
				memcpy(swapBuff, buff + i, len);
				memcpy(buff, swapBuff, len);
				return false;
			}
		}//if
	}//for
	
	return false;
}//end function

  

接收数据帧的代码:

void CSocket::receiveData()
{
  ROS_INFO("Receive Thread Start...");

	int len =0;
	int revDateCount = 0;
  while(bRunning)
  {
    if(!connected)
    {
        ROS_INFO("Restarting connection...");
		ErroFunc(_T("连接中断,尝试重新连接..."));

        if(serverSocket > 0)
        {
          closesocket(serverSocket);
          serverSocket = -1;
        }

        if(clientSocket > 0)
        {
          closesocket(clientSocket);
          clientSocket = -1;
        }

        bool re;
        if(bServer)
          re = startServer();
        else
          re = connectServer();
        if(!re)
            continue;
		revDateCount = 0;
		MsgFunc(_T("连接到机器人!"));
    }

    int re = selectSocket(clientSocket, 500);
    if(re < 0)
    {
      ROS_ERROR("Receive Thread Select Error!");
      connected = false;
      continue;
    }
    else if(re == 0)
      continue;
    else
    {
      len  = recv(clientSocket, (char*)recvBuffer, MAX_BUFFER_LEN, 0);
      if(len > 0)
      {
		if(revDateCount + len < MAX_BUFFER_LEN)
		{
			memcpy((void*)(buff + revDateCount), (void*)recvBuffer, len);
			revDateCount += len;
		}
		else
		{
			memcpy((void*)buff, (void*)recvBuffer, len);
			revDateCount = len;
		}
		
		while(findData(buff, revDateCount))
		{
			ROS_INFO("Receive data!");
		}		
      }
	  else
      {
        ROS_ERROR("Receive Data Error!");
        connected = false;
        goto __reconnect__;
	  }
	}
    __reconnect__:;
  }
  ROS_INFO("Receive Thread Stop.");
}

  

TCP粘包和半包的处理方法

标签:

原文地址:http://www.cnblogs.com/stemon/p/4538693.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!