I need, for a course, to program a simple server that answer "Goodbye" when we tell him "Hello". I tried to do that using netcat, but how can I make a script that listen to the client, test his answer, then print the "Goodbye" ? I tried to do :
netcat -l -p 8080 -e bye
with bye.c :
int main(int argc, char ** argl){
char res[100] ;
while(1){
fgets(res, 100, stdin) ;
if(!strcasecmp(res, "Hello"))
{printf("Goodbye\n") ; return 0 ; } }
}
but it doesn't seem to work. Can you help me ?
Just replace strcasecmp with strncmp(res, "Hello", 5) and it should work.
However, you have to avoid blocking of fgets "It stops when either (n-1) characters are read, the newline character is read, or the end-of-file is reached, whichever comes first."