我有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;
}
答案 0 :(得分:1)
参数作为额外的char *
参数传递给execl
()。
将参数转换为char
字符串,然后将其作为附加参数传递给execl
()。
或使用exec
()的其他几个替代版本,如execvp
(),如果他们使用的替代参数传递约定对您更方便。