Networking Articles

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

TCP/IP program of TCP client for ECHO service

Posted By: Milind Mishra     Category: Networking     Views: 6856

TCP/IP program of TCP client for ECHO service.

Code for TCP/IP program of TCP client for ECHO service in Networking

/* TCPecho.c - main, TCPecho */

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

externint    errno;

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

#define    LINELEN        128

/*------------------------------------------------------------------------ * main - TCP client for ECHO service *------------------------------------------------------------------------ */int
main(int argc, char *argv[])
    char    *host = "localhost";    /* host to use if none supplied    */char    *service = "echo";    /* default service name        */switch (argc) {
    case 1:
        host = "localhost";
    case 3:
        service = argv[2];
        /* FALL THROUGH */case 2:
        host = argv[1];
        fprintf(stderr, "usage: TCPecho [host [port]]\n");
    TCPecho(host, service);

/*------------------------------------------------------------------------ * TCPecho - send input to ECHO service on specified host and print reply *------------------------------------------------------------------------ */int
TCPecho(constchar *host, constchar *service)
    char    buf[LINELEN+1];        /* buffer for one line of text    */int    s, n;            /* socket descriptor, read count*/int    outchars, inchars;    /* characters sent and received    */

    s = connectTCP(host, service);

    while (fgets(buf, sizeof(buf), stdin)) {
        buf[LINELEN] = '\0';    /* insure line null-terminated    */
        outchars = strlen(buf);
        (void) write(s, buf, outchars);

        /* read it back */for (inchars = 0; inchars < outchars; inchars+=n ) {
            n = read(s, &buf[inchars], outchars - inchars);
            if (n < 0)
                errexit("socket read failed: %s\n",
        fputs(buf, stdout);

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

Milind Mishra
Milind Mishra author of TCP/IP program of TCP client for ECHO 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!