gpt4 book ai didi

C: 通过 execl 传递套接字连接

转载 作者:太空宇宙 更新时间:2023-11-04 08:28:51 25 4
gpt4 key购买 nike

我正在尝试创建一个支持多个客户端的基本服务器。我希望主服务器连接到两个客户端,然后通过调用 execl 将客户端的文件描述符 fork 并传递给另一个程序。两个客户端都连接到服务器并能够与其通信(通过向客户端询问用户名或句柄进行测试),但是当我尝试将客户端传递给第二个程序时(这意味着两者之间的个人服务器)客户端举办棋盘游戏比赛)当第二个程序试图联系他们时,客户端没有显示输出,我肯定这是我试图传递连接信息的方式。对正确传递连接的任何帮助表示赞赏。

这是主服务器接受两个连接后的代码:

主服务器:注意。根据建议添加了额外的错误检查。

    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 的建议答案确保私有(private)服务器接收到正确的输入,但客户端似乎没有存储来自私有(private)服务器的字符串。下面调整后的客户端代码声明客户端正在接收 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);

关于C: 通过 execl 传递套接字连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29292358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com