Advertisement
Guest User

Untitled

a guest
Feb 15th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.86 KB | None | 0 0
  1. /* fpont 12/99 */
  2. /* pont.net    */
  3. /* udpServer.c */
  4.  
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <netinet/in.h>
  8. #include <netdb.h>
  9. #include <stdio.h>          //printf
  10. #include <stdlib.h>
  11. #include <arpa/inet.h>
  12. #include <linux/if_packet.h>
  13. #include <string.h>         //strncpy
  14. #include <sys/ioctl.h>
  15. #include <sys/socket.h>
  16. #include <net/if.h>         //ifreq
  17. #include <unistd.h>         //close
  18. #include <netinet/ether.h>
  19. #include <sys/time.h>       //gettimeofday
  20. #include <pthread.h>
  21. #include <string.h>
  22. #include <arpa/inet.h>
  23. #include <netinet/in.h>
  24. #include <sys/ioctl.h>
  25. #include <linux/if_packet.h>
  26. #include <time.h>
  27. #include <math.h>
  28.  
  29. //#include <stdlib.h>
  30. //#include <sys/types.h>
  31. //#include <sys/socket.h>
  32. //#include <netinet/in.h>
  33. //#include <arpa/inet.h>
  34. //#include <netdb.h>
  35. //#include <stdio.h>
  36. //#include <unistd.h> /* close() */
  37. //#include <string.h> /* memset() */
  38. //#include <time.h>
  39.  
  40. #define LOCAL_SERVER_PORT 1500
  41. #define MAX_MSG 100
  42.  
  43. void printCurrentTime();
  44.  
  45. int main(int argc, char *argv[]) {
  46.  
  47.   int sd, rc, n, cliLen;
  48.   struct sockaddr_in cliAddr, servAddr;
  49.   char msg[MAX_MSG];
  50.  
  51.   /* socket creation */
  52.   sd=socket(AF_INET, SOCK_DGRAM, 0);
  53.   if(sd<0) {
  54.     printf("%s: cannot open socket \n",argv[0]);
  55.     exit(1);
  56.   }
  57.  
  58.   /* bind local server port */
  59.   servAddr.sin_family = AF_INET;
  60.   //servAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  61.   servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  62.   servAddr.sin_port = htons(LOCAL_SERVER_PORT);
  63.   rc = bind (sd, (struct sockaddr *) &servAddr,sizeof(servAddr));
  64.  
  65.   if(rc<0) {
  66.     printf("%s: cannot bind port number %d \n",
  67.        argv[0], LOCAL_SERVER_PORT);
  68.     exit(1);
  69.   }
  70.  
  71.   printf("%s: waiting for data on port UDP %u\n",
  72.        argv[0],LOCAL_SERVER_PORT);
  73.  
  74.   /* server infinite loop */
  75.   int i = 1;
  76.   while(1) {
  77.    
  78.     /* init buffer */
  79.     memset(msg,0x0,MAX_MSG);
  80.  
  81.  
  82.     /* receive message */
  83.     cliLen = sizeof(cliAddr);
  84.     n = recvfrom(sd, msg, MAX_MSG, 0,
  85.          (struct sockaddr *) &cliAddr, &cliLen);
  86.  
  87.     printCurrentTime();
  88.  
  89.     if(n<0) {
  90.       //printf("%s: cannot receive data \n",argv[0]);
  91.       continue;
  92.     }
  93.      
  94.     /* print received message */
  95.     //printf("%s: from %s:UDP%u : %s \n",
  96.        //argv[0],inet_ntoa(cliAddr.sin_addr),
  97.        //ntohs(cliAddr.sin_port),msg);
  98.    
  99.     //printf ("(%d)\n", i++);
  100.   }/* end of server infinite loop */
  101.  
  102. return 0;
  103.  
  104. }
  105.  
  106. void printCurrentTime() {
  107.   char buffer[26];
  108.   int millisec;
  109.   struct tm* tm_info;
  110.   struct timeval tv;
  111.  
  112.   gettimeofday(&tv, NULL);
  113.  
  114.   millisec = lrint(tv.tv_usec/1000.0); // Round to nearest millisec
  115.   if (millisec>=1000) { // Allow for rounding up to nearest second
  116.     millisec -=1000;
  117.     tv.tv_sec++;
  118.   }
  119.  
  120.   tm_info = localtime(&tv.tv_sec);
  121.  
  122.   strftime(buffer, 26, "%Y:%m:%d %H:%M:%S", tm_info);
  123.   printf("%s.%03d\n", buffer, millisec);
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement