Problem Implement a FIFO program in which a client sends the
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
Provide code in C and 2 files. Server and client files in C programming.
Solution
Note : To execute this program first run the server program from the command line or terminal, then to run client program open new window in the terminal and complime client program , to excute type ./a.out localhost 8888
====================================server program========================================
 #include<stdio.h>
 #include<string.h>
 #include<sys/socket.h>
 #include<arpa/inet.h>
 #include<unistd.h>
 
 int main(int argc , char *argv[])
 {
 int socket_desc , client_sock , c , read_size,i,flag,count=0;
 struct sockaddr_in server , client;
 char client_message[2000];
 char flag_send[4],count_vowels[4];
 
 //Create socket
 socket_desc = socket(AF_INET , SOCK_STREAM , 0);
 if (socket_desc == -1)
 {
 printf(\"Could not create socket\");
 }
 puts(\"Socket created\");
 
 //Prepare the sockaddr_in structure
 server.sin_family = AF_INET;
 server.sin_addr.s_addr = INADDR_ANY;
 server.sin_port = htons( 8888 );
 
 //Bind
 if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
 {
 //print the error message
 perror(\"bind failed. Error\");
 return 1;
 }
 puts(\"bind done\");
 
 //Listen
 listen(socket_desc , 3);
 
 //Accept and incoming connection
 puts(\"Waiting for incoming connections...\");
 c = sizeof(struct sockaddr_in);
 
 //accept connection from an incoming client
 client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
 if (client_sock < 0)
 {
 perror(\"accept failed\");
 return 1;
 }
 puts(\"Connection accepted\");
 
 //Receive a message from client
 while( (read_size = recv(client_sock , client_message , 2000 , 0)) > 0 )
 {
 //Send the message back to client
 count=0;
 if(strlen(client_message)<=6) //checking if the message size is less than 6 or not
 {
 for ( i = 0; i < strlen(client_message); ++i)
 {
 /* code */
if(!(client_message[i]>=97 && client_message[i]<=122)) // checking whether the message has only small letters
 {
 flag =1;
 }
 }
 
 {
 for (i = 0; i < strlen(client_message); ++i) // counting number of vowels in the word
 {
 if(client_message[i]==\'a\'||client_message[i]==\'e\'||client_message[i]==\'i\'||client_message[i]==\'o\'||client_message[i]==\'u\')
  {
 count++;
 }
 }
 }
 printf(\"The variable you entered is %s\",client_message);
 if(flag!=1)
 {
 printf(\"it is a valid variable\");   
 printf(\"It has %d no of vowels in it\",count);
 write(client_sock , client_message , strlen(client_message));
 *((int*)count_vowels) = count; // converting integer variables to char and then sending
 write(client_sock,count_vowels,4);
 *((int*)flag_send) = flag_send;   
 write(client_sock,flag_send,4);
 }
 else
 {
 printf(\"it is not a valid variable\");
 }
 
 
 }
 else
 {
}
 }
 
 if(read_size == 0)
 {
 puts(\"Client disconnected\");
 fflush(stdout);
 }
 else if(read_size == -1)
 {
 perror(\"recv failed\");
 }
 
 return 0;
 }
====================================client program========================================
 #include<stdio.h>
 #include<string.h>
 #include<sys/socket.h>
 #include<arpa/inet.h>
 
 int main(int argc , char *argv[])
 {
 int sock;
 struct sockaddr_in server;
 char message[1000] , server_reply[2000];
 char status_flag[4],count[4];
 
 //Create socket
 sock = socket(AF_INET , SOCK_STREAM , 0);
 if (sock == -1)
 {
 printf(\"Could not create socket\");
 }
 puts(\"Socket created\");
 
 server.sin_addr.s_addr = inet_addr(\"127.0.0.1\");
 server.sin_family = AF_INET;
 server.sin_port = htons( 8888 );
 
 //Connect to remote server
 if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
 {
 perror(\"connect failed. Error\");
 return 1;
 }
 
 puts(\"Connected\ \");
 
 //keep communicating with server
 while(1)
 {
 printf(\"Enter message : \");
 scanf(\"%s\" , message);
 
 //Send some data
 if( send(sock , message , strlen(message) , 0) < 0)
 {
 puts(\"Send failed\");
 return 1;
 }
 
 //Receive a reply from the server
 if( recv(sock , server_reply , 2000 , 0) < 0)
 {
 puts(\"recv failed\");
 break;
 }
 recv( sock, status_flag, 4, 0 );   
 puts(\"Server reply :\");
 puts(server_reply);
 recv( sock, count, 4, 0 ); // Accepting data from server
 puts(\"Server reply :\");
 printf(\"The variable you entered is: %s\ \",server_reply);
 printf(\"The word is %s\ \",status_flag);
 printf(\"No of vowels in the word is %s\ \",count);
}
 
 close(sock);
 return 0;
 }




