Problem: Implement a FIFO program in which a client sends the server 3 variable
ID: 3797967 • Letter: P
Question
Problem: Implement a FIFO program in which a client sends the server 3 variable names (strings). A valid variable name is defined for this assignment to be 6 characters or less, consisting only of lower-case letters from a to 'z', inclusive. The server checks the name for validity, and if valid, it counts the number of in the variable name. Each of the original variable names is sent back to the client, along with a message whether it is valid or invalid, and a count of the number of letters in the indicating name that are vowels. Vowels, for those of you who might have forgotten, are a, e, i, o and u Print the variable name, validity, and count of vowels on the server side as well as on the client side after it has been received.
Please take a look and correct what I've got below
/*------------------------------- Client -----------------------------------*/
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
main (void)
{
int fda;
int fdb;
struct program
{
char string1;
int good1;
int count1;
int status;
} data;
int rbyte, wbyte;
rbyte = 0;
wbyte = 0;
//Open Pathway to FIFO
if((fda = open("FIFOTOS",O_WRONLY)) < 0){
printf("Can't open FIFOTOS to write ");
return;
}
if((fdb = open("FIFOFROMS",O_RDONLY)) < 0){
printf("Can't open FIFOFROMS to read ");
return;
}
//Get input from user
printf("Client: Please enter first string: ");
scanf("%s",&data.string1);
//Write data to FIFO
wbyte = write(fda,&data,sizeof(data));
//Check if write successfully
if (wbyte <= 0)
{
printf("Client: Can't write data to server");
}
else
{
printf("Client: Write &d byte to server ");
//Read data from server
rbyte = read(fdb,&data,sizeof(data));
if(rbyte <=0)
{
printf("Client: Can't read from server ");
}
else
{
printf("Client: %d byte read from server ",rbyte);
if(data.status >=0)
{
printf("String : %s",data.string1);
printf(" Validity : %c",(data.good1 =1 ? 'Y' : 'N'));
printf(" Count of Vowels : %d",data.count1);
}
else
{
printf("Client: Error");
}
}
}
printf("All Done ");
close(fda);
close(fdb);
}
/*------------------------------------------ Server ------------------------------------------*/
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
main (void)
{
int fda;
int fdb;
int i;
struct program
{
char string1;
int good1;
int count1;
int status;
} data;
int rbyte, wbyte;
rbyte = 0;
wbyte = 0;
//Create the FIFO and open pathway
if((mkfifo("FIFOTOS",0666) < 0 && errno != EEXIST)){
perror("Can't create FIFOTOS");
exit(-1);
}
if((mkfifo("FIFOFROMS",0666) < 0 && errno != EEXIST)){
perror("Can't create FIFOFROMS");
exit(-1);
}
if((fdb = open("FIFOTOS",O_RDONLY)) < 0){
printf("Can't open FIFOTOS to write ");
return;
}
if((fda = open("FIFOFROMS",O_WRONLY)) < 0){
printf("Can't open FIFOFROMS to read ");
return;
}
//Read data from client
rbyte = read(fdb, &data, sizeof(data));
//Check if read successfully
if (rbyte <= 0)
{
printf("No byte read from Client ");
}
else
{
data.status = 1;
// Check validity of string 1
for (i=0; i < strlen(data.string1); i++)
{
if (i<=6);
{
if (string1[i] >= 'a' && string1[i] <= 'z')
{
data.good1 = 1;
}
}
}
// Count vowel of string 1
if(data.good1 = 1)
{
for(i=0; string1[i] != ''; i++)
{
if(string1[i] == 'a' || string1[i] == 'i' || string1[i] == 'o' ||string1[i] == 'e' || string1[i] == 'u' )
{
data.count1++;
}
}
}
//Send data to client
wbyte = write(fda, &data, sizeof(data));
if (wbyte <= 0)
{
printf("Server: Can't write to client ");
}
else
{
printf("Server: %d byte write to client ",wbyte);
}
//Print out result in server
printf("String : %s",data.string1);
printf(" Validity : %c",(data.good1 =1 ? 'Y' : 'N'));
printf(" Count of Vowels : %d",data.count1);
}
printf("All Done ");
close(fda);
close(fdb);
unlink("FIFOFROMS");
unlink("FIFOTOS");
}
Explanation / Answer
//After making the changes:
Server.c
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
main (void)
{
int fda;
int fdb;
int i;
/* struct program
{
char * string1;
int good1;
int count1;
int status;
} data;
*/
int rbyte, wbyte;
rbyte = 0;
wbyte = 0;
int count=0;
int good=0;
int status=0;
//data.status=0;
//data.count1=0;
//data.good1=0;
char buf[1024];
//Create the FIFO and open pathway
if((mkfifo("FIFOTOS",0666) < 0 && errno != EEXIST))
{
perror("Can't create FIFOTOS");
exit(-1);
}
if((mkfifo("FIFOFROMS",0656) < 0 && errno != EEXIST))
{
perror("Can't create FIFOFROMS");
exit(-1);
}
if((fdb = open("FIFOTOS",O_RDONLY)) < 0)
{
printf("Can't open FIFOTOS to write ");
return;
}
if((fda = open("FIFOFROMS",O_WRONLY)) < 0)
{
printf("Can't open FIFOFROMS to read ");
return;
}
//Read data from client
rbyte = read(fdb, buf, 1024);
printf("%s ",buf);
//rbyte =read(fdb,&data,sizeof(data));
//Check if read successfully
if (rbyte <= 0)
{
printf("No byte read from Client ");
}
else
{
// data.status = 1;
// Check validity of string 1
// char * str=data.string1;
char *str=buf;
if(strlen(str)<=6)
{
status=1;
for (i=0; i < strlen(str); i++)
{
if (str[i] >= 'a' && str[i] <= 'z')
{
good=1;
}
else
{
good=0;
break;
}
}
// Count vowel of string 1
//if(data.good1 == 1)
if(good==1)
{
for(i=0; str[i] != ''; i++)
{
if(str[i] == 'a' || str[i] == 'i' || str[i] == 'o' ||str[i] == 'e' || str[i] == 'u' )
count++;
}
}
}
else
{
status=0;
good=0;
}
//Send data to client
char *ans;
if(good==1)
{
ans="Valid String Count of vowels :"+(count+48);
}
else
ans="Invalid String ";
wbyte=write(fdb,ans,1024);
if (wbyte <= 0)
{
printf("Server: Can't write to client ");
}
else
{
printf("Server: %d byte write to client ",wbyte);
}
//Print out result in server
printf("String : %s",str);//data.string1);
printf(" Validity : %c",((good==1) ? 'Y' : 'N'));
printf(" Count of Vowels : %d ",count);
}
printf("All Done ");
close(fda);
close(fdb);
unlink("FIFOFROMS");
unlink("FIFOTOS");
}
Client.c
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
void main (void)
{
int fda;
int fdb;
int rbyte, wbyte;
rbyte = 0;
wbyte = 0;
//Open Pathway to FIFO
if((fda = open("FIFOTOS",O_WRONLY)) < 0)
{
printf("Can't open FIFOTOS to write ");
return;
}
if((fdb = open("FIFOFROMS",O_RDONLY)) < 0)
{
printf("Can't open FIFOFROMS to read ");
return;
}
//Get input from user
printf("Client: Please enter first string: ");
char str[1024];
scanf("%s",str);
printf("%s ",str );
//Write data to FIFO
wbyte = write(fda,str,sizeof(str));
if (wbyte <= 0)
{
printf("Client: Can't write data to server");
}
else
{
printf("Client: Write %d byte to server ",wbyte);
//Read data from server
char ans[1024];
rbyte=read(fdb,ans,1024);
//rbyte = read(fdb,&data,sizeof(data));
if(rbyte <=0)
{
printf("Client: Can't read from server ");
}
else
{
printf("Client: %d byte read from server ",rbyte);
printf("String : %s ",str);
printf("%s ",ans );
}
}
printf("All Done ");
close(fda);
close(fdb);
}
Output :
~/codes/schegg$ gcc server.c -o ser.o
~/codes/schegg$ gcc client.c -o cl.o
~/codes/schegg$ ./ser.o
Server: 8 byte write to client
String : agsra
Validity : Y
Count of Vowels : 2
All Done
~/codes/schegg$
Client side output :
agar@sagar-Lenovo-G580:~/codes/schegg$ ./cl.o
Client: Please enter first string: sagar
sagar
Client: Write 1024 byte to server
Client: 8 byte read from server
String : agsra
Server: Valid String Count of vowels: 2
All Done
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.