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

A Palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g., "madam" or "nurses run".

TCP Client-Server Program

Client and server configuration in which a client connects transmits a string to the server, and the server displays the original string and sends a confirmation to the client through socket connection whether the string is a palindrome or not.

Input − WOW

Output − Palindrome

Input − soap

Output − Not Palindrome

How Does It Work?

  • First, establish a client-server connection.

  • After the connection is established, the client utilizes the send system function to deliver the user input string to the server.

  • The server will wait for a string supplied by the client on the client-side.

  • The reading system call is used by the server to read the string.

  • After that, the server determines whether or not the string is a palindrome and returns the confirmation to the client.


  • Run the server application as a GCC server first.

server./server c –o
  • Run the GCC client program on a different terminal.

client./client c -o
  • The client's string is being awaited by the server software.

  • Client-side, enter the string.

  • The original string will be printed by the server software.

  • The result will be printed by the client software.

TCP Server

#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
   struct sockaddr_in client, server;
   int s1, n, sock, g, j, left, right, flag;
   char b11[20], b2[10], b3[10], b4[10];

   s1 = socket(AF_INET, SOCK_STREAM, 0);
   server.sin_family = AF_INET;
   server.sin_port = 20000;
   server.sin_addr.s_addr = inet_addr("");
   bind(s1, (struct sockaddr*)&server, sizeof server);
   listen(s1, 1);
   n = sizeof client;
   sock = accept(s1, (struct sockaddr*)&client, &n);
   for (;;) {
      recv(sock, b11, sizeof(b1), 0);

The string received is:%s
", b11);       if (strlen(b11) == 0)          flag = 1;       else {          left = 0;          right = strlen(b11) - 1;          flag = 1;          while (left < right && flag) {             if (b11[left] != b11[right])                flag = 0;                else {                   left++;                   right--;             }          }       }       send(sock, &flag, sizeof(int), 0);       break;    }    close(sock);    close(s1); }


#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
   struct sockaddr_in client;
   int s, flag;
   char buffer[20];
   s = socket(AF_NET, SOCK_STREAM, 0);
   client.sin_family = AF_NET;
   client.sin_port = 20000;
   client.sin_addr.s_addr = inet_addr("");
   connect(s, (struct sockaddr*)&client, sizeof client);
   for (;;) {
Enter a string to check palindrome: ");       scanf("%s", buffer);       printf("
Client: %s", buffer);       send(s, buffer, sizeof(buffer), 0);       recv(s, &flag, sizeof(int), 0);       if (flag == 1) {          printf("
Server: The string is Palindrome.
");          break;       }       else {          printf("
Server: The string isn't a palindrome.
");          break;       }    }    close(s); }