Question

I have a strange question. I did the following test on android:

i=0;
while(i< PACKET_NUMBER)
{

//UDP send packets
if( sendto(sockfd,buffer,strlen(buffer),0, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) <= 0)
{
error("failed to send packets to remote host via udp\n");
}
i++;
}

I did this tests with different buffer size. When the buffer size is smaller than a number(it should be around 500bytes), no mater which size I choose, such as 20 40 80 160 320 , the while statement can be performed in a nearly same time, such as 3mins 10secs. However, when the packet size is lager than 500, like 510 520 600 1280, the perform time of the while statement is much more longer, such as longer than 10 mins. Can anyone give me some hints about the reason?

Was it helpful?

Solution

In fact, the problem is most likely caused by the strlen(), which tries to find /0 character in buffer on each iteration of the while() loop!

Replace it with a variable, and execution speed will increase.

Another possible reason is packet fragmentation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top