The UNIX Operating System

TOP

UP


Ray Seyfarth

ORCA


Computer Science

COST

USM




A TCP DAYTIME Client


  • DAYTIME service prints date and time

  • TCP version sends upon connection
    server reads no client data

  • UDP version sends upon receiving any message
    #define LINELEN         128
    
    int main(int argc, char *argv[])
    {
        char *host = "localhost";    /* host to use if none supplied */
        char *service = "daytime";   /* default service port         */
    
        switch (argc) {
        case 1:
            host = "localhost";
            break;
        case 3:
            service = argv[2];
            /* FALL THROUGH */
        case 2:
            host = argv[1];
            break;
        default:
            fprintf(stderr, "usage: TCPdaytime [host [port]]\n");
            exit(1);
        }
        TCPdaytime(host, service);
        exit(0);
    }
    
    void TCPdaytime(const char *host, const char *service)
    {
        char        buf[LINELEN+1];         /* buffer for one line of text  */
        int s, n;                   /* socket, read count           */
    
        s = connectTCP(host, service);
    
        while( (n = read(s, buf, LINELEN)) > 0) {
            buf[n] = '\0';          /* ensure null-terminated       */
            (void) fputs( buf, stdout );
        }
    }
    


University of Southern Mississippi
Ray.Seyfarth@usm.edu
Updated 20:05 Jun 5, 2000