【发布时间】:2013-07-09 08:54:30
【问题描述】:
我想通过 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);
任何帮助。
【问题讨论】:
-
你有正确的原则,但你应该在循环的下一次迭代中收到
iResult字节less。否则,您可能会在第一次调用(第一次迭代)中收到4095字节,在下一次调用(下一次迭代)中收到4096,这不是您想要的。 -
不知道是接收到的数据不超过4096字节。
-
@abdo.eng2006210:不,您每次最多会收到 512 个字节,因此请准备好在已经收到的字节之后复制您收到的字节数。通常,您还应该检查您总共可以接收超过 4096 个字节。
-
我每次都接收 512 字节或更少字节的流,所以我需要从接收到的数据中收集每 8 个并将它们存储在大 buff[4096] 数组中。
标签: c++ c visual-c++ tcpsocket