简体   繁体   中英

C Socket program - How can I accept user input while updating reading from stdout?

I have written a little irc bot application in C. It just writes out the server messages to the screen. This is just accomplished with a while loop that reads from the server filedescriptor. How can I concurrently accept input? I would like to now extend it to accept user input so that it can be used as a client application. I am new to C development so I am not sure how this is accomplished. Can someone provide me with an example or point me in the direction to some documentation?

I basically would like to mimic a telnet application. The stdout is updated and the user can provide server commands at the console.

Any help/advise would be greatly appreciated.

EDIT

I am developing in a Unix environment.

Thanks

Avoid multiprocess and multi-threaded programming if you can. That road leads to pain. Use event driven programming. For the type of thing you are wanting to do, event driven programming is much easier , and will perform just as well. The two main ways in C to do event driven programming (related to I/O) are select and poll .

Here is working example of using select:

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>

int
main(void)
{
    fd_set rfds;
    struct timeval tv;
    int retval, len;
    char buf[4096];

    while (1) {
        /* Watch stdin (fd 0) to see when it has input. */
        FD_ZERO(&rfds);
        FD_SET(0, &rfds);

        /* Wait up to five seconds. */
        tv.tv_sec = 5;
        tv.tv_usec = 0;

        retval = select(1, &rfds, NULL, NULL, &tv);
        /* Don't rely on the value of tv now! */

        if (retval == -1) {
            perror("select()");
            exit(EIO);
        } else if (retval) {
            printf("Data is available now.\n");
        } else {
            printf("No data within five seconds.\n");
            continue;
        }
        if (FD_ISSET(0, &rfds)) {
            len = read(0, buf, 4096);
            if (len > 0) {
                buf[len] = 0;
                printf("Got data on stdin: %s\n", buf);
            } else {
                // fd closed
                perror("read()");
                exit(EIO);
            }

        }
    }
}

FD_SET is used to create the list of file-descriptors you want to select on (get events from). After select returns successfully (meaning there is an event to process), you use FD_ISSET to find the file-descriptors that caused events. In your case you are going to have an open socket file-descriptor that you will add to the set and process appropriately.

Useful documentation include the following man pages:

  • man 2 select
  • man 2 poll
  • man 3 read
  • man 3 open

You want to monitor both socket, and stdin. If thats right, have a look at the select() system call here: http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html#select

There is a little thing called multi-threading. Multithreading (in C++) depends entirely on either the operating system, or an external library.

If you are using windows, then you could use beginthread() and endthread() defined in "Windows.h".

It is fairly easy to use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM