[C] Linux中时间服务器端代码 →→→→→进入此内容的聊天室

来自 , 2020-10-25, 写在 C, 查看 127 次.
URL http://www.code666.cn/view/ddeebdee
  1. /* timeserv.c - a socket-based time of day server
  2.  */
  3.  
  4. #include  <stdio.h>
  5. #include  <unistd.h>
  6. #include  <sys/types.h>
  7. #include  <sys/socket.h>
  8. #include  <netinet/in.h>
  9. #include  <netdb.h>
  10. #include  <time.h>
  11. #include  <strings.h>
  12.  
  13. #define   PORTNUM  13000   /* our time service phone number */
  14. #define   HOSTLEN  256
  15. #define   oops(msg)      { perror(msg) ; exit(1) ; }
  16.  
  17. int main(int ac, char *av[])
  18. {
  19.         struct  sockaddr_in   saddr;   /* build our address here */
  20.         struct  hostent         *hp;   /* this is part of our    */
  21.         char    hostname[HOSTLEN];     /* address                */
  22.         int     sock_id,sock_fd;       /* line id, file desc     */
  23.         FILE    *sock_fp;              /* use socket as stream   */
  24.         char    *ctime();              /* convert secs to string */
  25.         time_t  thetime;               /* the time we report     */
  26.  
  27.       /*
  28.        * Step 1: ask kernel for a socket
  29.        */
  30.  
  31.         sock_id = socket( PF_INET, SOCK_STREAM, 0 );    /* get a socket */
  32.         if ( sock_id == -1 )
  33.                 oops( "socket" );
  34.  
  35.       /*
  36.        * Step 2: bind address to socket.  Address is host,port
  37.        */
  38.  
  39.         bzero( (void *)&saddr, sizeof(saddr) ); /* clear out struct     */
  40.  
  41.         gethostname( hostname, HOSTLEN );       /* where am I ?         */
  42.         hp = gethostbyname( hostname );         /* get info about host  */
  43.                                                 /* fill in host part    */
  44.         bcopy( (void *)hp->h_addr, (void *)&saddr.sin_addr, hp->h_length);
  45.         saddr.sin_port = htons(PORTNUM);        /* fill in socket port  */
  46.         saddr.sin_family = AF_INET ;            /* fill in addr family  */
  47.  
  48.         if ( bind(sock_id, (struct sockaddr *)&saddr, sizeof(saddr)) != 0 )
  49.                oops( "bind" );
  50.  
  51.       /*
  52.        * Step 3: allow incoming calls with Qsize=1 on socket
  53.        */
  54.  
  55.         if ( listen(sock_id, 1) != 0 )
  56.                 oops( "listen" );
  57.  
  58.       /*
  59.        * main loop: accept(), write(), close()
  60.        */
  61.  
  62.         while ( 1 ){
  63.                sock_fd = accept(sock_id, NULL, NULL); /* wait for call */
  64.                 printf("Wow! got a call!\n");
  65.                if ( sock_fd == -1 )
  66.                        oops( "accept" );       /* error getting calls  */
  67.  
  68.                sock_fp = fdopen(sock_fd,"w");  /* we'll write to the   */
  69.                if ( sock_fp == NULL )          /* socket as a stream   */
  70.                        oops( "fdopen" );       /* unless we can't      */
  71.  
  72.                thetime = time(NULL);           /* get time             */
  73.                                                /* and convert to strng */
  74.                fprintf( sock_fp, "The time here is .." );
  75.                fprintf( sock_fp, "%s", ctime(&thetime) );
  76.                fclose( sock_fp );              /* release connection   */
  77.         }
  78. }

回复 "Linux中时间服务器端代码"

这儿你可以回复上面这条便签

captcha