My goal is create an app client server, written in C++. When the server read an input from the client, should process the string and give an output. Basically, I have a simply echo server that send the same message. But if the user types a special string (like "quit"), the program have to do something else. My problem is that this one dont happend, because the comparison between strings is not working... I dunno why! Here a simple code:
while(1) {
int num = recv(client,buffer,BUFSIZE,0);
if (num < 1) break;
send(client, ">> ", 3, 0);
send(client, buffer, num, 0);
char hello[6] ="hello";
if(strcmp(hello,buffer)==0) {
send(client, "hello dude! ", 12, 0);
}
buffer[num] = '\0';
if (buffer[num-1] == '\n')
buffer[num-1] = '\0';
std::cout << buffer;
strcpy(buffer, "");
}
Why the comparison is not working? I have tried many solutions...but all failed :(
bufferis not null-terminated, you should not use it in string functions before you null-terminate it yourself. Also print it out on receive, there might be characters you haven't thought about after the text (new-line for instance).