プリミティブテストを書き込もうとしています。プログラムは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に書き込みますが、スレーブエンドはそれを受信しません。多くの例を見てきましたが、エラーは見られません。私に提案してください...
UPDint len = read(STDIN_FILENO, buf, sizeof(buf));
の代わりに試しましたがexecvp
、正常に動作します。エンターのようなコントロールシンボルを送る必要があると思います...何か考えはありますか?