Logo 
Search:

Networking Articles

Submit Article
Home » Articles » Networking » TCP/IPRSS Feeds

TCP/IP program of UDP client for TIME service that prints the resulting time

Posted By: Milind Mishra     Category: Networking     Views: 3998

TCP/IP program of UDP client for TIME service that prints the resulting time.

Code for TCP/IP program of UDP client for TIME service that prints the resulting time in Networking

#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#define    BUFSIZE 64

#define    UNIXEPOCH    2208988800    /* UNIX epoch, in UCT secs    */#define    MSG        "what time is it?\n"externint    errno;

int    connectUDP(constchar *host, constchar *service);
int    errexit(constchar *format, ...);

/*------------------------------------------------------------------------ * main - UDP client for TIME service that prints the resulting time *------------------------------------------------------------------------ */int
main(int argc, char *argv[])
{
    char    *host = "localhost";    /* host to use if none supplied    */char    *service = "time";    /* default service name        */
    time_t    now;            /* 32-bit integer to hold time    */int    s, n;            /* socket descriptor, read count*/switch (argc) {
    case 1:
        host = "localhost";
        break;
    case 3:
        service = argv[2];
        /* FALL THROUGH */case 2:
        host = argv[1];
        break;
    default:
        fprintf(stderr, "usage: UDPtime [host [port]]\n");
        exit(1);
    }

    s = connectUDP(host, service);

    (void) write(s, MSG, strlen(MSG));

    /* Read the time */

    n = read(s, (char *)&now, sizeof(now));
    if (n < 0)
        errexit("read failed: %s\n", strerror(errno));
    now = ntohl((u_long)now);    /* put in host byte order    */
    now -= UNIXEPOCH;        /* convert UCT to UNIX epoch    */
    printf("%s", ctime(&now));
    exit(0);
}
  
Share: 



Milind Mishra
Milind Mishra author of TCP/IP program of UDP client for TIME service that prints the resulting time is from India.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!