Hi to all,
I'm programming a very simple socket server following this sample code. The server is up and running and I can connect to it via telnet (using Putty, actually). Whenever a new connection is received, I spawn a new thread that is the following code:
DWORD WINAPI receive_cmds(LPVOID lpParam)
{
char outbuffer[100];
outbuffer[0] = '\0';
char inbuffer[100];
inbuffer[0] = '\0';
int res;
SOCKET current_client = (SOCKET)lpParam;
while (1) {
res = recv(current_client, inbuffer, sizeof inbuffer - 1, 0);
if (res > 0) {
inbuffer[res] = '\0';
printf("Got '%s'\n", inbuffer);
}
Sleep(10);
if (res == 0) {
closesocket(current_client);
ExitThread(0);
}
//printf("%s\n", inbuffer);
strcpy(inbuffer, "");
}
}
As soon as I connect it prints this gibberish:
If i input "hello" and then "stackoverflow" in the client, this is what I get:
Even if I declared explicitly the end of line '\0', it seems to take much more of the line, plus I don't know why it prints the input two times, the second being the first one minus the first one or two characters.
Any help on understanding what is going on? Thanks in advance.
EDIT: edited accordingly to what suggested by unwind