Reverse a string in C/C++ using Client Server model
Last Updated :
26 Jan, 2018
This article describes a Client and Server setup where a Client connects, sends a string to server and the server shows the original string and sends reversed string to client using socket connection.
Prerequisite : Socket Programming
Examples:
Input : welcome
Output :emoclew
Input :geeks for geeks
Output :skeeg rof skeeg
Explanation
In this, first setup client-server connection. When connection will setup, client will send user input string to server by send system call. At server side, server will wait for string sent by client. Server read string by read system call. After this, server will reverse the string and send back to client.
Compiling :
1. First run the server program as
gcc Server.c -o server
2. Run the client program on another terminal
gcc Client.c -o client
3. Server program is waiting for string sent by client.
4. Input the string in client side.
5. Server program will print original string.
6. Client program will print reversed string.
Client.c
C
// C client code to send string to reverse
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#define PORT 8090
// Driver code
int main()
{
struct sockaddr_in address;
int sock = 0, valread;
struct sockaddr_in serv_addr;
char str[100];
printf("\nInput the string:");
scanf("%[^\n]s", str);
char buffer[1024] = { 0 };
// Creating socket file descriptor
if ((sock = socket(AF_INET,
SOCK_STREAM, 0))
< 0) {
printf("\n Socket creation error \n");
return -1;
}
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IPv4 and IPv6 addresses from
// text to binary form 127.0.0.1 is local
// host IP address, this address should be
// your system local host IP address
if (inet_pton(AF_INET, "127.0.0.1",
&serv_addr.sin_addr)
<= 0) {
printf("\nAddress not supported \n");
return -1;
}
// connect the socket
if (connect(sock, (struct sockaddr*)&serv_addr,
sizeof(serv_addr))
< 0) {
printf("\nConnection Failed \n");
return -1;
}
int l = strlen(str);
// send string to server side
send(sock, str, sizeof(str), 0);
// read string sent by server
valread = read(sock, str, l);
printf("%s\n", str);
return 0;
}
Server.c
C
// Server C code to reverse a
// string by sent from client
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#define PORT 8090
// Driver code
int main()
{
int server_fd, new_socket, valread;
struct sockaddr_in address;
char str[100];
int addrlen = sizeof(address);
char buffer[1024] = { 0 };
char* hello = "Hello from server";
// Creating socket file descriptor
if ((server_fd = socket(AF_INET,
SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
// Forcefully attaching socket to
// the port 8090
if (bind(server_fd, (struct sockaddr*)&address,
sizeof(address)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
// puts the server socket in passive mode
if (listen(server_fd, 3) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
if ((new_socket = accept(server_fd,
(struct sockaddr*)&address,
(socklen_t*)&addrlen)) < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
// read string send by client
valread = read(new_socket, str,
sizeof(str));
int i, j, temp;
int l = strlen(str);
printf("\nString sent by client:%s\n", str);
// loop to reverse the string
for (i = 0, j = l - 1; i < j; i++, j--) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
// send reversed string to client
// by send system call
send(new_socket, str, sizeof(str), 0);
printf("\nModified string sent to client\n");
return 0;
}
Similar Reads
Program to Reverse a String using Pointers Given a string, the task is to reverse this String using pointers. Examples:Input: GeeksOutput: skeeGInput: GeeksForGeeksOutput: skeeGroFskeeGApproach: This method involves taking two pointers, one that points at the start of the string and the other at the end of the string. The characters are then
2 min read
Reverse a String â Complete Tutorial Given a string s, the task is to reverse the string. Reversing a string means rearranging the characters such that the first character becomes the last, the second character becomes second last and so on.Examples:Input: s = "GeeksforGeeks"Output: "skeeGrofskeeG"Explanation : The first character G mo
13 min read
Reverse each word in a linked list node Given a linked list of strings, we need to reverse each word of the string in the given linked list. Examples: Input: geeksforgeeks a computer science portal for geeks Output: skeegrofskeeg a retupmoc ecneics latrop rof skeeg Input: Publish your own articles on geeksforgeeks Output: hsilbuP ruoy nwo
6 min read
TCP Server-Client implementation in C Prerequisites - Socket Programming in C/C++, TCP and UDP server using select, UDP Server-Client implementation in C If we are creating a connection between client and server using TCP then it has a few functionalities like, TCP is suited for applications that require high reliability, and transmissi
4 min read
<strings> library in C++ STL Member functions String.constructor : Construct string object (public member function ).String.destructor : String destructor (public member function )String.operator= : String assignment (public member function ) Iterators Begin : Return iterator to beginning (public member function )End : Return i
3 min read
PHP Reverse a String Reversing a string in PHP refers to rearranging a given string's characters in reverse order, starting from the last character to the first. This task is often used in text manipulation or algorithm challenges, highlighting PHP's string-handling capabilities.Examples: Input : GeeksforGeeksOutput : s
3 min read