hi
I was working on a UDP client and echo server. Let say my buffer is 20 byte only. I can send 20 byte form the server, and the server receives 20 byte successfully. But the problem arise when the server wants to echo the same thing inside the received buffer back to the client in which it will send 21 byte. another strange thing is that the client will accept 20 byte only..Is there some thing that I need to pay attention to with the sendto and recvfrom?
here is the server side
Code:
while (1) {
fflush(stdout);
/*if ( (sd2=accept(sd, (struct sockaddr *)&cad, &alen)) < 0) {
fprintf(stderr, "accept failed\n");
exit(1);
}*/
//printf("this is the child\n");
/* The sd has data available to be read */
//close(sd);
//clear buffer
memset(buf, 0, sizeof(buf));
n = recvfrom(sd, buf, sizeof(buf), 0, (struct sockaddr *)&cad, &alen);
//while there is still data
while(n > 0){
printf("received size: %d\n", n);
printf("%s\n", buf);
sleep(1);
n = sendto(sd, buf, strlen(buf), 0, (struct sockaddr *)&cad, sizeof(cad));
printf("Byte send back to client: %d\n", n);
//clear buffer
memset(buf, 0, sizeof(buf));
//receive data into buffer again
n = recvfrom(sd, buf, sizeof(buf), 0, (struct sockaddr *)&cad, &alen);
}
}
and here is the client side
Code:
n = read(fileno(ifp), buf, sizeof(buf));
while(n > 0)
{
//read from file
printf("Number of byte read from file: %d\n",n);
printf("%s\n", buf);
sendto(sd,buf,strlen(buf), 0, (struct sockaddr *)&sad, sizeof(sad));
//clear the buffer to avoid any garbage
memset(buf, 0, sizeof(buf));
alen = sizeof(sad);
//read the echo from server
n = recvfrom(sd, buf, sizeof(buf), 0, (struct sockaddr *)&sad, &alen);
printf("number of byte received from server: %d\n", n);
printf("%s\n", buf);
//write it out to to the output file
n = write(fileno(outputfiledesc), buf, n);
memset(buf, 0, sizeof(buf));
//get character, print and send it out
n = read(fileno(ifp), buf, sizeof(buf));
}
another strange problem is that when I run wireshark to check the packets, it saids that checksum incorrect? Can someone point me into the right direction?
thank you for all the help