C: 通过 execl 传递套接字连接
C: Pass socket connection via execl
我正在尝试创建一个支持多个客户端的基本服务器。我希望主服务器连接到两个客户端,然后通过调用 execl 将客户端的文件描述符分叉并传递给另一个程序。两个客户端都连接到服务器并能够与其通信(通过向客户端询问用户名或句柄进行测试),但是当我尝试将客户端传递给第二个程序时(这意味着两者之间的个人服务器)客户端举办棋盘游戏比赛)当第二个程序试图联系他们时,客户端没有显示输出,我肯定这是我试图传递连接信息的方式。对正确传递连接的任何帮助表示赞赏。
这是主服务器接受两个连接后的代码:
主服务器:注意。根据建议添加了额外的错误检查。
if(fork() == 0){
close(listener);
int nBytes;
char* playerOne[20];
char* playerTwo[20];
//Creates strings to hold file descriptor information for execl
char connAscii[sizeof(int)];
char connAscii2[sizeof(int)];
snprintf(connAscii,sizeof(conn), "%d", conn);
snprintf(connAscii2,sizeof(conn2), "%d", conn2);
fprintf(stderr, "Int conn: %d, asciiConn: %s, backToInt: %d", conn, connAscii, atoi(connAscii));
char *argf[2];
argf[0] = connAscii;
argf[1] = connAscii2;
//Send Handle Request to Connection 1
nBytes = send(conn, handleRequest, sizeof(handleRequest),0);
if(nBytes == -1){
perror("send");
exit(1);
}
//Receive Handle from Connection 1
nBytes = recv(conn, playerOne, 20, 0);
if(nBytes == -1){
perror("recv");
exit(1);
}
//Send Handle Request to Connection 2
nBytes = send(conn2, handleRequest, sizeof(handleRequest),0);
if(nBytes == -1){
perror("send");
exit(1);
}
//Receive Handle from Connection 2
nBytes = recv(conn2, playerTwo, 20, 0);
if(nBytes == -1){
perror("recv");
exit(1);
}
//Send Handle for Connection 2 to Connection 1
nBytes = send(conn, playerTwo, sizeof(playerTwo),0);
if(nBytes == -1){
perror("send");
exit(1);
}
//Send Handle for Connection 1 to Connection 2
nBytes = send(conn2, playerOne, sizeof(playerOne),0);
if(nBytes == -1){
perror("send");
exit(1);
}
//Passes file descriptors to nimMatch
execl("nimMatch", "nimMatch", argf, (char *)0);
}
个人服务器(与主服务器不同的 .c 文件):注意。添加调试语句
char greet[] = {"Hello players, please wait for match setup."};
int main(int argc, char *argv[]) {
int conn1 = atoi(argv[1]);
int conn2 = atoi(argv[2]);
int sent;
fprintf(stderr, "Attempting connection\n");
sent = send(conn1, greet,sizeof(greet),0);
if(sent == -1){
perror("send");
exit(1);
}
return 0;
}
以及连接匹配对手后的客户端代码:
printf("Your opponent is: %s\n\n Connecting to match server...", otherHandle);
memset(&buf[0],0,sizeof(buf));
if ((numbytes = recv(sockfd, buf, 99, 0)) == -1) { //should come from personal server
perror("recv");
exit(1);
}
printf("From match server: %s\n", buf);
如果需要更多代码请告诉我,我知道客户端已成功连接到服务器并且在编译时我没有收到任何错误或 运行。
来自 EJP 的建议答案确保私人服务器接收到正确的输入,但客户端似乎没有存储来自私人服务器的字符串。下面调整后的客户端代码表明客户端正在接收 64 字节但 buf 为空。
printf("You're opponent is: %s\n\n Connecting to match server...", otherHandle);
memset(&buf[0],0,sizeof(buf));
numbytes = recv(sockfd, buf, 99, 0);
if (numbytes == -1) {
perror("recv");
exit(1);
}
if(strlen(buf) < 1)
fprintf(stderr, "No buffer input, found %d bytes\n", numbytes);
printf("From match server: %s\n", buf);
您将参数传递给 execl()
不正确。它不采用参数数组,它采用可变参数列表。应该是
execl(path, connAscii, connAscii2, (char*)0);
我正在尝试创建一个支持多个客户端的基本服务器。我希望主服务器连接到两个客户端,然后通过调用 execl 将客户端的文件描述符分叉并传递给另一个程序。两个客户端都连接到服务器并能够与其通信(通过向客户端询问用户名或句柄进行测试),但是当我尝试将客户端传递给第二个程序时(这意味着两者之间的个人服务器)客户端举办棋盘游戏比赛)当第二个程序试图联系他们时,客户端没有显示输出,我肯定这是我试图传递连接信息的方式。对正确传递连接的任何帮助表示赞赏。
这是主服务器接受两个连接后的代码:
主服务器:注意。根据建议添加了额外的错误检查。
if(fork() == 0){
close(listener);
int nBytes;
char* playerOne[20];
char* playerTwo[20];
//Creates strings to hold file descriptor information for execl
char connAscii[sizeof(int)];
char connAscii2[sizeof(int)];
snprintf(connAscii,sizeof(conn), "%d", conn);
snprintf(connAscii2,sizeof(conn2), "%d", conn2);
fprintf(stderr, "Int conn: %d, asciiConn: %s, backToInt: %d", conn, connAscii, atoi(connAscii));
char *argf[2];
argf[0] = connAscii;
argf[1] = connAscii2;
//Send Handle Request to Connection 1
nBytes = send(conn, handleRequest, sizeof(handleRequest),0);
if(nBytes == -1){
perror("send");
exit(1);
}
//Receive Handle from Connection 1
nBytes = recv(conn, playerOne, 20, 0);
if(nBytes == -1){
perror("recv");
exit(1);
}
//Send Handle Request to Connection 2
nBytes = send(conn2, handleRequest, sizeof(handleRequest),0);
if(nBytes == -1){
perror("send");
exit(1);
}
//Receive Handle from Connection 2
nBytes = recv(conn2, playerTwo, 20, 0);
if(nBytes == -1){
perror("recv");
exit(1);
}
//Send Handle for Connection 2 to Connection 1
nBytes = send(conn, playerTwo, sizeof(playerTwo),0);
if(nBytes == -1){
perror("send");
exit(1);
}
//Send Handle for Connection 1 to Connection 2
nBytes = send(conn2, playerOne, sizeof(playerOne),0);
if(nBytes == -1){
perror("send");
exit(1);
}
//Passes file descriptors to nimMatch
execl("nimMatch", "nimMatch", argf, (char *)0);
}
个人服务器(与主服务器不同的 .c 文件):注意。添加调试语句
char greet[] = {"Hello players, please wait for match setup."};
int main(int argc, char *argv[]) {
int conn1 = atoi(argv[1]);
int conn2 = atoi(argv[2]);
int sent;
fprintf(stderr, "Attempting connection\n");
sent = send(conn1, greet,sizeof(greet),0);
if(sent == -1){
perror("send");
exit(1);
}
return 0;
}
以及连接匹配对手后的客户端代码:
printf("Your opponent is: %s\n\n Connecting to match server...", otherHandle);
memset(&buf[0],0,sizeof(buf));
if ((numbytes = recv(sockfd, buf, 99, 0)) == -1) { //should come from personal server
perror("recv");
exit(1);
}
printf("From match server: %s\n", buf);
如果需要更多代码请告诉我,我知道客户端已成功连接到服务器并且在编译时我没有收到任何错误或 运行。
来自 EJP 的建议答案确保私人服务器接收到正确的输入,但客户端似乎没有存储来自私人服务器的字符串。下面调整后的客户端代码表明客户端正在接收 64 字节但 buf 为空。
printf("You're opponent is: %s\n\n Connecting to match server...", otherHandle);
memset(&buf[0],0,sizeof(buf));
numbytes = recv(sockfd, buf, 99, 0);
if (numbytes == -1) {
perror("recv");
exit(1);
}
if(strlen(buf) < 1)
fprintf(stderr, "No buffer input, found %d bytes\n", numbytes);
printf("From match server: %s\n", buf);
您将参数传递给 execl()
不正确。它不采用参数数组,它采用可变参数列表。应该是
execl(path, connAscii, connAscii2, (char*)0);