Open In App

Simple client/server application in C

Last Updated : 30 Jun, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Overview :
Create a simple client/server application in C using the concept of socket programming. Where server send some message to the client when getting connected.

Client :  
In this section, the term ‘Client’ is defined and its socket workflow is explained in detail, through different functions used to implement the client. An application that runs on a personal computer. It has an extensive and appealing user interface, but it has no data that it can manipulate or present to the user. The client application ‘asks’ what a user wants, then connects to the server application to obtain that data. Once the data has been obtained, it is presented to the user in a nice format. A client usually gets the data from one server at a time and interacts with one user only.

Client Workflow :

  • The first part of any socket programming is to create the socket itself.
  • The client socket is created with a socket() call. The socket() function returns an integer. In the socket call, we specify the following parameters
    • Domain: IPv4 (AF_INET)
    • Type of socket: TCP/UDP (SOCK_STREAM)
    • Protocol: IP (0)
  • Connection to a remote address is created with connect() call. Here, we specify the IP address and the port that we are going to connect with. If the connection is successful, a value is returned.
  • The data is retrieved with a recv() call. The received data can be stored in a file, or into a string.
  • Connection to a remote address is created with connect() call. Here, we specify the IP address and the port that we are going to connect with. If the connection is successful, a value is returned.
  • The data is retrieved with a recv() call. The received data can be stored in a file, or into a string.

Code Implementation –

C




#include <netinet/in.h> //structure for storing address information
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h> //for socket APIs
#include <sys/types.h>
  
int main(int argc, char const* argv[])
{
    int sockD = socket(AF_INET, SOCK_STREAM, 0);
  
    struct sockaddr_in servAddr;
  
    servAddr.sin_family = AF_INET;
    servAddr.sin_port
        = htons(9001); // use some unused port number
    servAddr.sin_addr.s_addr = INADDR_ANY;
  
    int connectStatus
        = connect(sockD, (struct sockaddr*)&servAddr,
                  sizeof(servAddr));
  
    if (connectStatus == -1) {
        printf("Error...\n");
    }
  
    else {
        char strData[255];
  
        recv(sockD, strData, sizeof(strData), 0);
  
        printf("Message: %s\n", strData);
    }
  
    return 0;
}


 

Server : 
In this section, the term ‘Server’ is defined and its socket workflow is explained in detail, through different functions used to implement the server. An application runs on a large computer (large meaning either fast or large storage space or both). The server application is usually written in such a way that it does not provide any method to interact with a user. Instead, it waits for other programs to connect. Typically, a server application has control over large amounts of data and can access those data fast and efficiently. It can also handle requests by many clients (more or less) simultaneously.

Server Socket Workflow :

  • First, a socket will be created (similar to the client program).
  • Next, the IP and port of the socket will be bound using the bind() function (client used to connect() function to connect to the server; the server will use the bind() function to listen for the connections).
  • Then listen() function is called to listen to the connections (to see if any client is trying to connect to the server socket).
  • The function accept() is called. The function returns a client socket (the client that has connected).

Code Implementation –

C




#include <netinet/in.h> //structure for storing address information
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h> //for socket APIs
#include <sys/types.h>
  
int main(int argc, char const* argv[])
{
  
    // create server socket similar to what was done in
    // client program
    int servSockD = socket(AF_INET, SOCK_STREAM, 0);
  
    // string store data to send to client
    char serMsg[255] = "Message from the server to the "
                       "client \'Hello Client\' ";
  
    // define server address
    struct sockaddr_in servAddr;
  
    servAddr.sin_family = AF_INET;
    servAddr.sin_port = htons(9001);
    servAddr.sin_addr.s_addr = INADDR_ANY;
  
    // bind socket to the specified IP and port
    bind(servSockD, (struct sockaddr*)&servAddr,
         sizeof(servAddr));
  
    // listen for connections
    listen(servSockD, 1);
  
    // integer to hold client socket.
    int clientSocket = accept(servSockD, NULL, NULL);
  
    // send's messages to client socket
    send(clientSocket, serMsg, sizeof(serMsg), 0);
  
    return 0;
}


Instructions to Execute :

  1. Open two terminals on your machine and compile the server and the client programs in different terminals.
  2. Run the server program first, followed by running the client program.
  3. It can be seen that the data sent by the server is printed on the terminal running the client program

Reference : 
http://pirate.shu.edu/~wachsmut/Teaching/CSAS2214/Virtual/Lectures/lecture17.html



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads