Logo 
Search:

Networking Articles

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

TCP/IP program of iterative server for DAYTIME service

Posted By: Milind Mishra     Category: Networking     Views: 16984

TCP/IP program of iterative server for DAYTIME service.

Code for TCP/IP program of iterative server for DAYTIME service in Networking

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>

#include <unistd.h>
#include <stdio.h>
#include <string.h>

externint    errno;

int    daytime(char buf[]);
int    errexit(constchar *format, ...);
int    passiveTCP(constchar *service, int qlen);
int    passiveUDP(constchar *service);

#define    MAX(x, y)    ((x) > (y) ? (x) : (y))

#define    QLEN          5

#define    LINELEN        128

int
main(int argc, char *argv[])
{
    char    *service = "daytime";    /* service name or port number    */char    buf[LINELEN+1];        /* buffer for one line of text    */struct sockaddr_in fsin;    /* the request from address    */int    alen;            /* from-address length        */int    tsock;             /* TCP master socket        */int    usock;            /* UDP socket            */int    nfds;
    fd_set    rfds;            /* readable file descriptors    */switch (argc) {
    case    1:
        break;
    case    2:
        service = argv[1];
        break;
    default:
        errexit("usage: daytimed [port]\n");
    }

    tsock = passiveTCP(service, QLEN);
    usock = passiveUDP(service);
    nfds = MAX(tsock, usock) + 1;    /* bit number of max fd    */

    FD_ZERO(&rfds);

    while (1) {
        FD_SET(tsock, &rfds);
        FD_SET(usock, &rfds);

        if (select(nfds, &rfds, (fd_set *)0, (fd_set *)0,
                (struct timeval *)0) < 0)
            errexit("select error: %s\n", strerror(errno));
        if (FD_ISSET(tsock, &rfds)) {
            int    ssock;        /* TCP slave socket    */

            alen = sizeof(fsin);
            ssock = accept(tsock, (struct sockaddr *)&fsin,
                &alen);
            if (ssock < 0)
                errexit("accept failed: %s\n",
                        strerror(errno));
            daytime(buf);
            (void) write(ssock, buf, strlen(buf));
            (void) close(ssock);
        }
        if (FD_ISSET(usock, &rfds)) {
            alen = sizeof(fsin);
            if (recvfrom(usock, buf, sizeof(buf), 0,
                (struct sockaddr *)&fsin, &alen) < 0)
                errexit("recvfrom: %s\n",
                    strerror(errno));
            daytime(buf);
            (void) sendto(usock, buf, strlen(buf), 0,
                (struct sockaddr *)&fsin, sizeof(fsin));
        }
    }
}

/*------------------------------------------------------------------------ * daytime - fill the given buffer with the time of day *------------------------------------------------------------------------ */int
daytime(char buf[])
{
    char    *ctime();
    time_t    now;

    (void) time(&now);
    sprintf(buf, "%s", ctime(&now));
}
  
Share: 


Didn't find what you were looking for? Find more on TCP/IP program of iterative server for DAYTIME service Or get search suggestion and latest updates.

Milind Mishra
Milind Mishra author of TCP/IP program of iterative server for DAYTIME service 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!