【问题标题】:How to keep a program running with a scanf如何使用 scanf 保持程序运行
【发布时间】:2014-08-26 08:37:27
【问题描述】:

我想找到一种方法退出“有问题的循环”(见下文)并保持程序运行,我的意思是:在程序中必须有消息:“准备发送信息,退出停止! "如果用户不键入 exit,程序必须仍在运行,而无需等待用户单击字符然后输入。信息必须不断发送,直到用户键入exit退出程序。

我希望我能很好地解释这个问题,并且我确信有一个简单的解决方案

提前感谢您的帮助

do
{

    /* menu */
    printf("\r\n");
    printf("1 : Start, Sending Informations.\r\n");
    printf("2 : Quit.\r\n");
    printf("choice : ");
    scanf("%d", &nChoice);

    printf("\n Trying to do a connection \n \r");

    /* if connection is not established, call accept, TCP SERVER */
    if (fd_client == -1)
    { 
        printf("ready to accept...\n");             
        fd_client = accept(fd_listen, (struct sockaddr*)&client_addr, &addrlen);

        if (fd_client >= 0) 
        {
            printf("accept: %s, port %d\r\n", inet_ntoa(client_addr.sin_addr), htons(client_addr.sin_port));
        }
    }

    if (fd_client < 0) 
    {
        printf(" connection is not established, continue to accept\n");

        /*connection is not established, continue to accept*/
        continue;               
    }

    FD_ZERO(&rfd);
    FD_ZERO(&wfd);
    FD_ZERO(&efd);
    FD_SET(fd_tty, &rfd);
    FD_SET(fd_client, &rfd);
    maxfd = (fd_tty > fd_client ? fd_tty : fd_client) + 1;

    /*connection is established, call select to see if any data arrived from the GPS*/
    printf("connection is established, seeing if any data arrived\n");

    char chaine1[256];
    char chaine2[] = "exit";
    int i;

    if (nChoice == 1)
    {  
        do // THE LOOP IN QUESTION
        { 
            printf("preparing to send informations, exit to stop !\n");
            i = strcmp(chaine1, chaine2); // Building the condition to exit
            scanf("%s", chaine1);
            fflush(stdin);
            // printf ("i= %d\n",i); 

            ret = select(maxfd, &rfd, &wfd, &efd, &tm);

            if (ret < 0) // Select failed 
            { 
                printf("select fail\r\n");
                break;
            } 
            else if (ret == 0) 
            {
                continue;   /*no data arrived, continue to call select*/
            }
            else 
                printf("data is arriving \n");

            if (FD_ISSET(fd_tty, &rfd)) /*tty port has data to be read*/
            {
                ret = read(fd_tty, buf, sizeof(buf));

                if (ret <= 0) 
                {
                    printf("read tty port fail\r\n");
                    break;
                }

                if (send(fd_client, buf, ret, 0) < 0) /*send data to ethernet*/
                {   
                    printf("send to ethernet fail\r\n");
                    break;              
                }
            }

            if (FD_ISSET(fd_client, &rfd)) /*tcp client has data to be read*/
            { 
                ret = recv(fd_client, buf, sizeof(buf), 0);

                if (ret < 0) 
                {
                    printf("read from ethernet fail\r\n");
                    break;
                } 
                else if (ret == 0) 
                {
                    printf("disconnect by remote\r\n");
                    close(fd_client);
                    fd_client = -1;
                    continue; /*continue to accept...*/
                }

                if (write(fd_tty, buf, ret) < 0) /*send data to tty port*/
                {       
                    printf("write to tty fail\r\n");
                    break;              
                }
            }

        } while (i != 0 ); // The exit condition
    }
} while(nChoice != 2);

【问题讨论】:

  • 请修正您不完整的标题。
  • 我尝试改进代码的格式,但放弃了。 PLEASE 把它变成正确的SSCCE。
  • 您应该先scanf("%s", chaine1);,然后再i = strcmp(chaine1, chaine2);。另一种方式,您第一次将有一个未初始化的 chaince1 缓冲区

标签: c sockets tcp scanf interruption


【解决方案1】:

您可以做的简单事情就是等待超时。但是对于scanf,您不能这样做,因此您可以通过stdin 拨打select() 呼叫该事件,如果在那段时间内没有事件,则继续。

请检查以下链接 http://cboard.cprogramming.com/c-programming/96544-unblock-scanf-call.html

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    我认为应该注意fflush(); 是为输出流设计的,并且在用于输入流时未定义,解决此问题的一种方法是scanf(" %s", chaine1); 或创建一个清除输入缓冲区的函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-06
      • 2014-06-10
      • 2023-03-13
      • 1970-01-01
      • 2017-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多