-2

这是一个多客户端套接字程序。

当我尝试运行我的程序时,最后几个 recvfrom() 不返回任何数据。但是之前的 recvfrom 有多余的数据。有人可以帮我解决这个问题。

客户:

while (1) {
memset(buff, 0, sizeof(buff));  
recvfrom(sockfd,buff,sizeof(buff),0,(struct sockaddr *)&servaddr,&len);
printf("%s\n", buff);
if (buff[0] == 'U') {
    while(1) {
        printf("Insert your username: ");
        fgets(username,sizeof(username),stdin);
        username[strlen(username) - 1] = '\0';
        printf("Username chosen is %s\n", username);
        // Username Check (Error Check)
        if (strlen(username) < 1 || strlen(username) > 16) {
            printf("Minimum of 1 and maximum of 16.\n");
            continue;
        }
        for (i = 0; i < strlen(username); i++) {
            if (isalnum(username[i]) == 0) {
                printf("Username must contain only alphanumeric characters.\n");
                j = 0;
                break;
            }
        }
        if (j = 0) {
            continue;   
        } else {
            break;
        }       
        }
        sendto(sockfd,username,strlen(username),0,(const struct sockaddr *)&servaddr,len);

        memset(username, 0, sizeof(username));  

    } else {
        break;
    }
}               

printf("Players:\n");

memset(buff, 0, sizeof(buff));  

for (i = 0; i < numplayer; i++) {
    printf("BUFF: %s\n", buff);
    recvfrom(sockfd,buff,sizeof(buff),0,(struct sockaddr *)&servaddr,&len);
    printf("%s\n", buff);

    memset(buff, 0, sizeof(buff));  


}       

服务器:

      for (i = 0; i < numplayer; i++) {
    while (1) {
        memset(mesg,0,sizeof(mesg));
        if (recvfrom(connfd[i],mesg,sizeof(mesg),0,(struct sockaddr *)&cliaddr,&len) < 0) {
            perror("Recvfrom");
            exit(-1);
        }
        for (j = 0; j < numplayer; j++) {
            if (strcmp(username[j], mesg) == 0) {
                // Reinitialize buff
                memset(buff, 0, sizeof(buff));  
                check = 1;          
                break;
            }
        }

        if (check == 1) {
            check = 0;
            sprintf(buff, "Username already exist!");
            if ( sendto(connfd[i],buff,strlen(buff),0,(const struct sockaddr *)&cliaddr,len) < 0 ) {
                perror("Sendto");
                exit(-1);
            }
            memset(buff, 0, sizeof(buff));  

            continue;
        } else {
            sprintf(buff, "Valid Username!");
            if ( sendto(connfd[i],buff,strlen(buff),0,(const struct sockaddr *)&cliaddr,len) < 0 ) {
                perror("Sendto");
                exit(-1);
            }
            strcpy(username[i], mesg);
            printf("Username of Player %d is %s.\n" ,i + 1,username[i]);
            break;  
        }
    }
}
    printf("Players:");
for (i = 0; i < numplayer; i++) {
    memset(buff, 0, sizeof(buff));  

    sprintf(buff, "> %s", username[i]);
    printf("%s\n", buff);   

    for (j = 0; j < numplayer; j++) {
        if ( sendto(connfd[j],buff,strlen(buff),0,(const struct sockaddr *)&cliaddr,len) < 0 ) {
            perror("Sendto");
            exit(-1);
        }
    }
}
4

2 回答 2

1

没有“多余的字节”。recvfrom() 返回一个长度。你忽略了它。它也可以是 EOS (0) 或错误指示(-1,参见“errno”)。您需要检查所有这些可能性。

于 2013-09-23T04:02:44.130 回答
0

TCP 连接可能会将您发送的数据分段为较小的数据包。

如果您正在接收已知大小的数据,则接收器必须使用循环来获取有效负载的所有字节。

如果您不熟悉网络编程,您可以找到一些库来为您完成这项工作。

Boost的asio是我能想到的,但是它的语法可能有点难。

于 2013-09-23T05:00:29.813 回答