It seems like it shouldn't work like that. I start the clock (on the client side) after sending the first packet (size 10 bytes and increasing linearly 10 bytes at time) and turn it off after sending the last packet.
On the server side (the recipient of the packets), I start the clock after receiving the first packet and stop after receiving the last packet. Why does it take longer to send than to receive?
Code for Client model (UDP sender)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <ctime>
#define TRIALS 6500 // will have 6500 trials to keep under maximum UDP packet length of 65536
#define SERVERPORT 4950 // the port users will be connecting to
#define BUFFER 65001 // the max # of bytes I will be sending
int main(int argc, char *argv[])
{
clock_t start; // for calculating time
long double duration = 0; // final result of the time
unsigned long long int totalbytes = 0; // for calculating total data
unsigned long long int throughput = 0; // will be final result of the throughput (i.e. totalbytes/duration)
int sockfd;
struct sockaddr_in their_addr; // connector's address information
struct hostent *he;
int numbytes;
if ((he=gethostbyname(argv[1])) == NULL) { // get the host info
perror("gethostbyname");
exit(1);
}
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(1);
}
their_addr.sin_family = AF_INET; // host byte order
their_addr.sin_port = htons(SERVERPORT); // short, network byte order
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
memset(&(their_addr.sin_zero), '\0', 8); // zero the rest of the struct
char message[BUFFER] = "abcdefghij"; //will start with 10 bytes
for (int i = 0; i < TRIALS; i++)
{
if ((numbytes = sendto(sockfd, message, strlen(message), 0,
(struct sockaddr *)&their_addr, sizeof(struct sockaddr))) == -1) {
perror("sendto");
exit(1);
}
if (i == 0)
{
start = clock(); //startin clock after sending first message
}
strcat(message, "abcdefghij"); //will add 10 bytes at each try
//printf("Trial #: %d\n", i);
//printf("sent %d bytes to %s\n", numbytes, inet_ntoa(their_addr.sin_addr));
totalbytes += numbytes; //updating total number of bytes sent
}
duration = (clock() - start ) / (double) CLOCKS_PER_SEC;
printf("\nThe process took %Lf seconds.\n", duration);
printf("A total of %llu bytes were sent.\n", totalbytes);
throughput = (long double)totalbytes/duration;
printf("The attempted data rate is: %llu bytes per second\n\n", throughput);
close(sockfd);
return 0;
}
and the code for the UDP server (receiver)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ctime>
#define MYPORT 4950 // the port users will be connecting to
#define MAXBUFLEN 65001 // max # of bytes I will be receiving.
int main(void)
{
int sockfd;
struct sockaddr_in my_addr; // my address information
struct sockaddr_in their_addr; // connector's address information
socklen_t addr_len;
int numbytes;
char buf[MAXBUFLEN];
clock_t start;
long double duration = 0; // final result of the time
unsigned long long int totalbytes = 0; // for calculating total data
unsigned long long int throughput = 0; // will be final result of the throughput (i.e. totalbytes/duration)
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(1);
}
my_addr.sin_family = AF_INET; // host byte order
my_addr.sin_port = htons(MYPORT); // short, network byte order
my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct
if (bind(sockfd, (struct sockaddr *)&my_addr,
sizeof(struct sockaddr)) == -1) {
perror("bind");
exit(1);
}
addr_len = sizeof(struct sockaddr);
int i = 0; // count number of packets received.
while(1)
{
if ((numbytes = recvfrom(sockfd, buf, MAXBUFLEN , 0,
(struct sockaddr *)&their_addr, &addr_len)) == -1) {
perror("recvfrom");
exit(1);
}
if(i == 0)
{
start = clock(); // starting time when receiving the first packet of length = 10 bytes.
}
//printf("Trial #: %d\n", i);
//printf("got packet from %s\n",inet_ntoa(their_addr.sin_addr));
//printf("packet is %d bytes long\n",numbytes);
totalbytes += numbytes;
if(numbytes >= 65000) //if last packet lets break out of while loop
{
break;
}
//buf[numbytes] = '\0';
//printf("packet contains \"%s\"\n",buf);
++i;
}
duration = (clock() - start ) / (double) CLOCKS_PER_SEC;
printf("\nThe process took %Lf seconds.\n", duration);
printf("A total of %llu bytes were received.\n", totalbytes);
throughput = (long double)totalbytes/duration;
printf("The actual data rate is: %llu bytes per second\n\n", throughput);
close(sockfd);
return 0;
}
compile and run server
user:socket$ g++ udp_server.cc -o udp_server
user:socket$ ./udp_server
compile and run server
user:socket$ g++ udp_client.cc -o udp_client
user:socket$ ./udp_client 127.0.0.1
Sample results:
Client (sender)
The process took 0.238971 seconds.
A total of 211282500 bytes were sent.
The attempted data rate is: 884134476 bytes per second
Server (receiver)
The process took 0.042443 seconds.
A total of 211214670 bytes were received.
The actual data rate is: 4976431213 bytes per second