RECV 缓冲区为空,但 returns 值 > 1

RECV buffer empty, but returns a value > 1

我正在尝试制作一个简单的服务器,以便两个客户端可以相互通信。主服务器代码接受两个客户端连接,然后分叉出一个进程,该进程使用 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\n", conn, connAscii, atoi(connAscii));
        char *argf[2];
        argf[0] = connAscii; 
        argf[1] = connAscii2;
        fprintf(stderr, "that is to say %s and %s\n", argf[0], argf[1]);

        //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 privateServer
        execl("privateServer","privateServer", connAscii, connAscii2, (char *)0); 
}

execl 调用的个人服务器:

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 with %d\n", conn1);
    sent = send(conn1, greet,sizeof(greet),0);
    if(sent == -1){
        perror("send");
        exit(1);
    }

    sent = send(conn2, greet,sizeof(greet),0);
    if(sent == -1){
        perror("send");
        exit(1);
    }
    fprintf(stderr,"Hopefully they got it\n");
    return 0;
    }

客户端:逐个字符读取 recv buff 字符导致乱码,打印整个缓冲区没有显示任何内容,但 numbytes == 61。

char *buff = (char*)malloc(100);
memset(&buff[0],0,sizeof(buff));
numbytes = recv(sockfd, buff, 99, 0);  //should be from private server
if (numbytes == -1) {
        perror("recv");
        exit(1);
    }
buff[numbytes] = '[=13=]';
int i;
for(i = 0; i < numbytes; i++){
    fprintf(stderr, "%c", buff[i]);
}
printf("From match server: %.*s (%d bytes)\n", numbytes, buff, numbytes);   

有几个错误:

char* playerOne[20];
char* playerTwo[20];

您需要一个字符数组,而不是一个指向字符的指针数组

改为

char playerOne[20];
char playerTwo[20];

这里:

char *buff = (char*)malloc(100);
memset(&buff[0],0,sizeof(buff));

sizeof(buff)是指向char的指针的大小,改为

memset(&buff[0],0,100);

正如@user3629249 所指出的,不要使用像 100 这样的幻数,而是

#define MAX_BUFF 100
char *buff = malloc(MAX_BUFF);
memset(&buff[0],0,MAX_BUFF);

但是如果您要以 null 终止字符串,则不需要 memset