[英]what's the best way to store stream of data over tcp into large array?
我想通過TCP連接將數據流存儲到大型陣列中,我該怎么做?
我的代碼:
int iResult, count;
int recvbuflen = 512;
char buff[4096]={0};
char recvbuf[512] = {0};
.................
count = 0;
do {
iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
if (iResult > 0) {
count+=iResult;
//code to store in the buff[] array until reach to 4096 byte
//that's what i need
//for example: each time bind or add the recvbuf[] array at
//the end of buff[] array until reach to 4096 byte.
if(count == 4096)
{
//do the next process
count = 0;
}
}
}while(iResult > 0);
任何幫助。
你可以直接回到你的大緩沖區並每次添加一個偏移量:
iRes = recv(ClientSocket, (buff+offset), 4096-offset, 0);
等等。注意不要溢出緩沖區。 如果您需要單獨接收數據並根據內容將它們添加到緩沖區,只需將recvbuf memcpy到緩沖區(帶偏移量)。 偏移量只是跟蹤,直到緩沖區已經填滿。 再次,留意緩沖區溢出。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.