OBJECTIVES

To develop a client/server application using TCP sockets and the C programming language that is capable of supporting multiple concurrent service requests from different clients.

PROBLEM

Using the Ubuntu operating system as well as both the client and the server programs provided below, you are to modify your server program to process requests from more than one client concurrently. This means different clients may request either the same service or a totally different one. The services supported by your server will include:

(1) changing all characters of a text file to uppercase, and
(2) counting the number of a supplied character in a text file.

Your client program will forward service requests to the server stating (1) the kind of service is needed, and (2) all required data necessary for the successful completion of the request. Your client program must use the following syntax for any request to the server where toUpper is the request to change characters to uppercase, and count to return how many times the supplied character is present in the file.

Your-Prompt> toUpper < file.txt >
Your-Prompt> count < char, file.txt >

In the above two examples file.txt is to indicate that a text file is to be suppled as an input parameter. The char string is to inform that a character is to be supplied. The information returned to the client, by your server, must be stored in text files with the names fileUpper.txt and fileChar.txt.

HOW TO TEST YOUR SOLUTION

You are to populate your file.txt with following data and use the blank ' character for testing your solution.

source code
represents the part of process that
contains the programming
language itself. you may
use a text editor to
write your source code file.
/* Program: server.c
* A simple server TCP sockets.
* Server is executed before Client.
* Port number is to be passed as an argument.
*
* To test: Open a terminal window.
* At the prompt ($ is my prompt symbol) you may
* type the following as a test:
*
* $ ./server 54554
* Run client by providing host and port
*
*
*/
#include < arpa/inet.h>
#include < stdio.h>
#include < stdlib.h>
#include < string.h>
#include < unistd.h>
#include < sys/types.h>
#include < sys/socket.h>
#include < netinet/in.h>

void error(const char *msg)
{
perror(msg);
exit(1);
}

int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno;
socklen_t clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;

if (argc < 2) {
fprintf(stderr,"ERROR, no port providedn");
exit(1);
}
fprintf(stdout, "Run client by providing host and portn");
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr,
&clilen);
if (newsockfd < 0)
error("ERROR on accept");
bzero(buffer,256);
n = read(newsockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
printf("Here is the message: %sn",buffer);
n = write(newsockfd,"I got your message",18);
if (n < 0)
error("ERROR writing to socket");
close(newsockfd);
close(sockfd);
return 0;
}
/*
simple client to work with server.c program.
* Host name and port used by server is to be
* passed as arguments.
*
* To test: Open a terminal window.
* At prompt ($ is my prompt symbol) you may
* type the following as a test:
*
* $./client 127.0.0.1 54554
* Please enter the message: Operating Systems is fun!
* I got your message
* $
*
*/
#include < stdio.h>
#include < stdlib.h>
#include < unistd.h>
#include < string.h>
#include < sys/types.h>
#include < sys/socket.h>
#include < netinet/in.h>
#include < netdb.h>

void error(const char *msg)
{
perror(msg);
exit(0);
}

int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;

char buffer[256];
if (argc < 3) {
fprintf(stderr,"usage %s hostname portn", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such hostn");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
printf("Please enter the message: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer,256);
n = read(sockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
printf("%sn",buffer);
close(sockfd);
return 0;
}
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.