Question
Write comments to eachline of code #include #include #include #include #include #define FIFO_FILE_1 /tmp/client_to_server_fifo #define FIFO_FILE_2 /tmp/server_to_client_fifo int main() { int client_to_server; int server_to_client; char
Write comments to eachline of code
#include
#define FIFO_FILE_1 "/tmp/client_to_server_fifo" #define FIFO_FILE_2 "/tmp/server_to_client_fifo"
int main() { int client_to_server; int server_to_client; char buf[BUFSIZ];
/* create the FIFO (named pipe) */ mkfifo(FIFO_FILE_1, 0666); mkfifo(FIFO_FILE_2, 0666);
printf("Server ON. ");
while (1) { /* open, read, and display the message from the FIFO */ client_to_server = open(FIFO_FILE_1, O_RDONLY); server_to_client = open(FIFO_FILE_2, O_WRONLY);
read(client_to_server, buf, BUFSIZ);
if (strcmp("$",buf)==0) { printf("Server OFF. "); break; }
else if (strcmp("",buf)!=0) { printf("Received: %s ", buf); printf("Sending back... "); write(server_to_client,buf,BUFSIZ); }
/* clean buf from any data */ memset(buf, 0, sizeof(buf));
}
close(client_to_server); close(server_to_client);
unlink(FIFO_FILE_1); unlink(FIFO_FILE_2); return 0; } client
#include
#define FIFO_FILE_1 "/tmp/client_to_server_fifo" #define FIFO_FILE_2 "/tmp/server_to_client_fifo"
int main() { system("clear"); int client_to_server; int server_to_client;
char str[140];
printf("Input message to server: "); scanf("%139[^ ]", str);
/* write str to the FIFO */ client_to_server = open(FIFO_FILE_1, O_WRONLY); server_to_client = open(FIFO_FILE_2, O_RDONLY);
if(write(client_to_server, str, sizeof(str)) < 0){ perror("Write:");//print error exit(-1); } if(read(server_to_client,str,sizeof(str)) < 0){ perror("Read:"); //error check exit(-1); } printf(" ...received from the server: %s ",str);
close(client_to_server); close(server_to_client);
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started