recvfrom contiene exceso de bytes
Frecuentes
Visto 266 veces
-2
This is a multi-client socket program.
When I am trying to run my program the few last recvfrom() do not return any data. But the recvfrom before that have the excess data. Can someone help me how to fix this.
Cliente:
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));
}
Servidor:
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 Respuestas
1
There are no 'excess bytes'. recvfrom() returns a length. You are ignoring it. It could also be an EOS (0) or an error indication (-1, see 'errno'). You need to check all those possibilities.
Respondido el 23 de Septiembre de 13 a las 05:09
0
TCP connection may segment the data you send to smaller packets.
If you are receiving data of known size, the receiver have to use a loop to get all the bytes of the payload.
You could find some library to do this job for you if you're new to network programming.
Boost's asio is one that I can think of, but its syntax may be a bit difficult.
Respondido el 23 de Septiembre de 13 a las 06:09
Or larger. It may coalesce. - user207421
how can I stop receiving bytes if all I want to receive is just the the bytes send by one use of sendto? - Rojo
simply breaking out of the while loop might do - leesei
@Red There is no such thing in TCP as 'just the bytes sent by one use of sendto()
. It is a bye-stream protocol. No message boundaries. - user207421
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas c sockets tcp or haz tu propia pregunta.
You don't seem to have any understanding how recvfrom works. Go read the documentation and look for examples. Also, see what EJP said. - xaxxon