I'm having problems with byte array of chars. I'm creating file transfer program, which transfers binary data over TCP socket. So, when I'm getting data from chunks, I'm saving them to temporary container and then I have to add somewhere to hold whole data. I have tried std::vector but doesn't work well (or I'm using it wrong.)
size_t nbytes = 0; char buffer[5]; //temporary container. int result = 0; std::vector<char> abc; if (ioctl(sockfd, FIONREAD, (char*)&nbytes) < 0) { printf("[-] Error getting available data.\n"); return -1; } printf("[*] Bytes available: %lu\n", nbytes); while(true) { if(nbytes > sizeof(buffer)) { result = recv(sockfd, buffer, sizeof(buffer), 0); for(int i = 0; i < result; i++) { abc.push_back(buffer[i]); //big data causes memory corruption. } nbytes -= result; continue; } else if(nbytes <= sizeof(buffer) && nbytes != 0) { result = recv(sockfd, buffer, nbytes, 0); for(int i = 0; i < result; i++) { abc.push_back(buffer[i]); //big data causes memory corruption. } break; } else { result = 0; break; } } printf("Data Received: %s", &abc[0]);
abc?