简体   繁体   English

为什么strncmp无法比较? 套接字编程C

[英]Why is strncmp not comparing? Socket programing C

I'm trying to parse the received data from a client in a server. 我正在尝试解析从服务器中的客户端接收到的数据。 The server have to send back a message according to what the client sent before. 服务器必须根据客户端之前发送的内容发送回一条消息。 But I can not make the strncmp function compare the strings. 但是我不能使strncmp函数比较字符串。 It always get to the else and my server close the conection. 它总是到达其他位置,而我的服务器关闭了连接。 Also my client stays connected and print in screen the option I typed. 我的客户也保持联系并在屏幕上打印我键入的选项。

Please need help to understand what is wrong! 请需要帮助以了解问题所在!

Thanks! 谢谢!

Incorrect Inputclose error: Bad file descriptor 不正确的Inputclose错误:错误的文件描述符

Program exited with code 01. 程序退出,代码为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);

    }
}

With a little logic 有一点逻辑

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

Is where the else lies. 是其他地方所在。

Ie

it gets run when after it is hits this: 当它被击中时它会运行:

 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 use toupper http://www.cplusplus.com/reference/cctype/toupper/ BTW使用toupper http://www.cplusplus.com/reference/cctype/toupper/

ie

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

Just add another ELSE! 只需添加另一个ELSE!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM