home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / pmsock01.zip / daytime.c next >
Text File  |  1994-09-23  |  2KB  |  111 lines

  1.  
  2.  
  3. #undef DEBUG
  4. #include <stdio.h>
  5. #include "pmsock.h"
  6.  
  7. WSADATA WSAData;
  8. #define WSVERSION 0x101
  9.  
  10.  
  11. int main(int argc, char *argv[])
  12. {
  13.   int i, err;
  14.   int dayport; 
  15.   SOCKET s;
  16.   SERVENT *pse;    /* pts to "service" data (internally allocated) */
  17.   unsigned long ipa;  // for Internet address of host
  18.   HOSTENT *phe; // pts to host info struct (internally allocated)
  19.   char szHost[255];
  20.  
  21. #ifdef DEBUG
  22.   printf("Calling WSAStartup\n");
  23. #endif
  24.   if (err = WSAStartup(WSVERSION, &WSAData))
  25.   {
  26.      printf("Error starting up\n");
  27.      exit(1);
  28.   }
  29. #ifdef DEBUG
  30.   printf("Getting service name\n");
  31. #endif
  32.   if (pse = getservbyname("daytime", "tcp"))
  33.   {
  34.  
  35.     dayport = pse->s_port;
  36. #ifdef DEBUG
  37.     printf("Debug: daytime port is %d\n", ntohs(dayport));
  38. #endif
  39.   }
  40.   
  41.   if (!argv[1]) {
  42.     if (gethostname(szHost, sizeof(szHost))==SOCKET_ERROR) {
  43.       printf("Error getting hostname\n");
  44.       exit(1);
  45.     }
  46.   }
  47.   else 
  48.     strcpy( szHost, argv[1]);
  49.  
  50. #ifdef DEBUG
  51.   printf("Checking host %s\n", szHost);
  52. #endif
  53.   if ((ipa = inet_addr(szHost)) == INADDR_NONE) 
  54.   {
  55.     if (phe = gethostbyname(szHost))
  56.     {
  57.       ipa = *((u_long *)phe->h_addr);
  58.     }
  59.     else
  60.     {
  61.       printf("can't get inet addr\n");
  62.       exit(1); 
  63.     }
  64.   }
  65. #ifdef DEBUG
  66.   printf( "Found IP for %s = %x\n", szHost, ntohl(ipa)); 
  67. #endif
  68.  
  69.   if ((s = socket(AF_INET, SOCK_STREAM, 0)) >= 0)
  70.   {
  71.      SOCKADDR_IN server;
  72.  
  73.      // address of the time server
  74.      memset(&server, 0, sizeof(server));
  75.      server.sin_family = AF_INET;
  76.      server.sin_port = dayport;
  77.      server.sin_addr.s_addr = ipa;
  78.  
  79.      // establish a stream connection to server
  80.      if (!connect(s, (SOCKADDR *)&server, sizeof(server)))
  81.      {
  82.         char msg[255];
  83.         int msglen;
  84.  
  85.         strcpy(msg, "\r\n");
  86.         msglen = strlen(msg);
  87. #ifdef DEBUG
  88.         printf( "Socket connected, sending CRLF\n");
  89. #endif
  90.  
  91.         if (send(s, msg, msglen, 0) == msglen)
  92.         {
  93.            static char buf[500];      // receive buffer
  94.        int nchars;
  95. #ifdef DEBUG
  96.            printf("Reading result..\n");
  97. #endif
  98.            printf("\n");
  99.     
  100.            while (( nchars = recv(s, (char FAR *)&buf,
  101.              sizeof(buf), 0)) > 0)
  102.                printf("%s",buf);             // add chars to display list
  103.            printf("\n");
  104.          }
  105.      closesocket(s);
  106.       }
  107.    }
  108. }
  109.  
  110.  
  111.