3 //One clock tick = (1 / CPU processor speed in Hz) secs
5 // gcc -Wall -o server clocksyncserver.c
10 #include <sys/socket.h>
11 #include <netinet/in.h>
22 static __inline__ unsigned long long rdtsc(void) {
24 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
25 return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
29 unsigned long long dir[DIRSIZE]; /* used for incomming dir name, and
33 struct sockaddr_in sin;
34 struct sockaddr_in pin;
37 f1=fopen("dc-1", "w");
39 unsigned long long array1[NUMITER];
40 unsigned long long array2[NUMITER];
41 /* get an internet domain socket */
42 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
47 /* complete the socket structure */
48 memset(&sin, 0, sizeof(sin));
49 sin.sin_family = AF_INET;
50 sin.sin_addr.s_addr = INADDR_ANY;
51 sin.sin_port = htons(PORT);
53 /* bind the socket to the port number */
54 if (bind(sd, (struct sockaddr *) &sin, sizeof(sin)) == -1) {
59 /* show that we are willing to listen */
60 if (listen(sd, 5) == -1) {
64 /* wait for a client to talk to us */
65 addrlen = sizeof(pin);
66 if ((sd_current = accept(sd, (struct sockaddr *)&pin, &addrlen)) == -1) {
70 /* if you want to see the ip address and port of the client, uncomment the
74 printf("Hi there, from %s#\n",inet_ntoa(pin.sin_addr));
75 printf("Coming from port %d\n",ntohs(pin.sin_port));
82 send(sd_current, data, sizeof(data), MSG_NOSIGNAL);
83 for(i=0; i<NUMITER; i++) {
84 /* get a message from the client */
85 if (recv(sd_current, dir, sizeof(unsigned long long), 0) == -1) {
89 //printf("DEBUG: dir[0]= %lld\n", dir[0]);
91 //printf("DEBUG: array2[i]= %lld\n", array2[i]);
92 //array1[i]=array2[i] - dir[0];
93 array1[i]= dir[0] - array2[i];
94 printf("%lld\n", array1[i]);
96 /* acknowledge the message, reply w/ the file names */
97 if (send(sd_current, &array2[i], sizeof(unsigned long long), MSG_NOSIGNAL) == -1) {
104 for(i=0; i<(NUMITER-1); i++) {
108 /* spew-out the results */
109 //printf("DEBUG: Average offset= %lld\n", (norm/(NUMITER-1)));
110 long long average=(norm/(NUMITER-1));
111 printf("average= %lld",(norm/(NUMITER-1)));
113 long long stddev, avg1=0;
114 for(i=0; i<(NUMITER-1); i++) {
115 avg1 += ((array1[i] - average) * (array1[i] - average));
117 float ans = (avg1/(NUMITER-1));
118 float squareroot= sqrt(ans);
119 float squareroot2= sqrt(avg1);
121 printf("stddev= %f\n", squareroot);
122 printf("error= %f\n", squareroot2/(NUMITER-1));
123 fprintf(f1,"%lld\n",(norm/(NUMITER-1)));
126 /* give client a chance to properly shutdown */
129 /* close up both sockets */