Time client code in Linux

From , 4 Years ago, written in C, viewed 52 times.
URL https://pastebin.vip/view/186fb23a
  1. /* timeclnt.c - a client for timeserv.c
  2.  *              usage: timeclnt hostname portnumber
  3.  */
  4. #include       <stdio.h>
  5. #include       <sys/types.h>
  6. #include       <sys/socket.h>
  7. #include       <netinet/in.h>
  8. #include       <netdb.h>
  9.  
  10. #define        oops(msg)       { perror(msg); exit(1); }
  11.  
  12. main(int ac, char *av[])
  13. {
  14.         struct sockaddr_in  servadd;        /* the number to call */
  15.         struct hostent      *hp;            /* used to get number */
  16.         int    sock_id, sock_fd;            /* the socket and fd  */
  17.         char   message[BUFSIZ];             /* to receive message */
  18.         int    messlen;                     /* for message length */
  19.  
  20.      /*
  21.       * Step 1: Get a socket
  22.       */
  23.  
  24.         sock_id = socket( AF_INET, SOCK_STREAM, 0 );    /* get a line   */
  25.         if ( sock_id == -1 )
  26.                 oops( "socket" );                       /* or fail      */
  27.  
  28.      /*
  29.       * Step 2: connect to server
  30.       *         need to build address (host,port) of server  first
  31.       */
  32.  
  33.         bzero( &servadd, sizeof( servadd ) );   /* zero the address     */
  34.  
  35.         hp = gethostbyname( av[1] );            /* lookup host's ip #   */
  36.         if (hp == NULL)
  37.                 oops(av[1]);                    /* or die               */
  38.         bcopy(hp->h_addr, (struct sockaddr *)&servadd.sin_addr, hp->h_length);
  39.  
  40.         servadd.sin_port = htons(atoi(av[2]));  /* fill in port number  */
  41.  
  42.         servadd.sin_family = AF_INET ;          /* fill in socket type  */
  43.  
  44.                                                        /* now dial     */
  45.         if ( connect(sock_id,(struct sockaddr *)&servadd, sizeof(servadd)) !=0)
  46.                oops( "connect" );
  47.  
  48.      /*
  49.       * Step 3: transfer data from server, then hangup
  50.       */
  51.  
  52.         messlen = read(sock_id, message, BUFSIZ);     /* read stuff   */
  53.         if ( messlen == - 1 )
  54.                oops("read") ;
  55.         if ( write( 1, message, messlen ) != messlen )  /* and write to */
  56.                oops( "write" );                        /* stdout       */
  57.         close( sock_id );    
  58. }

Reply to "Time client code in Linux"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website