c - 为什么 strncmp 不进行比较?套接字编程C

标签 c strncmp

我正在尝试解析服务器中从客户端接收到的数据。服务器必须根据客户端之前发送的消息发回消息。但我无法使 strncmp 函数比较字符串。它总是到达 else 并且我的服务器关闭连接。我的客户也保持连接并在屏幕上打印我输入的选项。

请需要帮助来了解问题所在!

谢谢!

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

    }
}

最佳答案

有一点逻辑

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

顺便说一句,使用toupper http://www.cplusplus.com/reference/cctype/toupper/

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

只需添加另一个 ELSE!

关于c - 为什么 strncmp 不进行比较?套接字编程C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15591767/

相关文章:

将指针复制到结构在c中不起作用

c++ - 有没有接近C/C++又容易自动并行化的语言?

c++ - strncmp 没有正确匹配

c - strncmp 的这种用法是否包含越界读取?

c - 是否有 strncmp() 的惯用用法?

c - 为什么即使使用了 volatile 关键字,编译器也会因 strncmp() 而优化掉共享内存读取?

c - 检测 Azure Linux VM 中的临时磁盘

c - ATmega328p,定时器0中断

c - n 太大的 strncmp 会产生奇怪的输出

c - 如何修复我的代码?