2013-03-23 42 views
0

我試圖解析從服務器中的客戶端接收到的數據。服務器必須根據客戶以前發送的內容發回消息。但我不能讓strncmp函數比較字符串。它總是到達其他地方,我的服務器關閉連接。我的客戶也保持連接並在屏幕上打印我輸入的選項。爲什麼strncmp沒有比較?套接字編程C

請幫助理解錯在哪裏!

謝謝!

Incorrect Inputclose error: Bad file descriptor

Program exited with code 01.

void 
result(int sockfd) 
{ 
    ssize_t  n; 
    char  buf[MAXLINE]; 
    int   temp; 
    time_t  ticks; 
    int   i; 
again: 
    while ((n =read(sockfd, buf, 15)> 0)) 
    { 
    buf[n] = '\0'; 
    printf("Message Recieved:%s\n",buf); 
    srand (time(NULL)); 
    temp = rand() % 15+1; 
    printf("Ramdom es %i\n",temp); 

    if ((strncmp (buf,"A",1) == 0) || (strncmp (buf,"a",1) == 0)) 
    { 
     snprintf(buf, sizeof(buf), "You chose option A -%i times on %.24s\r\n", temp,ctime(&ticks)); 
     Writen(sockfd, buf, n); 
    } 
    if ((strncmp (buf,"B",1) == 0) || (strncmp (buf,"b",1) == 0)) 
    { 
     snprintf(buf, sizeof(buf), "You chose option B -%i times on on %.24s\r\n", temp,ctime(&ticks)); 
     Writen(sockfd, buf, n); 
    } 
    else 
    { 
     printf("Incorrect Input"); 
     Close(sockfd); 
     break; 
    } 
    } 
    if (n < 0 && errno == EINTR) 
    goto again; 
    else if (n < 0) 
     err_sys("read error"); 
} 

int 
main(int argc, char **argv) 
{ 
    int     listenfd, connfd; 
    socklen_t   len; 
    struct sockaddr_in servaddr, cliaddr; 
    char    buff[MAXLINE]; 
    /*char    message[MAXLINE];*/ 
    char    recvline[MAXLINE + 1]; 

    listenfd = Socket(AF_INET, SOCK_STREAM, 0); 
    bzero(&servaddr, sizeof(servaddr)); 
    servaddr.sin_family  = AF_INET; 
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);/*----------------------------------------------------*/ 
    servaddr.sin_port  = htons(5678); 

    Bind(listenfd, (SA *) &servaddr, sizeof(servaddr)); 
    Listen(listenfd, LISTENQ); 
    printf("EDMTS is running on 129.128.4.80, listening on port 5678\n"); 
    printf("\n"); 
    printf("Waiting for incoming connections...Press Ctrl+C to end server\n"); 

    for (; ;) 
    { 
     len = sizeof(cliaddr); 
     connfd = Accept(listenfd, (SA *) &cliaddr, &len); 

     /*Client connects to server*/ 
     printf("\n"); 
     printf("Connection from %s, port %d\n", 
       Inet_ntop(AF_INET, &cliaddr.sin_addr, buff, sizeof(buff)), 
       ntohs(cliaddr.sin_port)); 

      result(connfd); 
       Close(connfd); 

    } 
} 
+0

至少有一個問題是您的程序關閉了兩次套接字。 – 2013-03-23 20:37:31

+1

套接字數據可能不是空終止。 – 2013-03-23 20:38:10

+0

當'printf'語句顯示收到的消息時,它說了什麼? – 2013-03-23 20:38:59

回答

1

隨着一點點的邏輯

if ((strncmp (buf,"B",1) == 0) || (strncmp (buf,"b",1) == 0)) 
{ 
    snprintf(buf, sizeof(buf), "You chose option B -%i times on on %.24s\r\n", temp,ctime(&ticks)); 
    Writen(sockfd, buf, n); 
} 
else 
{ 
    printf("Incorrect Input"); 
    Close(sockfd); 
    break; 
} 

是在別的地方所在。

I.e.

它被運行時後,是觀看次數:

if ((strncmp (buf,"A",1) == 0) || (strncmp (buf,"a",1) == 0)) 
{ 
    snprintf(buf, sizeof(buf), "You chose option A -%i times on %.24s\r\n", temp,ctime(&ticks)); 
    Writen(sockfd, buf, n); 
} 

BTW使用toupperhttp://www.cplusplus.com/reference/cctype/toupper/

if ('B' == toupper(buf[0]) ... 

只需添加另一回事!

+0

@Eric Urban ......它永遠不會去snprintf。 – netfreak 2013-03-23 21:12:59

+0

@crony ...(gdb)p buf $ 1 =「c」,'\ 0'<重複2027次>,「\ 203 \037 」,'\ 0'<重複12次>,「H \ 215H \ 215D \ 2150#\ 204 \ 000 \ 000 \ 000 \ 000 \ 215 \ 207 \ 215#\ 204 「,'\ 0'<重複20次>,」 \ 000 \ 000 \ 000 \ 000 \ 000 \ 000 \ 000 \000 \237 「,」\「 0'<重複16次>,「H \215 」,「\ 0」<重複20次>,「\ 001 \ 000 \ 000 \ 000 \ 000 \ 000 \ 000 \ 000 \204 \215 」 ,'\ 0'<重複36次>,「 \ \ \ 000 \ 000 \ 000#\204 \ 030 \204 \ 000 \ 000 \ 000 \000H \215 \ 003」 ,'\ 0'<重複19次>,「\ n \ 000 \ 000 \000G \215 」,「\ 0」<重複40次>,「」,'\ 0'<重複11次>。 。 (gdb)p temp $ 2 = 14 – netfreak 2013-03-23 21:13:56

+0

@ netfreak-呃?爲什麼在亂碼中,但後來我假設這個MB是英文 – 2013-03-23 21:19:58