尝试编写原始测试。程序必须启动tcp-server,接收连接并将接收到的数据重定向到派生程序。这是代码:

#include "TcpServer.h"

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <termios.h>
#include <stropts.h>
#include <sys/ioctl.h>

//test
#include <pty.h>

int main() {

    if (setsid() < 1) {
        perror("cannot setsid()");
        return 1;
    }

    TcpServer tcp_server;
    int client;
    char buf[1024];

    int master, slave;
    char * slave_name;


    bzero(buf, sizeof(buf));

    tcp_server.setPort(1025);
    int server = tcp_server.startup();
    client = accept(server, NULL, NULL);

    fprintf(stderr, "forking...\n");
    pid_t pid = forkpty(&master, NULL, NULL, NULL);


    if (pid == 0) {
        setsid();


        fprintf(stderr, "slave pty is opened\n");

        fprintf(stdout, "hello, client!\n");

        char * cmd[] = {"upper.py", NULL};

        fprintf(stderr, "slave: start to login\n");
        int res = execvp("upper.py", cmd);
        if (res < 0) {
            fprintf(stderr, "trouble while running exec - '%s'\n", strerror(errno));
            return 1;
        }
        fprintf(stderr, "slave: quit\n");
        return 0;
    } else if (pid < 0) {
        perror("cannot fork off process");
        return 1;
    }

    fd_set in;
    int max_fd, n;

    struct termios ot, t;
    tcgetattr(master, &ot);
    t = ot;
    t.c_lflag &= ~(ICANON | ISIG | ECHO | ECHOCTL | ECHOE | ECHOK | ECHOKE | ECHONL | ECHOPRT );
    t.c_iflag |= IGNBRK;
    t.c_cc[VMIN] = 1;
    t.c_cc[VTIME] = 0;
    tcsetattr(master, TCSANOW, &t);

    while (1) {
        FD_ZERO(&in);

        max_fd = (master > max_fd) ? master : max_fd;
        FD_SET(master, &in);

        max_fd = (client > max_fd) ? client : max_fd;
        FD_SET(client, &in);

        fprintf(stderr, "select starts\n");
        if (select(max_fd + 1, &in, NULL, NULL, NULL) < 0) {
            fprintf(stderr, "select returned incorrectly\n");
            if (errno == EINTR)
                continue;

            perror("error while select");
            return 1;
        }

        if (FD_ISSET(client, &in)) {
            fprintf(stderr, "client is set\n");
            n = recv(client, buf, sizeof(buf), 0);

            if (n < 0) {
                fprintf(stderr, "error while receiving data from client '%s'\n", strerror(errno));
                return 1;
            }

            if (n <= sizeof(buf)) {
                buf[n] = 0;
            }

            fprintf(stderr, "received from client: '%s'\n", buf);

            n = write(master, buf, n);
            tcflush(master, TCIOFLUSH);
        }

        if (FD_ISSET(master, &in)) {
            fprintf(stderr, "master is set ");

            n = read(master, buf, sizeof(buf));

            if (n < 0) {
                fprintf(stderr, ": %s - ", strerror(errno));
                return 1;
            }
            if (n <= sizeof(buf))
                buf[n] = 0;
            fprintf(stderr, "sending data - '%s'\n", buf);
            n = send(client, buf, n, 0);
        }

    }

    return 0;
}

程序的输出:
[milo@milo telnetd]$ sudo ./server
forking...
select starts
master is set sending data - 'slave pty is opened
hello, client!
slave: start to login
'
select starts
master is set sending data - 'input string: '
select starts
client is set
received from client: 'hello'
select starts

简而言之,麻烦在于:服务器成功地从tcp-client接收了数据,并将其写入主pty,但是从属端未接收到它。我看过很多示例,但是看不到错误。请建议我...

UPD 我尝试使用int len = read(STDIN_FILENO, buf, sizeof(buf));而不是execvp,它工作正常!我想我必须发送某种控制符号,例如回车...有什么想法吗?

最佳答案

雅虎!我已经解决了这个问题!错误是(如我之前在 UPD 中所说),输入中没有\n符号! otherside程序等待输入,直到出现终端符号!

10-06 01:42