Search code examples
csocketssendrecv

How to handle 3 way send() and recv() in BSD socket using C


After sending "wrong" username - client won't start loop from beginning, actually, there is no server asks:?

Dunno how to handle 3 way client-server message sender for such auth. I must understand this to continue such message receiving in further.

client.c:

 int is_authenticated = 0;
 size_t sendline_s;

 while (!is_authenticated) {
    recv(sockfd, recvline, MAXLINE, 0);
    printf("%s", "server asks:");
    fputs(recvline, stdout);
    printf("?> ");
    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");
    }
 }

server.c

  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);
    }
  }

But, If i send right username - it passes the block and everything goes well.

enter image description here


Solution

  • server:

      while (!is_authenticated) { 
    
                        if ((n = recv(connfd, buf, MAXLINE-1,0))== -1) {
                                perror("recv");
                                exit(1);
                        }
                        else if (n == 0) {
                                printf("Connection closed\n");
                                //So I can now wait for another client
                                break;
                        }
                        printf("\n%d truely recieved", n);
                        buf[n] = '\0';
                        printf("Server:Msg Received %s\n", buf);
                        if (strcmp(buf, "DONE") == 0) {
                            strcpy(buf, "DONE");
                            if ((send(connfd,buf, strlen(buf),0))== -1)
                            {
                                fprintf(stderr, "Failure Sending Message\n");
                                close(connfd);
                                break;
                            }
    
                            puts("KONEC");
                            is_authenticated = 1;
                        }
                        else {
                            if ((send(connfd,buf, strlen(buf),0))== -1)
                            {
                                fprintf(stderr, "Failure Sending Message\n");
                                close(connfd);
                                break;
                            }
    
                        }
    
                        printf("Server:Msg being sent: %s\nNumber of bytes sent: %d\n", buf, strlen(buf));
    }
    

    client:

     while (!is_authenticated) {
                printf("Client: Enter Data for Server:\n");
                if (fgets(sendline, MAXLINE-1, stdin) != NULL) {
                    if (sendline[(strlen(sendline)-1)] == '\n') {
    
                        sendline[strlen(sendline)-1] = '\0';
    
                        if ((send(sockfd,sendline, strlen(sendline),0))== -1) {
                                fprintf(stderr, "Failure Sending Message\n");
                                close(sockfd);
                                exit(1);
                        }
                        else {
                                printf("Client:Message being sent: %s\n",sendline);
                                n = recv(sockfd, sendline, sizeof(sendline),0);
                                if ( n <= 0 )
                                {
                                        printf("Either Connection Closed or Error\n");
                                        //Break from the While
                                        break;
                                }
    
                                sendline[n] = '\0';
                                if (strcmp(sendline, "DONE") == 0) {
                                    puts("AUTH PASSED");
                                    is_authenticated = 1;
                                }
                                printf("Client:Message Received From Server -  %s\n",sendline);
                        }
                    }
                    //EOF
                }
     }