2013-03-23 35 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

相关问题