In this article I will show you how to write a simple TCP echo server.

The echo server is the simpliest server you can develop, it waits for data from a client and then sends the same data to the client.

/*+————————————————————–+
  |         tcp.server.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.server tcp.server.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 port number                             |
  | Description   :  The simple TCP echo server                  |
  +————————————————————–+*/
int main(int argc, char **argv)
{
    int                 serv_fd, clnt_fd;
    struct sockaddr_in  serv_addr;
    char                c;

 

First of all I check for the command line arguments, the echo server shall know the port on which it must wait for a connection. Normally on Unix systems port from 0 to 2048 are reserved so use a port number greater than 2048.

    if(argc!=2)

    {

        Usage(argv);
        exit(EXIT_FAILURE);
    }

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

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

Preparing the server address where I want to receive data. 

    /* prepare for bind*/
    serv_addr.sin_family       =  AF_INET;
    serv_addr.sin_port         =  htons(atoi(argv[1]));
    serv_addr.sin_addr.s_addr  =  INADDR_ANY;

Binding the address

    /* bind*/
    bind(serv_fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
    /* listen only 1 queue*/

Prepare for listening, only 1 client per time. If you want to accept more client you shall change the 2nd parameter of the listen() call.

    listen(serv_fd, 1);
    /* accept only 1 client*/

Waiting for a connection

    clnt_fd = accept(serv_fd, NULL, NULL);

The following lines will echo data received from the client to the client itself.

    /* read and write until ^D (from client)*/

    while (read(clnt_fd, &c, 1))   /* read from client*/
    {
        write(clnt_fd, &c, 1);       /* write to client*/
    }

Release the sockets and exit.


    /* close connection from client and stop echo server*/
    close(clnt_fd);
    close(serv_fd);
    return 0;
}

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

In a next article the client.

Gg1