【发布时间】:2015-03-31 01:23:50
【问题描述】:
我有 2 个程序(可执行文件)客户端和服务器。客户端从用户那里捕获 2 个整数,并将它们传输到服务器。服务器进行计算并返回给客户端。
但我不知道如何将参数从客户端传递到服务器。
#include <cstdlib>
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
using namespace std;
int main(int argc, const char* argv[]){
argv[0] = "server"; // the Server program, located in the same directory
int int1, int2;
char operator1;
//data entry
cout << "Please enter 2 integers" << endl;
cin >> int1 >> int2;
cout << "Please enter a character, either a + or a -" << endl;
cin >> operator1;
int PID = fork(); // creating a child process
while (operator1 == '+' || operator1 =='-') {
if (PID == 0){
execl(arg[0], "server", NULL); // child process (server code).
//How to pass the items to this program?
_exit(EXIT_FAILURE);
}
else if (PID < 0){
perror("execution failed\n");
exit(EXIT_FAILURE);
}
// re-enter data
cout << "Please enter 2 integers" << endl;
cin >> int1 >> int2;
cout << "Please enter a character, either a + or a -" << endl;
cin >> operator1;
}
return 0;
}
【问题讨论】:
-
这可能对您的问题有所帮助stackoverflow.com/questions/24810889/…
标签: c++ linux system-calls