2015-11-22 45 views
-1

發出「錯誤」的用戶名後,辦理在BSD插座3路的send()和recv() - 客戶不會從開始啓動循環,實際上,沒有server asks:?如何用C

說不上如何處理3路客戶端 - 服務器消息發件人進行此類身份驗證。我必須理解這一點才能繼續收到這樣的消息。

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

但是,如果我發送正確的用戶名 - 它傳遞塊,萬事如意。

enter image description here

+1

目前尚不清楚你的客戶端的當前行爲是什麼。至少應該用不正確的用戶名顯示測試會話,包括輸入內容和輸出內容。 – kaylum

+0

這裏有問題嗎?你期望發生什麼事情沒有發生? –

回答

0

服務器:

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

客戶端:

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