第一種方式:
// 向伺服器傳送資料
socket soc = *( ks->getsocket() );
char xmllen[4]; // 表示包體長度的串
char redstr[1024]; // 乙個完整的資料報
// char sendbuffer[512]; // 發緩衝
int bodylength = 0; // 包長度
int ret; // socket收的返回值
char redbuffer[51200]; // 讀緩衝
int xmlindex = 0; // 處理緩衝的最後位
char xmlbuffer[102400]; // 處理緩衝
// 迴圈堵塞收資料
while(1)
for(int i = 0; i < ret; i++)
xmlindex += ret;
int redindex = 0;
while(1)
if(bodylength == 0)
bodylength = char2int(xmllen,4);
redindex += 5;
}if(( xmlindex - redindex) >= bodylength)
redstr[bodylength] = '/0';
redindex += bodylength;
bodylength = 0;
}else
xmlindex = count;
break;}}
第二種方式:
/*
nreadlen = recv(soc, ver, 1, 0);
if (socket_error == nreadlen)
if (0 == nreadlen)
// 訊息的主體長度
nreadlen = recv(soc, xmllen, 4, 0);
if (socket_error == nreadlen)
if (0 == nreadlen)
bodylength = char2int(xmllen,4);
cstring ss = "";
ss.format("%d",bodylength);
// log::info("包長度");
// log::info(ss);
nreadlen = recv(soc, redbuffer, bodylength, 0);
if (socket_error == nreadlen)
if (0 == nreadlen)
redbuffer[bodylength] = '/0';
cstring sss = "";
sss.format("%s",redbuffer);
log::info(sss);
// cstring redmsg = evtsocket(sss);
cstring redmsg = "";
int ndatalen = 0;*/}
return 0;
第三種方式
lresult cmysocketclient::onclientreceive(wparam wparam, lparam lparam)
m_receivedbytessizelast=m_receivedbytessize+m_databuffersizelast; //socket 收到的資料 與上次流下的資料總和
char* tempbytes = new char[m_receivedbytessizelast];
memset(tempbytes, 0, sizeof(char)*m_receivedbytessizelast );
trymemcpy(tempbytes,databuffer,m_receivedbytessize);
encodingbytes(tempbytes, m_receivedbytessizelast);
}catch(std::exception &e)
delete tempbytes; //socket 收到的資料 與上次流下的資料總和
}catch(std::exception &e)
delete databuffer; //socket 收到的資料
}break;
case fd_close:
closesocket(m_hsocket);
afxmessagebox("伺服器退出。");
break;
}return 0l;
}int cmysocketclient::encodingbytes(char totalbytes, int totalbytescount)
char tempheadbytes[4]; //第一位是版本號 ,第二到第5是包的長度
char a;
for (int i = 0; i < 4; i++)
int temppacklen = chartoint(tempheadbytes, 4); //每個包的長度
if ((temppacklen + dataheadlen) > totalbytescount) //如果接收的資料還達不到定義的長度返回
char* temppackbytes = new char[temppacklen+1];
memset(temppackbytes, 0, sizeof(char)*temppacklen );
//char a;
for (int i = 0; i < temppacklen; i++)
temppackbytes[temppacklen]='/0';
//cstring str=temppackbytes; // str1.format("%s", temppackbytes); str11=temppackbytes (char*)temppackbytes;
m_callbackoutput->execute((void*)temppackbytes);
delete temppackbytes;
if ((temppacklen + dataheadlen) < totalbytescount) //第一次取完後還的內容處理
}catch(std::exception &e)
return 0l;
通訊協議包
packet分為 head 和 body head為12個位元組,前8個位元組是重要資訊,包括標誌 是否壓縮 伺服器id 是否公升級等字段,後4個位元組是body的長度 body有9byte,命令id是4byte,引數選項1byte 表數量4byte 然後是所有的表 表 字段型別有byte int ...
UDP通訊寫包發包收包
void cmainframe senddata int cindex,cstring changeddata 點的個數和名字陣列if lobyte wsadata.wversion 1 hibyte wsadata.wversion 1 socket sockclient socket af in...
TCP通訊(二) 非同步連線
本篇部落格主要講述的非同步的問題。首先明確非同步到底是什麼東東,非同步就是發起乙個指令,並不需要一直等待指令的執行結果,而是可以繼續忙其他的事情。一 非同步連線 服務端 using system using system.collections.generic using system.linq u...