title: How to handle 3 way send() and recv() in BSD socket using C
body: <p>After sending "wrong" username - client won't start loop from beginning, actually, there is no <code>server asks:?</code></p>
  
  <p>Dunno how to handle 3 way client-server message sender for such auth. I must understand this to continue such message receiving in further.</p>
  
  <p><strong>client.c:</strong></p>
  
  <pre><code> int is_authenticated = 0;
   size_t sendline_s;
  
   while (!is_authenticated) {
      recv(sockfd, recvline, MAXLINE, 0);
      printf("%s", "server asks:");
      fputs(recvline, stdout);
      printf("?&gt; ");
      fflush(stdout);
      while (fgets(sendline, MAXLINE, stdin) != NULL) {
          sendline_s = strlen(sendline);
          if (sendline[sendline_s-1] == '\n') {
              sendline[sendline_s-1] = '\0';
              send(sockfd, sendline, sendline_s+1, 0);
              puts("username sended");
              break;
          }
      // handling ^Z (EOF) here
      //
      }
      recv(sockfd, recvline, MAXLINE, 0);
      printf("\nawaiting for server ACK\n");
      puts(recvline);
      if (strcmp(recvline, "ACCEPTED_AUTH") == 0) {
          puts("authentication complete successful");
          is_authenticated = 1;
      }
      else {
          puts("authentication declined");
      }
   }
  </code></pre>
  
  <p><strong>server.c</strong></p>
  
  <pre><code>  int is_authenticated = 0;
    char *accepted = "ACCEPTED_AUTH";
    char *name = "kaldown";
    char *wrong = "wrong";
    size_t name_s = strlen(name);
    size_t accepted_s = strlen(accepted);
    size_t wrong_s = strlen(wrong);
  
    while (!is_authenticated) { 
      send(connfd, name, name_s+1, 0);
      puts("authentication request was send");
      recv(connfd, buf, MAXLINE, 0);
      printf("username was recieved: ");
      puts(buf);
      if (strcmp(buf, name) == 0) {
          puts("hurray");
          send(connfd, accepted, accepted_s+1, 0);
          is_authenticated = 1;
          //break;
      }
      else {
          puts("WRONG NAME");
          send(connfd, wrong, wrong_s+1, 0);
      }
    }
  </code></pre>
  
  <p>But, If i send right username - it passes the block and everything goes well.</p>
  
  <p><a href="https://i.stack.imgur.com/cnB92.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cnB92.png" alt="enter image description here"></a></p>