Question

i wanna to store the stream of data over TCP connection into large array, how can i do that?

my code:

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);

any helping.

Was it helpful?

Solution

You could just directly recv into your big buffer and add an offset every time:

iRes = recv(ClientSocket, (buff+offset), 4096-offset, 0);

Etc. Just watch out not to overflow the buffer. If you need to receive the data separately and add them to the buffer depending on content, just memcpy the recvbuf into buffer (with offset). The offset just keeps track up until which point the buffer is already filled. Again, keep an eye out for buffer overflows.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top