1

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 ?

Nathos8
  • 27
  • 6
  • What about first making bye print what it receives on stdin? There might be a newline (`"Hello\n"`) transmitted which makes the strcasecmp fail. – Jens Mar 15 '18 at 19:38
  • 2
    If you are already programming in C I think the answer is obvious here and there is a lot of sample code on the net you could use for a simple tcp server and client. I feel like we are missing an important part of the question however; like why you need to do this way and what problem you are trying to solve. I could post the code here but I would only being googling it in your stead. Perhaps this thread can get you started. https://stackoverflow.com/questions/662328/what-is-a-simple-c-or-c-tcp-server-and-client-example – Josiah DeWitt Mar 15 '18 at 20:34
  • ..."doesn't seem to work"? Note that our guidelines call for a question to include *a specific problem or error*, and the shortest code necessary to reproduce it. (Ideally, you should isolate whether you want help with the bash part or the C part *before* you ask the question, and include only one). – Charles Duffy Mar 15 '18 at 20:52

1 Answers1

1

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."

Werner
  • 281
  • 1
  • 12