How can I hardcode input with the "select" system call in C?

Posted by Archer on Stack Overflow See other posts from Stack Overflow or by Archer
Published on 2012-11-28T05:00:13Z Indexed on 2012/11/28 5:04 UTC
Read the original article Hit count: 215

Filed under:
|
|

If I understand this system call "select" correctly, it will loop waiting for user input from the keyboard or from an outside server. Every time I call "message_loop", I'm going to type in the same few lines of input each time. Is there a way to hard code this in so I don't have to type it in each time?

void message_loop(FILE* fpin, FILE* fpout, Socket sock)
{
        fd_set  readfds, readfds_bak ;
        int     in, max_fd, n, ret ;
        char    buf[MAXMESG];

        in = fileno(fpin) ;
        FD_ZERO(&readfds) ;
        FD_SET(in, &readfds) ;
        FD_SET(sock.socketfd, &readfds) ;

        readfds_bak = readfds ;

        max_fd = ((in > sock.socketfd) ? in : sock.socketfd) + 1 ;


        while(1){
                readfds = readfds_bak ;

/* select function */

                if((ret = select(max_fd, &readfds, NULL, NULL, NULL)) < 0){
                        perror("select") ;
                        break ;
                }
                else if (ret != 0) {
                        if(FD_ISSET(in, &readfds)){
/* keyboard input */
                                fgets(buf, MAXMESG, fpin) ;
                                if(send_message(buf, sock) == -1)
                                        break ;
                        }
                        if(FD_ISSET(sock.socketfd, &readfds)){
/* messages from server */
                                n = receive_message(buf, MAXMESG, &sock) ;
                                if(n == -1)
                                        break ;
                                else if(n > 0){
                                        fputs(buf, fpout) ;
                                        fputc('\n', fpout) ;
                                }
                                fflush(stdout) ;
                        }
                }
        }
}

© Stack Overflow or respective owner

Related posts about c

    Related posts about unix