首页 > 代码库 > 自定义网络消息
自定义网络消息
#define MSG_HEAD_LEN 4 //消息头部结构 typedef struct tagMsg { //消息类型 u16 type; //消息体长度 u16 length; }TMsg; void receive(SOCKET s) { char buffer[1024*28]; //CServMsg cMsg; u16 wRecvLen = 0 ; u16 wMsgLen = 0; u16 wRet = 0; while (TRUE) { if(wRecvLen < MSG_HEAD_LEN) { wRet = recv(s, &(buffer[wRecvLen]), MSG_HEAD_LEN - wRecvLen, 0); wRecvLen += wRet; if ( wRecvLen == MSG_HEAD_LEN ) { TMsg *ptMsg = (TMsg*)buffer; wMsgLen = ptMsg.length + MSG_HEAD_LEN; } //break; } else if( wRecvLen < wMsgLen ) { wRet = recv(s, &(buffer[wRecvLen]), wMsgLen - wRecvLen, 0); wRecvLen += wRet; if ( wRecvLen == wMsgLen ) { memset(buffer, 0, sizeof(buffer)) wRecvLen = 0 ; wMsgLen = 0; wRet = 0; } } else { //error } } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。