Search code examples
csocketstcpscanfinterruption

How to keep a program running with a scanf


I want to find a way to exit from the "loop in question" (see below) with keeping the program running, I mean : In the program there must be the message: "preparing to send informations, exit to stop !" if the user doesn't type exit, the program must still running without waiting the user to click in a character and then enter. The informations must be sent continuously until the user type exit to quit the program.

I hope I explain well the problem, and I'm sure that there is an easy solution

Thank you in advance for your help

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

Solution

  • Simple thing you can do is to wait for a timeout. But with scanf you can't do that so you can go for a select() call for the event at stdin and proceed if no event within that time.

    Please check the following link http://cboard.cprogramming.com/c-programming/96544-unblock-scanf-call.html

    Hope this helps.