Client server socket programming project in c linux

In: Project Code: PL000009
Advertisements
Share

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.

course thumb

Project Contents

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];
        while(1)
        {  
            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
            getCommand(cmd);
            if(cmd[0] == '\n')
            {
                break;
            }
            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'; 
            close(hSocket);
        }
        shutdown(hSocket,0);
        shutdown(hSocket,1);
        shutdown(hSocket,2);
        return 0;
    }
    void getCommand(char *name) 
    {
      printf("Enter your command:");
      fgets(name,CMD_LENGTH,stdin);
    }
    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("127.0.0.1");
        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 127.0.0.1.

    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 $
            

    To Read Full Article

    Login or Register to create a free Mbed Technologies account

    Register Login
    Share