3

试图编写原始测试。程序必须启动 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 的数据,将其写入 master pty,但从属端没有收到它。我看过很多例子,但看不到错误。请建议我...

UPD我试过int len = read(STDIN_FILENO, buf, sizeof(buf));代替execvp它,它工作正常!我想我必须发送某种控制符号,例如输入...有什么想法吗?

4

2 回答 2

1

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

于 2011-07-13T04:54:46.800 回答
0

你的孩子做的第一件事就是打电话setsid。从POSIX 文档(强调我的):

如果调用进程不是进程组领导,则 setsid() 函数将创建一个新会话。返回时,调用进程将是这个新会话的会话负责人,应是新进程组的进程组负责人,并且不应有控制终端

所以你的孩子做的第一件事就是与它的控制终端分离。

于 2011-07-13T01:50:12.643 回答