Logo 
Search:

Networking Articles

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

TCP/IP program of Iterative TCP server for DAYTIME service protocol

Posted By: Milind Mishra     Category: Networking     Views: 4181

TCP/IP program of Iterative TCP server for DAYTIME service protocol.

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

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

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

externint    errno;
int        errexit(constchar *format, ...);
int        TCPdaytimed(int fd);
int        passiveTCP(constchar *service, int qlen);

#define QLEN    5

/*------------------------------------------------------------------------ * main - Iterative TCP server for DAYTIME service *------------------------------------------------------------------------ */int
main(int argc, char *argv[])
{
    struct    sockaddr_in fsin;    /* the from address of a client    */char    *service = "daytime";    /* service name or port number    */int    msock, ssock;        /* master & slave sockets    */int    alen;            /* from-address length        */switch (argc) {
    case    1:
        break;
    case    2:
        service = argv[1];
        break;
    default:
        errexit("usage: TCPdaytimed [port]\n");
    }

    msock = passiveTCP(service, QLEN);

    while (1) {
        ssock = accept(msock, (struct sockaddr *)&fsin, &alen);
        if (ssock < 0)
            errexit("accept failed: %s\n", strerror(errno));
        (void) TCPdaytimed(ssock);
        (void) close(ssock);
    }
}

/*------------------------------------------------------------------------ * TCPdaytimed - do TCP DAYTIME protocol *------------------------------------------------------------------------ */int
TCPdaytimed(int fd)
{
    char    *pts;            /* pointer to time string    */
    time_t    now;            /* current time            */char    *ctime();

    (void) time(&now);
    pts = ctime(&now);
    (void) write(fd, pts, strlen(pts));
    return 0;
}
  
Share: 



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