I am working on a small project to determine the upload speed of network.I have created a small Server in C which calculates the upload speed based on 1 MB data being sent by the client to the time taken for so.
Here is my server Code in C:
void main(int argc, char** argv){
int sock_desc = 0;
int connfd = 0;
int read_size = 0;
int clntSock;
struct sockaddr_in serv_addr;
struct sockaddr_in echoClntAddr;
int rc;
char client_message[1048576];//1048576 //524288
time_t start,end;
double t;
int temp;
double Bandwidth;
int result[1];
struct timeval tv1, tv2;
sock_desc = socket(AF_INET, SOCK_STREAM, 0);
if(sock_desc < 0 )
dieWithError("Unable to open Socket\n");
memset(&serv_addr,0,sizeof(serv_addr));
serv_addr.sin_family = AF_INET ;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(7024);
if(bind(sock_desc, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
dieWithError("bind failed\n");
if(listen(sock_desc,MAX_CONNECTIONS) < 0)
dieWithError("listen failed\n");
clntSock = sizeof(struct sockaddr);
bahar:
connfd = accept(sock_desc, (struct sockaddr *)&echoClntAddr,(socklen_t*)&clntSock);
printf("Connection accepted\n");
while(1){
read_size = recv(connfd , client_message ,1, 0);
gettimeofday(&tv1, NULL);
if(read_size < 1048576)
read_again: temp = read_size;
read_size = 0;
read_size = recv(connfd , client_message+temp , (sizeof(client_message)-temp), 0);
read_size+=temp;
printf("read size is %d\n",read_size);
temp=0;
if(read_size < 1048576)//524288
goto read_again;
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;
// Bandwidth/=1024;
printf("Network Bandwidth is %d Kbps\n",result[0]);//Bandwidth);
rc = send(connfd , result , 4, 0);//send Bandwidth to client
goto bahar;
}
}
void dieWithError(char *errormsg){
printf("%s", errormsg);
}
Here is my client code in Java:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
public class JavaSAmpleClient {
public static void main(String[] args)throws IOException {
int i;
int arr[] = new int[10];
byte bfr[] = new byte[1024*1024];
for (i=0;i<1048576;i++)
bfr[i] = (byte)i;
Socket s1 = new Socket("127.0.0.1",7024);
BufferedOutputStream bos = new BufferedOutputStream(s1.getOutputStream());
BufferedInputStream bos1 = new BufferedInputStream(s1.getInputStream());
bos.write(bfr);
i= bos1.read();//read bandwidth returned by server
System.out.println(i);
}
}
The problem being faced is that if I write the client program in C it is able to receive the upload speed correrctly.But in case of Java i am not getting correct results.
For Eg: In case of java client: If server sends the bandwidth as 4282279 the Java client receives it as 167.
In case of C client: If server sends the bandwidth as 4282279 the client receives it as 4282279.
I cannot do client programming in C because I need to run client application on android.
You have both a byte-order problem in your C sending code and a read()
coding problem in your Java code.
result[0] = htonl(Bandwidth);
for that in the C code.DataInputStream
around the BufferedInputStream
, and call readInt()
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.