【发布时间】:2015-01-02 00:27:30
【问题描述】:
我需要创建一个客户端-服务器应用程序(使用套接字),基本思想是:
- 客户端发送字符串
- 服务器接收字符串并将其作为另一个应用程序的标准输入发送
- 服务器读取应用程序的标准输出
- 服务器向客户端发送应答。
“其他应用程序”是一个封闭源代码计算(从标准输入读取4 5 + 并打印9 的计算器)。
我正在尝试在服务器上创建一个双管道,fork,并使用此管道重定向计算的标准输入和标准输出:
if(!fork())
{
close(STDOUT_FILENO);
close(STDIN_FILENO);
dup2(outfd[0], STDIN_FILENO);
dup2(infd[1], STDOUT_FILENO);
close(outfd[0]); /* Not required for the child */
close(outfd[1]);
close(infd[0]);
close(infd[1]);
system("./calc/calc ");
exit(0);
}
从服务器写入一个管道并读取另一个管道
close(outfd[0]); /* These are being used by the child */
close(infd[1]);
char c;
do
{
c=getchar();
write(outfd[1],&c,1);
}while(c!='\n');
input[read(infd[0],input,100)] = 0; /* Read from child’s stdout */
printf("%s",input);
close(outfd[1]);
close(infd[0]);
我知道这很不完整,但我认为它至少应该打印 calc 中第一行的输出。
(完整代码)
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
int outfd[2];
int infd[2];
pipe(outfd); /* Where the parent is going to write to */
pipe(infd); /* From where parent is going to read */
if(!fork())
{
close(STDOUT_FILENO);
close(STDIN_FILENO);
dup2(outfd[0], STDIN_FILENO);
dup2(infd[1], STDOUT_FILENO);
close(outfd[0]); /* Not required for the child */
close(outfd[1]);
close(infd[0]);
close(infd[1]);
//system("/usr/bin/bc -q");
system("./calc/calc ");
exit(0);
}
else
{
char input[100];
close(outfd[0]); /* These are being used by the child */
close(infd[1]);
//write(outfd[1],"2^32\n",5); /* Write to child’s stdin */
//write(outfd[1],"2 4 +\n",6);
char c;
do
{
c=getchar();
write(outfd[1],&c,1);
}while(c!='\n');
input[read(infd[0],input,100)] = 0; /* Read from child’s stdout */
printf("%s",input);
close(outfd[1]);
close(infd[0]);
}
return 0;
这是非常基本的,但它只是用于测试。当我执行它时,它什么也不做。
如果我在执行过程中ps -e | grep calc,我可以看到它正在运行,但无论我输入什么似乎都没有做任何事情。
【问题讨论】:
-
孩子可能正在缓冲它的输出。它会在退出时刷新输出,但在执行此操作之前它可能会在
stdin上等待EOF。 -
它是你的
calc二进制文件吗?我可以针对常见的calc安装编译和运行您的代码。