Create a C program that uses a server program and a client program. The client p
ID: 3754649 • Letter: C
Question
Create a C program that uses a server program and a client program. The client program reads a line from a text file, converts everything to upper case letters, and send it to the server, using a named pipe. It pauses for 1 second, and repeats the above process, until the end of file.
The client sends a "Stop" string at the end.
The server displays any thing it receives. The server quits when it sees "Stop". The server needs to cleanup by deleting the named pipe.
Your programs (client and server) need to create the name pipe first, if it does not exist.
Explanation / Answer
Client File:
#include <stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
#include <stdlib.h>
int main(void) {
char str[1000];
FILE *fptr;
if ((fptr = fopen("myPIPE.txt", "r")) == NULL)
{
printf("file does not exist");
}
// Name PIPE (FIFO) file path
char * myPIPE = "/dikshita/desktop/myfifo";
// Creating the named PIPE file(FIFO) if it does not exist
// mkfifo(<pathname>, <permission>)
mkfifo(myPIPE, 0444);
fptr = open(myPIPE, O_WRONLY);
While(fgets(str, 1000, fptr) != NULL)
{
// Converting to uppercase
printf("The string in upper case: %s ", strupr(str));
// pause for 1 second
delay(1000);
}
Printf("Stop");
fclose(fptr);
}
Server File:
#include <stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
#include <stdlib.h>
int main(void) {
FILE *fread;
char * myPIPE = "/dikshita/desktop/myfifo";
char str2[1000];
fread = open(myPIPE,O_RDONLY);
read(fread, str2, 1000);
// Display the text
printf("%s ", str2);
if(fscanf(fread,"%[^ ]", "STOP"))
{
exit(1);
}
remove(myPIPE);
remove("myPIPE.bin");
}
Output:
File myPIPE with port permission 0444
Hi
Hello
(1) Run client.c
The string in upper case: HI
The string in upper case:HELLO
(2) Run server.c
HI
HELLO