So I am trying to compare the buffer to a string with strcmp. I want the server to respond in a special way when something the client says "man" and otherwise just reply with got it. I cant see what i am doing wrong, but my output is always "got it" and not man even when i input man from the client side.
void dostuff (int sock)
{
int n;
char buffer[256];
bzero(buffer,256);
n = read(sock,buffer,255);
//make list match each get text and match to user
if (n < 0) error("ERROR reading from socket");
if (!strcmp ( buffer, "man" )) n = write(sock,"you are manly",18);
else{ printf("Here is the message: %s\n",buffer);
n = write(sock,"got it",18);};
//n = write(sock,"you are gay",18);
if (n < 0) error("ERROR writing to socket");
/*
if (n < 0) error("ERROR reading from socket");
printf("Here is the message: %s\n",buffer);
n = write(sock,"got it",18);
if (n < 0) error("ERROR writing to socket");*/
}
The problem is that you are using fgets()
in the client to acquire the string to send to the server. The string will have a \n
and the end and won't match "man".
To remove the \n
at the end of the buffer you can do something like this:
strtok (buffer, "\n");
which will replace the \n
with a NULL
so your strcmp()
will work.