简体   繁体   English

C套接字程序-更新标准输出的读数时如何接受用户输入?

[英]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. 我用C语言编写了一个irc bot应用程序。它只是将服务器消息写到屏幕上。 This is just accomplished with a while loop that reads from the server filedescriptor. 这只是通过从服务器filedescriptor读取的while循环来完成的。 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. 我是C开发的新手,所以我不确定这是如何实现的。 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. 我基本上想模仿一个telnet应用程序。 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. 我正在Unix环境中进行开发。

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 . C中进行事件驱动编程(与I / O相关)的两种主要方法是selectpoll

Here is working example of using select: 这是使用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). FD_SET用于创建您要选择的文件描述符列表(从中获取事件)。 After select returns successfully (meaning there is an event to process), you use FD_ISSET to find the file-descriptors that caused events. select成功返回(意味着有事件要处理)后,可以使用FD_ISSET查找导致事件的文件描述符。 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 如果那是正确的,请在此处查看select()系统调用: 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. 多线程(在C ++中)完全取决于操作系统或外部库。

If you are using windows, then you could use beginthread() and endthread() defined in "Windows.h". 如果使用的是Windows,则可以使用“ Windows.h”中定义的beginthread()和endthread()。

It is fairly easy to use. 它很容易使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何让这个C程序接受新输入而不将其添加到旧输入? - How can I get this C program to accept new input without adding it to the old input? 写入另一个程序的stdin /从c中的另一个程序的stdout读取 - writing to stdin of another program/reading from stdout of another program in c 如何修改 c 程序,以便在用户输入非浮点输入时,向用户显示“无效输入”? - How can I modify the c program such that if the user enters a non-float input,“invalid input” is shown to the user? C 从套接字读取后程序停止 - C program halts after reading from socket 从C中的套接字文件描述符读取时如何检测定界符? - How to detect a delimiter while reading from a socket file descriptor in C? 如何在 C 程序中从终端读取输入 - how can I read input from terminal in a C program C程序如何在Linux环境中同时执行其他操作的同时轮询用户输入? - How can a C program poll for user input while simultaneously performing other actions in a Linux environment? C标准是否指定从stdout读取输入的行为? - Does the C standard specify behavior for reading input from stdout? c++ 程序如何在调试时读取 VSCode 中的用户输入 - How c++ program read user input in VSCode while debugging 程序运行时,如何通过终端获得用户输入? - How can I get user input through terminal, while the program is running?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM