So i'm currently working on a app that uses socket, for now everything is working, i just want to know if there is a more efficient way to send the length of the data and the data instead of sending twice.
i.e: Sending everything in one string.
void SendPackage(const SOCKET sock, const std::string package)
{
int length = package.lenth();
send(sock, std::to_string(length).c_str(), 10, 0); //Buffer length is 10 assuming data length
send(sock, package.c_str(), length, 0); //will never be greater than 9,999,999,999
}
void ReceivePackage(const SOCKET sock, std::string &package, int bufferLength)
{
std::vector<char> recvBuffer(10);
int length, bytesProcessed;
recv(sock, &recvBuffer[0], 10, 0); //Receiving length
length = atoi(&recvBuffer[0]);
recvBuffer.resize(length);
for (int i = 0; i < length; i += bytesProcessed)
{
bytesProcessed = recv(sock, &recvBuffer[0] + i, bufferLength, 0);
if (bytesProcessed < 0) break;
}
package = &recvBuffer[0];
}