IPv4 Daytime Client

home Author: Eva M. Castro
eva arroba gsyc.es

The daytime service is reduced to a request from the client and an answer from the server. In TCP version, the request is combined with connection a request. Therefore, the protocol answer is also combined with a connection acceptance message.

The daytime client is structured in two parts: connection phase and operation phase. Connection phase is grouped in one function (connect_client) and operation phase is reduced to a single read (or a receivefrom in the UDP version).


#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>

#include "connect_client.h"

const char *DAYTIME_PORT="13";


int
main(int argc, char *argv[])
{
    int connfd;
    char *myhost;
    char timeStr[256];

    myhost = "127.0.0.1";
    if (argc > 1)
        myhost = argv[1];

    connfd = connect_client(myhost, DAYTIME_PORT, AF_UNSPEC, 
                            SOCK_STREAM);

    if (connfd < 0) {
         fprintf(stderr, "client error:: could not create "
                         "connected socket\n");
         return -1;
    }

    memset(timeStr, 0, sizeof(timeStr));

    while (read(connfd, timeStr, sizeof(timeStr)) > 0) 
        printf("%s", timeStr);

    close(connfd);

    return 0;
}
Like listen_server function, the connect_client function is devoted to solve the connection phase in the client.


#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>

#include "connect_client.h"

int 
connect_client (const char *hostname, 
                const char *service, 
                int         family, 
                int         socktype)
{
    struct sockaddr_in sin;
    struct hostent *phe;
    struct servent *pse;
    struct protoent *ppe;
    int sockfd;
    char *protocol;
    
    memset(&sin, 0, sizeof(sin));

    sin.sin_family=AF_INET;

    switch(socktype) {

        case SOCK_DGRAM:
            protocol= "udp";
            break;
        case SOCK_STREAM:
            protocol= "tcp";
            break;
        default:
            fprintf(stderr, "listen_server:: unknown socket "
                            "type=[%d]\n", socktype);
            return -1;
    }


    if ( pse = getservbyname(service, protocol) ) {
        sin.sin_port = pse->s_port;

    } else if ((sin.sin_port = htons((u_short)atoi(service)))==0) {
         fprintf(stderr, "connec_client:: could not get "
                         "service=[%s]\n", service);
         return -1;
    }


    if (!hostname) {
        fprintf(stderr, "connect_client:: there should be a "
                "hostname!\n");
        return -1;
                
    } else {
        if (phe = gethostbyname(hostname)) {
            memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);

        } else if ( (sin.sin_addr.s_addr = inet_addr(hostname)) == 
						INADDR_NONE) {
             fprintf(stderr, "connect_client:: could not get "
                             "host=[%s]\n", hostname);
             return -1;
        }
    }

    if ((ppe = getprotobyname(protocol)) == 0) {
         fprintf(stderr, "connect_client:: could not get "
                         "protocol=[%s]\n", protocol);
         return -1;
    }
     
    if ((sockfd = socket(PF_INET, socktype, ppe->p_proto)) < 0) {  
        fprintf(stderr, "connect_client:: could not open socket\n");
        return -1;
    }

    if (connect(sockfd,(struct sockaddr *)&sin, sizeof(sin)) < 0) {
        fprintf(stderr, "connect_client:: could not connect "
                "to host=[%s]\n", hostname);
        return -1;
    }

    return sockfd;            
}


home home Author: Eva M. Castro
eva arroba gsyc.es