Search This Blog

Friday, March 16, 2012

server.c timestamp

/* Program name : server.c
*Ajay Shankar Bidyarthy, Email: b.ajay@iitg.ernet.in
*/
#include /* printf(3) et al.                            */
#include /* getopt(3), read(2), etc.                    */
#include /* exit(2).                                    */
#include /* errno declaration & error codes.            */
#include   /* String manipulation & memory functions.     */
#include /* time(2) & ctime(3).                         */
#include
#include /* Socket functions (socket(2), bind(2), etc). */
#include /* sockaddr_in & sockaddr_in6 definition.      */
#include
#include
/*
* This function reports the error and exits back to the shell:
*/
static void bail(const char *on_what)
{
    fputs(strerror(errno),stderr);
    fputs(": ",stderr);
    fputs(on_what,stderr);
    fputc('\n',stderr);
    exit(1);
}

int main(int argc,char **argv)
{
    int z,i;
    char *srvr_addr = NULL;
    struct sockaddr_in adr_inet;/* AF_INET */
    struct sockaddr_in adr_clnt;/* AF_INET */
    int len_inet; /* length */
    int s; /* Socket */
    char dgram[512]; /* Recv buffer */
    struct stat sb;
/*
* Use a server address from the command line, if one has been provided. Otherwise, this program will default to using the arbitrary address
* 127.0.0.23:
*/
    if ( argc >= 2 )
    {
        /* Addr on cmdline: */
        srvr_addr = argv[1];
    }
    else
    {
        /* Use default address: */
        srvr_addr = "127.0.0.23";
    }

/*
* Create a UDP socket to use:
*/
    s = socket(AF_INET,SOCK_DGRAM,0);
    if ( s == -1 )
    bail("socket()");

/*
* Create a socket address, for use with bind(2):
*/
    memset(&adr_inet,0,sizeof adr_inet);
    adr_inet.sin_family = AF_INET;
    adr_inet.sin_port = htons(9099);
    adr_inet.sin_addr.s_addr =
    inet_addr(srvr_addr);

    if ( adr_inet.sin_addr.s_addr == INADDR_NONE )
    bail("bad address."); /* prints error if no such ip address exist in the network*/

    len_inet = sizeof adr_inet;

/*
* Bind a address to our socket, so that client programs can contact this  server:
*/
    z = bind(s, (struct sockaddr *)&adr_inet, len_inet);
    if ( z == -1 )
    bail("bind()");

/*
* Now wait for requests:
*/
    for (;;)
    {
/*
* Block until the program receives a datagram at our address and port:
*/socklen_t t;
t=sizeof adr_clnt;
//fprintf(stderr,"flag1");
        len_inet = sizeof adr_clnt;
        z = recvfrom(s, /* Socket */
        dgram, /* Receiving buffer */
        512, /* Max recv buf size */
        0, /* Flags: no options */
        (struct sockaddr *)&adr_clnt, /* Addr */
        &t); /* Addr len, in & out */
//        if ( z < 0 )
//        bail("recvfrom(2)");
        fprintf(stderr,"%d",strlen(dgram));
        char a[strlen(dgram)-4];
        for(i=strlen(dgram)-4;i>=0;i--)
        a[i]=dgram[i];
        fprintf(stderr,"%s",a);
        /* New */
        fprintf(stderr,"\n Hello \n");
        fprintf(stderr,"Client from %s port %u;\n",
        inet_ntoa(adr_clnt.sin_addr),
        (unsigned)ntohs(adr_clnt.sin_port));
        /* end new */
    fprintf(stderr,"%s",dgram);
        stat(a,&sb);
/*
* Process the request:
*/
    //    dgram[z] = 0; /* null terminate */
    //    if ( !strcasecmp(dgram,"QUIT") )
    //    break; /* Quit server */

/*
* Get the current date and time:
*/
    //    time(&td); /* Get current time & date */
//    tm = *localtime(&td); /* Get components */

/*
* Format a new date and time string, based upon the input format string:
*/
//        strftime(dtfmt, /* Formatted result */
//        sizeof dtfmt, /* Max result size */
//        dgram, /* Input date/time format */
//        &tm); /* Input date/time values */

/*
* Send the formatted result back to the client program:
*/
    printf("%s",ctime(&sb.st_mtime));
        z = sendto(s, /* Socket to send result */
        ctime(&sb.st_mtime), /* The datagram result to snd */
        strlen(ctime(&sb.st_mtime)), /* The datagram lngth */
        0, /* Flags: no options */
        (struct sockaddr *)&adr_clnt,/* addr */
        len_inet); /* Client address length */
        if ( z < 0 )
        bail("sendto(2)");
    }

/*
* Close the socket and exit:
*/
    close(s);
    return 0;
}

No comments:

Post a Comment

Thank you