// Source code arr_serv.c // Written by Darren Challey // Rensselaer Polytechnic Institute // Spring 2000 // This server is to be used to determine the smoothing characteristics // of traffic shapers. It determines mean, min, max, and standard // deviation of arrival times // Based upon programs found in "UNIX Network Programming" - Stevens #include #include "netinet/in.h" // Needed for sockaddr_in struct #include // Needed for timeval #include // Needed for file I/O #include // Needed for error function #include "dc_const.h" // Needed for program constants int main() { // Open a file to write data FILE *arrData; int sockfd = 0; // socket file descriptor int in = 0; // input received from socket int a = 0; char msg[MAXLINE]; double arrive[LOOP + 1]; double delta[LOOP + 1]; double mean_sum = 0.0; double mean = 0.0; double std_sum = 0.0; double diff = 0.0; double std_dev = 0.0; double min = 10.0; double max = 0.0; int count = 0; int sec = 0; double mic = 0.0; struct sockaddr_in servaddr; struct sockaddr_in cliaddr; struct timeval tv; // Initialization for (a = 0; a < MAXLINE; a++) { msg[a] = ' '; } arrive[0] = 0; // Array element not used delta[0] = 0; // Array element not used printf("\nStarting Server\n"); if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { perror("No Socket"); printf("errno = %d.", errno); // p475 exit(1); } // If socket is successful, sockfd will be a small positive integer // arrData = fopen("/tmp/arrData.txt, "w+"); arrData = fopen("arrData.txt", "w+"); // w = write-enabled, + will allow file to be overwritten bzero(&servaddr, sizeof(servaddr)); // set entire structure to 0's servaddr.sin_family = AF_INET; // set family to IPV4 servaddr.sin_addr.s_addr = CLIENT_IP; // set in dc_const.h servaddr.sin_port = htons(52330); // randomly selected ephemeral // ports as being 49152 to 65535 if ( bind(sockfd, (struct sockaddr*) &servaddr, sizeof(servaddr)) < 0 ) { printf( "bind failed" ); exit(1); } else printf("\nBind Successful\n"); // Read information from the socket in = recvfrom(sockfd, msg, MAXLINE, 0, NULL, NULL); while(msg[0] != 'E') { // Loop until last datagram received count++; // Determine arrival time gettimeofday(&tv, NULL); sec = tv.tv_sec; mic = tv.tv_usec / 1000000.0; arrive[count] = (double)sec + mic; // Print arrival time to the screen and file printf("\ntime arrived: %f", arrive[count]); fprintf(arrData, "%f\n", arrive[count]); // Read information from the socket in = recvfrom(sockfd, msg, MAXLINE, 0, NULL, NULL); } // End of Loop while not last datagram // OUTPUT OF RESULTS //////////////////////////////////////// printf("\n\nTotal Packets Rec'd = %d", count); // Calculate the mean time of arrivals for (a = 2; a < LOOP + 1; a++) { delta[a] = arrive[a] - arrive[a - 1]; mean_sum += delta[a]; if (delta[a] > max) max = delta[a]; if (delta[a] < min) min = delta[a]; } mean = mean_sum / ((double)LOOP - 1.0); printf("\nMean interarrival time = %f", mean); // Calculate Standard Deviation for (a = 2; a < LOOP + 1; a++) { diff = mean - delta[a]; std_sum += diff * diff; } std_dev = std_sum / ((double)LOOP - 1.0); printf("\nMinimum interarrival time = %f", min); printf("\nMaximum interarrival time = %f", max); printf("\nStandard Deviation = %E", std_dev); fclose(arrData); printf("\n\n"); } // End Main