TCP客户端服务器程序以打印时间并退出

时间:2019-01-31 03:57:27

标签: c

它是TCP客户端-服务器程序的一部分。该程序将打印客户端给出的消息,并在循环中继续,当客户端输入时间时,它应打印当前时间并退出。我想知道如果条件不起作用,谁能帮助我解决这个问题

    for(;;)
    {
        printf("Please enter msg: ");
        bzero(buf, BUFSIZE);
        fgets(buf, BUFSIZE, stdin);
        if((strcmp(buf, "TIME") == 0) || (strcmp(buf, "Time") == 0) || (strcmp(buf, "time") == 0)  )
        {
            n = write(sockfd, buf, strlen(buf));
            bzero(buf, BUFSIZE);
            n = read(sockfd, buf, BUFSIZE);
            if (n < 0)
               error("ERROR reading from socket");
            printf("Echo from server: %s\n", buf);
            break;
        }
        /* send the message line to the server */
        n = write(sockfd, buf, strlen(buf));
        if (n < 0)
          error("ERROR writing to socket");

        /* print the server's reply */
        bzero(buf, BUFSIZE);
        n = read(sockfd, buf, BUFSIZE);
        if (n < 0)
          error("ERROR reading from socket");
        printf("Echo from server: %s", buf);

    }
    printf("\nCurrent local time: %s",asctime(timeinfo));
    close(sockfd);
    return 0;
}

0 个答案:

没有答案