linux 重定向服务器的STDOUT,使其对C++中的TCP连接的客户端可见

wfypjpf4  于 2023-08-03  发布在  Linux
关注(0)|答案(1)|浏览(83)

TCPclient-server实现中,我设置客户端发送一个shell命令,由服务器运行,然后服务器将操作系统的响应发送回客户端。

using namespace std;

string run_shell()
{
    // return buffer
    char buffer[1024];
    memset(buffer, 0, 1024);

    //fork child
    int pid = fork();

    //Child
    if(!pid)
    {
        char* args[] = {"ls", "-l", NULL};
        int status = execvp("ls", args);
        if(status < 0)
        {
            cout << "Error: executing shell command failed!" << endl;
            return "";
        }

        int out_pipe[2];

        // save stdout
        int saved_stdout = dup(STDOUT_FILENO);

        pipe(out_pipe);

        // redirect stdout to the pipe
        dup2(out_pipe[1], STDOUT_FILENO);
        close(out_pipe[1]);

        // read from pipe into buffer
        read(out_pipe[0], buffer, 1024);
    }

    //Parent
    else
    {
    }

    return buffer;
}

字符串
产出:
在服务器终端:

Server received a message
ls -l
-rw-r--r-- 1 XXXX XXXX  4865 Jul 15 12:27 Client.cpp
-rw-r--r-- 1 XXXX XXXX   281 Jul 15 16:40 client_main.cpp
-rw-r--r-- 1 XXXX XXXX 12008 Jul 15 17:06 client_main.o
Server sent a message


在客户端:

Client sent a message
ServerMessage: -> `�7��


我想得到服务器执行的命令的STDOUT作为string提供给客户端。从我所做的,我总是得到垃圾在客户端。
有人能帮我解决这个问题吗?父分支中应该有一些重定向的部分,但我不能正确编码。

  • 这些链接已被访问:12
anhgbhbe

anhgbhbe1#

解决方法:

string run_shell()
{
    // create pipe lines between parent and child processes
    int pipe_lines[2];

    // create the return buffer
    vector<char> return_buffer;

    // define the pipes
    if(pipe(pipe_lines) == -1)
    {
        cout << "Error: creating pipe_line in server failed!" << endl;
        return "";
    }

    //fork child
    int pid = fork();

    //Child
    if(!pid)
    {
        // command
        char* args[] = {"ls", "-l", NULL};

        // copy STDOUT and STDERR of child process to be copied into write pipe
        dup2 (pipe_lines[1], STDOUT_FILENO);
        dup2 (pipe_lines[1], STDERR_FILENO);

        // chile does not read
        close(pipe_lines[0]);

        // child does not write
        close(pipe_lines[1]);

        int status = execvp("ls", args);
        if(!status)
        {
            cout << "Error: executing shell command failed!" << endl;
            return "";
        }

        // send the 'exec' output into the pipe
        fprintf(stderr, "%s\n", execvp);

        // exit child process
        exit(EXIT_FAILURE);
    }

    //Parent
    else
    {
        // parent does not write
        close(pipe_lines[1]);

        // how many bytes have been read
        int read_bytes;

        // a temp char buffer
        char tmp_ch = 0;

        // read until there is no more character left in read pipe
        while((read_bytes = read(pipe_lines[0], &tmp_ch, 1)) == 1)
        {
            return_buffer.push_back(tmp_ch);
            tmp_ch = 0;
        }

        // block the parent process until any of its children has finished
        wait(NULL);
    }

    return string(return_buffer.data());
}

字符串

相关问题