[C++] Sending variable amount of data over Winsock
Hello
Client:
Code:
....
string cmd = "dir c:\filequeue > ";
string outputFilePath;
outputFilePath.append(getTempPath());
outputFilePath.append("\\test.txt");
cmd.append(outputFilePath);
system(cmd.c_str()); // dir c:\filequeue > %temp%\test.txt
string content = get_file_contents(outputFilePath.c_str());
send(s, content.c_str(), content.length(), 0);
I'm executing the "dir" command to get a listing of Folders/files of one Folder. Then I read the Output of the file and send it over winsock to the Server.
Now, the Problem is, I don't know how I can handle the recv properly, cause I have to set the buffer size without knowing, how much data is actually transfered. Sometimes maybe no files are in c:\filequeue, sometimes a 100k.
So I tried to make recv as a Loop:
Server:
Code:
...
while (rc != SOCKET_ERROR)
{
printf("\n#");
gets(buf); //please no discussion about gets, I will Change this later ;)
if (strcmp(buf, "ls") == 0){
send(connectedSocket, "LIST", 4, 0);
do{
rc = recv(connectedSocket, recvBuf, 255, 0);
recvBuf[rc] = '\0';
cout << recvBuf;
} while (rc > 0);
}
}
now it works, but as the recv blocks, it will never leave the Loop, even when the Transfer is finished.
What should I do?
I believe I could make unblocking sockets, but that's a bit complicated. Isn't there an easier solution, with malloc'ing the buffer or a Signal when to leave the recv Loop?
I am very new to C/C++, so if possible, explain it dummy-proof ;)