Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/*
This file contains codes mostly developed by Dr.Ken Christensen. Email: christen@csee.usf.edu
http://www.csee.usf.edu/~christen/tools/toolpage.html
class : Computer Networks
*/
/* Code is readable and therefore comments are added when absolutely needed */
/* Header Files for time, sockets, IO and String */
#include <sys/time.h>
#include <sys/timeb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#define NAME_OF_HOST "grad.csee.usf.edu"
#define PORT_NUMBER 8081 // Port number associated with the Server
#define GET_HTTP_REQUEST "GET /2.html HTTP/1.0\n\n" // HTTP request
#define RESULTS_FILE_NAME "CenterCourt_Large_AM.xls"
#define CLIENT_BUFFER_SIZE 4096
int main(void)
{
struct hostent* host; // Structure for gethostbyname()
struct in_addr address; // Structure for Internet address
char host_ip[256]; // Host IP address
int client_s; // Client socket descriptor
struct sockaddr_in server_addr; // Server Internet address
char out_buf[CLIENT_BUFFER_SIZE]; // Output buffer for data
char in_buf[CLIENT_BUFFER_SIZE]; // Input buffer for data
int retcode; // Return code
FILE* fp = fopen(RESULTS_FILE_NAME, "w"); // FILE I/O
int experiments; // Number of experiments
int iterations; // Number of download attempts in a single Experiment
double total; // Total download time
struct timeb start, stop; //Timer
double elapsed;
for (experiments = 1; experiments <= 10; experiments++) {
if (experiments != 1)
sleep(5); // Waiting time between each experiment
total = 0;
for (iterations = 1; iterations <= 10; iterations++) {
sleep(0.1); //Interval between each iteration(download attempt of the current experiment)
double total_downloadtime = 0;
ftime(&start); //Timer start
host = gethostbyname(NAME_OF_HOST); //DNS resolve
if (host == NULL)
printf("IP address for '%s' could not be found\n", NAME_OF_HOST);
else {
memcpy(&address, host->h_addr, 4);
strcpy(host_ip, (char*)inet_ntoa(address));
}
client_s = socket(AF_INET, SOCK_STREAM, 0);
if (client_s < 0) {
printf("*** ERROR - socket() failed \n");
goto ex;
}
server_addr.sin_family = AF_INET; // Address family to use
server_addr.sin_port = htons(PORT_NUMBER); // Port num to use
server_addr.sin_addr.s_addr = inet_addr(host_ip); // IP address to use
retcode = connect(client_s, (struct sockaddr*)&server_addr, sizeof(server_addr)); // Connect
if (retcode < 0) {
printf("*** ERROR - connect() failed\n");
goto ex;
}
strcpy(out_buf, GET_HTTP_REQUEST);
retcode = send(client_s, out_buf, (strlen(out_buf) + 1), 0); //Send the HTTP Get request
if (retcode < 0) {
printf("*** ERROR - send() failed \n");
goto ex;
}
retcode = recv(client_s, in_buf, CLIENT_BUFFER_SIZE + 1, 0);
while ((retcode > 0) || (retcode == -1)) {
retcode = recv(client_s, in_buf, CLIENT_BUFFER_SIZE + 1, 0);
if (strlen(in_buf) == 0) {
break;
}
if (retcode < 0) {
printf("*** ERROR - recv() failed \n");
break;
}
}
ftime(&stop); //Stop timer
total_downloadtime = ((double)stop.time + ((double)stop.millitm * 0.001)) //Measure download time
- ((double)start.time + ((double)start.millitm * 0.001));
fprintf(fp, "\n%d\t%7.3f", iterations, total_downloadtime);
printf("%d\t%7.3f\n", iterations, total_downloadtime); //write to output file
strcpy(out_buf, "Message: Closing Connection");
retcode = send(client_s, out_buf, (strlen(out_buf) + 1), 0); //Send message to server informing intent to close
if (retcode < 0) {
printf("*** ERROR - send() failed \n");
goto ex;
}
retcode = close(client_s); //get the response for the close request
if (retcode < 0) {
printf("*** ERROR - close() failed \n");
goto ex;
}
total = total + total_downloadtime; // Add the total download time for the whole experiment
}
double Mean;
Mean = total / 10;
fprintf(fp, "\t%7.3f\n", Mean); //write results to file
printf("experiment %d finished: Mean is %7.3f\n", experiments, Mean);
}
fclose(fp); //Close the result file
ex: //Exit here when error happens
fflush(stdin);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.