TCP Client-Server Program to Check if a Given String is Palindrome

Prerequisites:

This article describes a Client and Server setup where a Client connects, sends a string to the server and the server shows the original string and sends confirmation whether the string is a palindrome or not, to the client using socket connection.

Examples:



Input: naman
Output: Palindrome

Input: geek
Output: Not Palindrome

Approach:

  • In this, first set up a client-server connection.
  • When the connection will setup, the client will send the user input string to the server by the send system call.
  • At the server-side, the server will wait for a string sent by the client.
  • Server reads the string by the reading system call.
  • After this, the server will check if the string is a palindrome or not and sends the confirmation back to the client.

Compiling:

  1. First, run the server program as
    gcc server.c -o server
    ./server
  2. Run the client program on another terminal
    gcc client.c -o client
    ./client
  3. Server program is waiting for the string sent by the client.
  4. Input the string in client-side.
  5. Server program will print original string.
  6. Client program will print result.

Below is the implementation of the above approach:

TCP Server

filter_none

edit
close

play_arrow

link
brightness_4
code

// defines in_addr structure
#include <arpa/inet.h>
  
// contains constants and structures
// needed for internet domain addresses
#include <netinet/in.h>
  
// standard input and output library
#include <stdio.h>
  
// contains string functions
#include <string.h>
  
// for socket creation
#include <sys/socket.h>
  
// contains constructs that facilitate getting
// information about files attributes.
#include <sys/stat.h>
  
// contains a number of basic derived types
// that should be used whenever appropriate
#include <sys/types.h>
  
main()
{
    struct sockaddr_in client, server;
    int s, n, sock, g, j, left, right, flag;
    char b1[20], b2[10], b3[10], b4[10];
  
    // creating socket
    s = socket(AF_INET, SOCK_STREAM, 0);
  
    // assign IP, PORT
    server.sin_family = AF_INET;
  
    // this is the port number of running server
    server.sin_port = 2000;
    server.sin_addr.s_addr = inet_addr("127.0.0.1");
  
    // Binding newly created socket
    // to given IP and verification
    bind(s, (struct sockaddr*)&server, sizeof server);
    listen(s, 1);
    n = sizeof client;
  
    sock = accept(s, (struct sockaddr*)&client, &n);
    for (;;) {
        recv(sock, b1, sizeof(b1), 0);
  
        // whenever a request from a client came.
        // It will be processed here.
        printf("\nThe string received is:%s\n", b1);
        if (strlen(b1) == 0)
            flag = 1;
        else {
            left = 0;
            right = strlen(b1) - 1;
            flag = 1;
            while (left < right && flag) {
                if (b1[left] != b1[right])
                    flag = 0;
                else {
                    left++;
                    right--;
                }
            }
        }
        send(sock, &flag, sizeof(int), 0);
        break;
    }
    close(sock);
  
    // close the socket
    close(s);
}

chevron_right


TCP Client

filter_none

edit
close

play_arrow

link
brightness_4
code

// defines in_addr structure
#include <arpa/inet.h>
  
// contains constants and structures
// needed for internet domain addresses
#include <netinet/in.h>
  
// standard input and output library
#include <stdio.h>
  
// contains string functions
#include <string.h>
  
// for socket creation
#include <sys/socket.h>
  
// contains constructs that facilitate getting
// information about files attributes.
#include <sys/stat.h>
  
// contains a number of basic derived types
// that should be used whenever appropriate
#include <sys/types.h>
  
main()
{
    struct sockaddr_in client;
    int s, flag;
    char buffer[20];
  
    // socket create
    s = socket(AF_INET, SOCK_STREAM, 0);
  
    // assign IP, PORT
    client.sin_family = AF_INET;
    client.sin_port = 2000;
    client.sin_addr.s_addr = inet_addr("127.0.0.1");
  
    // connect the client socket to server socket
    connect(s, (struct sockaddr*)&client, sizeof client);
  
    for (;;) {
        printf("\nEnter a string to check palindrome: ");
        scanf("%s", buffer);
  
        printf("\nClient: %s", buffer);
        send(s, buffer, sizeof(buffer), 0);
        recv(s, &flag, sizeof(int), 0);
  
        if (flag == 1) {
            printf("\nServer: The string is a Palindrome.\n");
            break;
        }
        else {
            printf("\nServer: The string is not a palindrome.\n");
            break;
        }
    }
  
    // close the socket
    close(s);
}

chevron_right



Output:

  • Server Side:

  • Client Side:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.