Search This Blog

Friday, March 16, 2012

client.c timestamp

/* Program name : client.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

/*
* 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;
    socklen_t x;
    char *srvr_addr = NULL;
    struct sockaddr_in adr_srvr;/* AF_INET */
    struct sockaddr_in adr; /* AF_INET */
    int len_inet; /* length */
    int s; /* Socket */
    char dgram[512]; /* Recv buffer */

/*
* 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 socket address, to use to contact the server with:
*/
    memset(&adr_srvr,0,sizeof adr_srvr);
    adr_srvr.sin_family = AF_INET;
    adr_srvr.sin_port = htons(9099);
    adr_srvr.sin_addr.s_addr =
    inet_addr(srvr_addr);

    if ( adr_srvr.sin_addr.s_addr == INADDR_NONE )
    bail("bad address."); /*Prints error if ip address in not exist*/

    len_inet = sizeof adr_srvr;

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

    for (;;)
    {
/*
* Prompt user for a date format string:
*/
        //fputs("\nEnter the path of the file: ",stdout);
        //if ( !fgets(dgram,sizeof dgram,stdin) )
        //break; /* EOF */
        printf("\nEnter the path of the file: ");
        scanf("%s",dgram);

//        z = strlen(dgram);
//        if ( z > 0 && dgram[-z] == '\n' )
//        dgram[z] = 0; /* Stomp out newline */

/*
* Send format string to server:
*///fprintf(stderr,"flag1");
        z = sendto(s, /* Socket to send result */
        dgram, /* The datagram result to snd */
        strlen(dgram), /* The datagram lngth */
        0, /* Flags: no options */
        (struct sockaddr *)&adr_srvr,/* addr */
        len_inet); /* Server address length */
//fprintf(stderr,"flag2");
        if ( z < 0 )
        bail("sendto(2)");

/*
* Test if we asked for a server shutdown:
*/
        if ( !strcasecmp(dgram,"QUIT") )
        break; /* Yes, we quit too */

/*
* Wait for a response:
*/
        x = sizeof adr;
        z = recvfrom(s, /* Socket */
        dgram, /* Receiving buffer */
        512, /* Max recv buf size */
        0, /* Flags: no options */
        (struct sockaddr *)&adr, /* Addr */
        &x); /* Addr len, in & out */
        if ( z < 0 )
        bail("recvfrom(2)");

        /* New
        printf("\n Hello \n");
        printf("Client from %s port %u;\n",
        inet_ntoa(adr_clnt.sin_addr),
        (unsigned)ntohs(adr_clnt.sin_port));
         end new */

   
        dgram[z] = 0; /* null terminate */

/*
* Report Result:
*/
        printf("Result from %s port %u :\n\t'%s'\n",
        inet_ntoa(adr.sin_addr),
        (unsigned)ntohs(adr.sin_port),
        dgram);
    }

/*
* Close the socket and exit:
*/
    close(s);
    putchar('\n');

    return 0;
}

No comments:

Post a Comment

Thank you