//
// main.c
// UDP client
//
// Created by 国耀 徐 on 11-11-6.
// Copyright 2011年 Wayne State University. All rights reserved.
//
#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#define PORT_SERV 8888
#define BUFF_LEN_Latency 2000
#define BUF_LEN_THROUGHPUT 35000
void measure_latency(int s, struct sockaddr*to,int n);
void measure_throughput(int s, struct sockaddr*to,int n);
/* client program:
The following must passed in on the command line:
the first character you want to measure(only one character) (argv[1])
*/
char buffer[BUFF_LEN_Latency];
char buf[BUF_LEN_THROUGHPUT];
int main(int argc, char *argv[]){
int s;
struct sockaddr_in addr_serv,addr_clie;
int t = 1;
struct timeval start,end;
float timeuse;
int i,j,h;
/* create a socket
IP protocol family (PF_INET)
TCP protocol (SOCK_DGRAM)
*/
s = socket(AF_INET, SOCK_DGRAM, 0);
memset(&addr_serv, 0 ,sizeof(addr_serv));
addr_serv.sin_family = AF_INET;
inet_aton(argv[1],&addr_serv.sin_addr);
addr_serv.sin_port = htons(PORT_SERV);
int nRecvBuf = 35000;
int nSendBuf = 35000;
setsockopt(s, SOL_SOCKET, SO_RCVBUF, &nRecvBuf, sizeof(nRecvBuf));
setsockopt(s, SOL_SOCKET, SO_SNDBUF, &nSendBuf, sizeof(nSendBuf));
for(i = 1; i <= 10; i++){
memset(buffer, 0, BUFF_LEN_Latency);
int num = i * 100;
for (j = 0; j < num;j++){
buffer[j] = t;
}
timeuse = 0;
gettimeofday(&start,NULL);
printf("The size of the messages sent to the server is:%d\n",num);
measure_latency(s, (struct sockaddr*)&addr_serv,num);
gettimeofday(&end,NULL); /*Sum up the time it had cost*/
timeuse = 1000000 * (end.tv_sec - start.tv_sec) + end.tv_usec - start.tv_usec;
timeuse /= 1000; /* Convert the seconds to the microseconds*/
printf("Execution time:%.1f milliseconds\n\n",timeuse);
}
printf("\n\nThen the next is for the test of throughput\n\n\n");
for(i = 0; i <= 5; i++){
memset(buf, 0, BUF_LEN_THROUGHPUT);
int k = 1;
if (i != 0){
for (h = 0;h < i;h++){
k *= 2;
}
}
int num = k * 1000;
printf("The size of the messages is:%d\n",num);
for (j = 0;j < num; j++){
buf[j] = t;
}
timeuse = 0;
gettimeofday(&start,NULL);
measure_throughput(s, (struct sockaddr*)&addr_serv,num);
gettimeofday(&end,NULL); /*Sum up the time it had cost*/
timeuse = 1000000 * (end.tv_sec - start.tv_sec) + end.tv_usec - start.tv_usec;
timeuse /= 1000; /* Convert the seconds to the microseconds*/
printf("Execution time:%.1f milliseconds\n\n",timeuse);
}
close(s);
return 0;
}
void measure_latency(int s, struct sockaddr*to,int n){
struct sockaddr_in from;
int k;
int len = sizeof(*to);
sendto(s, buffer, n, 0, to, len);
memset(buffer, 0, BUFF_LEN_Latency);
k = recvfrom(s, buffer, BUFF_LEN_Latency, 0, (struct sockaddr*)&from, &len);
printf("The size of the received data is:%d\n",k);
}
void measure_throughput(int s, struct sockaddr*to,int n){
struct sockaddr_in in;
int len = sizeof(*to);
int p;
sendto(s, buf, n, 0, to, len);
memset(buf, 0, BUF_LEN_THROUGHPUT);
p = recvfrom(s, buf, BUF_LEN_THROUGHPUT, 0, (struct sockaddr*)&in, &len);
printf("The size of the received data is:%d\n",p);
}