recvfrom包含多余的字节

时间:2013-09-23 03:47:31

标签: c sockets tcp

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

当我尝试运行我的程序时,最后几个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);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

没有超额字节&#39;。 recvfrom()返回一个长度。你忽略了它。它也可以是EOS(0)或错误指示(-1,参见&#39; errno&#39;)。你需要检查所有这些可能性。

答案 1 :(得分:0)

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

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

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

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