Paul Sen
Paul Sen

Reputation: 554

How can i calculate upload speed accurately

I want to calculate the upload speed in my android device. For this i have created a socket-server, to which i send 1 MB of data from my android device through socket.By dividing the number of bytes with the time i calculate my upload speed.

The issue being face is that when i calculate upload speed using 1 MB data it is almost 1.5 times than that calculated using 1/2 MB data. I am not able to find out the issue.

Here is my code which calculates the upload speed in C. This is for 1MB data.For 1/2 MB data i just change the bytes to 524288 and same thing i do while sending from Android.

 int sock = *(int*)socket_desc;
int read_size;  
char client_message[1048576];//1048576
int rc = 0;
int temp;
double Bandwidth;
int result[1];
double t;
struct timeval  tv1, tv2;

          read_size = 0;
          temp = 0;
          gettimeofday(&tv1, NULL);
          read_size = recv(sock , client_message ,sizeof(client_message), 0); 


          if(read_size < (1048576)){
read_again1:  temp = read_size;
             read_size = 0;
             read_size = recv(sock , client_message+temp , (sizeof(client_message)-temp), 0); 
             read_size+=temp;
             temp=0; 
             if(read_size < (1048576))
                goto read_again1; 
          }  

         gettimeofday(&tv2, NULL);
         t =  (double) (tv2.tv_usec - tv1.tv_usec) / 1000000 +
            (double) (tv2.tv_sec - tv1.tv_sec);

          read_size*=8;
          read_size/=1024;
          Bandwidth = read_size/t;

          result[0]= (int) Bandwidth;
          rc = send(sock , result , 4, 0);

          read_size = recv(sock , client_message ,1024, 0); 

        if(read_size == 0){
           exit_thr = 1;
           rc = close(sock);
           pthread_exit(&exit_thr);
        }
        else if(read_size == -1){
                rc = close(sock);
                pthread_exit(&exit_thr);
        }     

Here is my Android side code:

 byte buffer[] = new byte[1048576];
         byte recvBuff[] = new byte[4]; 
         String strHexNumber, strHexNumber1= "";

       for(i=0;i<1048576;i++) 
        buffer[i] = (byte)i;

       try {
        bandwidth = 0;
        strHexNumber = strHexNumber1 ="";
         InetAddress addr = InetAddress.getByName("52.28.xxx.xx");

        Socket socket = new Socket();
        socket.connect(new InetSocketAddress(addr,8xxx));

        BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());


           bos.write(buffer);

           BufferedInputStream bos1 = new BufferedInputStream(socket.getInputStream());

           bos1.read(recvBuff,0,4);

In above code some braces may be missing.This code only contains the processing part for bandwidth other part's like connection accepting is omitted

Upvotes: 0

Views: 587

Answers (1)

user207421
user207421

Reputation: 310840

In your Java code you're assuming that read() fills the buffer, and you aren't checking for it returning -1.

In your C code you aren't checking for recv() returning -1 or zero. This code is incredibly convoluted, seeing all it has to do is read a fixed number of bytes. The purpose of the final read after the loop escapes me completely.

Upvotes: 1

Related Questions