In this article I'll show you the code for the tcp client for the echo server.

/*+————————————————————–+
  |         tcp.client.c  –  description                         |
  |                ——————-                           |
  | begin      : 05/08/2010 20.00                                |
  | copyright  : (C) 2010 xAppSoftware                           |
  | author     : Luigi D'Andrea                                  |
  | email      : gg1 ( at ) xappsoftware dot com                 |
  | compiling  : gcc -o tcp.client tcp.client.c                  |
  |                                                              |
  | Latest version on http://www.xappsoftware.com                |
  +————————————————————–+
  | udp client-server may be redistributed and modified under    |
  | certain conditions. This software is distributed on an       |
  | "AS IS" basis WITHOUT WARRANTY OF ANY KIND, either express or|
  | implied.  See the file License.txt for details.              |
  +————————————————————–+*/

/*+————————————————————–+
  | SYSTEM INCLUDES                                              |
  +————————————————————–+*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

/*+————————————————————–+
  | PROTOTYPES                                                   |
  +————————————————————–+*/
int main(int argc, char **argv);
void Usage(char **argv);

 

Blablablabla.

Finally the main function…..

/*+————————————————————–+
  | Function name :  main                                        |
  | Parameters    :  The destination IP address                  |
  |                  The port number                             |
  | Description   :  The simple TCP echo client                  |
  +————————————————————–+*/
int main(int argc, char **argv)
{
    int                 clnt_fd;
    struct sockaddr_in  serv_addr;
    char                c;

 

 

First of all I check for the command line arguments, the client  shall know the port on which it send data and the IP address of the system on which the server is running.

    if(argc!=3)

    {
        Usage(argv);
        exit(EXIT_FAILURE);
    }
 

 

I create a socket, if the socket() call fails then the program exits.


// create socket
    clnt_fd = socket(AF_INET, SOCK_STREAM, 0);
    if(clnt_fd==-1)
    {
        perror("Error while opening socket");
        exit(EXIT_FAILURE);
    }

 

Preparing the server address where I want to send data. 

// prepare for connect

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(atoi(argv[2]));
    serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
 

 

I'm trying to connect to the server. 


// connect to echo server
    connect(clnt_fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
 

 

The following lines read data from the keyboard and after a "return" send them to the server, then the clients waits for the echo. 

// write to echo server

    while ((c = (char) getchar()) != EOF)   // read from keyboard
    {
        write(clnt_fd, &c, 1);                // write to echo server
        read(clnt_fd, &c, 1);                 // read from echo server
        putchar((int) c);                     // write to monitor
    }
 

 

Release the socket and exit.

    close(clnt_fd);

    return 0;
}

void Usage(char **argv)
{
    printf("Usage: %s <dest_ip_address> <port_number>\n", argv[0]);
    return;
}