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

时间:2015-03-27 02:34:00

标签: c sockets network-programming client server

我正在尝试创建一个支持多个客户端的基本服务器。我希望主服务器连接到两个客户端,然后分叉并通过调用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文件):注意。添加了debuging语句

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); 

1 个答案:

答案 0 :(得分:2)

您错误地将参数传递给execl()。它不需要参数数组,它需要一个varargs参数列表。它应该是

execl(path, connAscii, connAscii2, (char*)0);