Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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, such as "madam", "racecar", or "level". This article demonstrates a TCP client-server program where the client sends a string to the server, and the server checks if it's a palindrome and returns the result.
How It Works
The TCP client-server palindrome checker follows this communication pattern:
-
Connection establishment − The server listens on a specific port, and the client connects to it using TCP sockets.
-
Data transmission − The client uses the
send()system call to transmit the input string to the server. -
Palindrome checking − The server receives the string using
recv()and checks if it reads the same forwards and backwards. -
Result communication − The server sends back a flag indicating whether the string is a palindrome or not.
Example Input/Output
Input: "WOW" ? Output: Palindrome
Input: "soap" ? Output: Not Palindrome
Compilation and Execution
To run this program, compile both server and client files separately:
gcc -o server server.c gcc -o client client.c
Run the server first, then the client in a separate terminal:
./server ./client
TCP Server Code
#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>
main(){
struct sockaddr_in client, server;
int s1, n, sock, left, right, flag;
char buffer[20];
s1 = socket(AF_INET, SOCK_STREAM, 0);
server.sin_family = AF_INET;
server.sin_port = 20000;
server.sin_addr.s_addr = inet_addr("127.0.0.1");
bind(s1, (struct sockaddr*)&server, sizeof server);
listen(s1, 1);
n = sizeof client;
sock = accept(s1, (struct sockaddr*)&client, &n);
for (;;) {
recv(sock, buffer, sizeof(buffer), 0);
printf("\nThe string received is: %s
", buffer);
if (strlen(buffer) == 0)
flag = 1;
else {
left = 0;
right = strlen(buffer) - 1;
flag = 1;
while (left < right && flag) {
if (buffer[left] != buffer[right])
flag = 0;
else {
left++;
right--;
}
}
}
send(sock, &flag, sizeof(int), 0);
break;
}
close(sock);
close(s1);
}
TCP Client Code
#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>
main(){
struct sockaddr_in client;
int s, flag;
char buffer[20];
s = socket(AF_INET, SOCK_STREAM, 0);
client.sin_family = AF_INET;
client.sin_port = 20000;
client.sin_addr.s_addr = inet_addr("127.0.0.1");
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 Palindrome.
");
break;
}
else {
printf("\nServer: The string isn't a palindrome.
");
break;
}
}
close(s);
}
Key Features
-
Socket programming − Uses TCP sockets for reliable client-server communication
-
String comparison − Implements palindrome checking using two-pointer technique
-
Bidirectional communication − Client sends string, server responds with result
Conclusion
This TCP client-server program demonstrates network programming concepts by implementing a palindrome checker. The server receives strings from clients, performs palindrome validation using character comparison, and returns the result over a reliable TCP connection.
