Client server socket programming project in c linux

In: Project Code: PL000009

In this project we will develop a Command line interface (CLI) using IPC client/server socket programming. We will also cause the server program to begin execution during linux startup.

Complete this project and update to consider it for requirements and earn certificate.

course thumb

Project Contents

Online courses related to this project

Course description

Learn to develop software solutions for linux environment, implement requirements through real time projects and get required practical skills for software jobs

Course description

Learn to develop embedded systems, interfacing electronic peripherals through real time projects and get required practical skills for software jobs

Project background

BSD sockets and applications.

Inter process communication(IPC) using internet protocol sockets are one way of communicating between two processes i.e. a client process and a server process in linux. We will use Internet sockets which are also called Berkeley or BSD sockets. BSD sockets can only be used to communicate between two processes on the same host linux machine.

One application of BSD sockets is in command line interface(CLI) development to provide an user interface to an electronic product. The client process sends a command to be executed to the server process using socket. The server process actually executes the command and provides the response back to the client process.

The steps involved in establishing a TCP socket on the server side are as follows:

  • Create a socket with the socket() function
  • Bind the socket to an address using the bind() function
  • Listen for connections with the listen() function
  • Accept a connection with the accept() function system call. This call typically blocks until a client connects with the server.
  • Send and receive data by means of send() and receive().
  • The steps for establishing a TCP socket on the client side are the following:

  • Create a socket using the socket() function
  • Connect the socket to the address of the server using the connect() function
  • Send and receive data by means of the read() and write() functions.
  • Client program development, compilation

    Client program development.
    client.c: client program to take user input command and get response from server.
    #include "stdio.h"
    #include "stdlib.h"
    #include "string.h"
    #include "sys/socket.h"
    #include "arpa/inet.h"
    #include "unistd.h"
    #define CMD_LENGTH 100
    #define SERVER_PORT 8090
    void getCommand(char *);
    short createSocket(void);
    int receiveData(int ,char* ,short );
    int sendSocket(int ,char* ,short );
    int connectSocket(int hSocket);
    int main()
        int hSocket, read_size;
        struct sockaddr_in server;
        char cmd[100];
        char server_response[200];
            hSocket = createSocket();
            if(hSocket == -1)
                printf("Could not create socket\n");
                return 1;
            if (connectSocket(hSocket) < 0)
                perror("connect failed.\n");
                return 1;
            //get command from user
            if(cmd[0] == '\n')
            sendSocket(hSocket, cmd, strlen(cmd)-1);
            read_size = receiveData(hSocket, server_response, 200);
            server_response[read_size] = '\0';
            printf("Server Response : %s\n",server_response); 
            server_response[0] = '\0'; 
        return 0;
    void getCommand(char *name) 
      printf("Enter your command:");
    short createSocket(void)
        short mysocket;
        mysocket = socket(AF_INET, SOCK_STREAM, 0);
        return mysocket;
    int connectSocket(int mysocket)
        int iRetval=-1;
        struct sockaddr_in remote= {0};
        remote.sin_addr.s_addr = inet_addr("");
        remote.sin_family = AF_INET;
        remote.sin_port = htons(SERVER_PORT);
        iRetval = connect(mysocket,(struct sockaddr *)&remote,sizeof(struct sockaddr_in));
        return iRetval;
    int sendSocket(int mysocket,char* Rqst,short lenRqst)
        int shortRetval = -1;
        struct timeval tv;
        tv.tv_sec = 20;
        tv.tv_usec = 0;
        if(setsockopt(mysocket,SOL_SOCKET,SO_SNDTIMEO,(char *)&tv,sizeof(tv)) < 0)
            printf("Time Out\n");
            return -1;
        shortRetval = send(mysocket, Rqst, lenRqst, 0);
        return shortRetval;
    int receiveData(int hSocket,char* Rsp,short RvcSize)
        int shortRetval = -1;
        struct timeval tv;
        tv.tv_sec = 20;
        tv.tv_usec = 0;
        if(setsockopt(hSocket, SOL_SOCKET, SO_RCVTIMEO,(char *)&tv,sizeof(tv)) < 0)
            printf("Time Out\n");
            return -1;
        shortRetval = recv(hSocket, Rsp, RvcSize, 0);
        return shortRetval;

    Let's go through the client code written in client.c. We created a socket using createSocket function. We used connectSocket function to make a connection to the server. We used server port as 8090 in both client and server programs. Client and server runs on same machine having same local host IP address as

    The getCommand function is written to get a command from user through stdin. sendSocket function is used to finally send the user command to the server

    Client program compilation.

    Execute the client program using following command. You should see an executable with name client is created.

              pi@raspberrypi:~/Client $ gcc client.c -o client
              pi@raspberrypi:~/Client $

    Subscribe online courses

    Subscribe below to unlock project and earn certificates for software jobs


    Schedule a project

    Embedded Systems Projects

    Linux Software Projects


     Online Certificate Courses

     Test your skills:online tests

     C Programming

     Python Programming

     Linux Software

     Quantitative Aptitude