I need to modify the client and server code so, that the reading and writing wil
ID: 3817475 • Letter: I
Question
I need to modify the client and server code so, that the reading and writing will be using duplex pipe.
/* The client process
*/
#define _GNU_SOURCE
#include "local.h"
int
main( ){
int n, privatefifo, publicfifo;
static char buffer[PIPE_BUF];
struct message msg;
sprintf(msg.fifo_name, "/tmp/fifo%d", getpid( ));
if (mknod(msg.fifo_name, S_IFIFO | 0666, 0) < 0) {
perror(msg.fifo_name);
return 1;
}
if ((publicfifo = open(PUBLIC, O_WRONLY)) == -1) {
perror(PUBLIC);
return 2;
}
while ( 1 ) {
write(fileno(stdout), " cmd>", 6);
memset(msg.cmd_line, 0x0, B_SIZ);
n = read(fileno(stdin), msg.cmd_line, B_SIZ);
if (!strncmp("quit", msg.cmd_line, n - 1))
break;
write(publicfifo, (char *) &msg, sizeof(msg));
if ((privatefifo = open(msg.fifo_name, O_RDONLY)) == -1) {
perror(msg.fifo_name);
return 3;
}
while ((n = read(privatefifo, buffer, PIPE_BUF)) > 0) {
write(fileno(stderr), buffer, n);
}
close(privatefifo);
}
close(publicfifo);
unlink(msg.fifo_name);
return 0;
}
/* The server process
*/
#define _GNU_SOURCE
#include "local.h"
int
main( ){
int n, done, dummyfifo, publicfifo, privatefifo;
struct message msg;
FILE *fin;
static char buffer[PIPE_BUF];
mknod(PUBLIC, S_IFIFO | 0666, 0);
if ((publicfifo = open(PUBLIC, O_RDONLY)) == -1 ||
(dummyfifo = open(PUBLIC, O_WRONLY | O_NDELAY)) == -1 ) {
perror(PUBLIC);
return 1;
}
while (read(publicfifo, (char *) &msg, sizeof(msg)) > 0) {
n = done = 0;
do {
if ((privatefifo=open(msg.fifo_name,
O_WRONLY|O_NDELAY)) == -1)
sleep(3);
else {
fin = popen(msg.cmd_line, "r");
write(privatefifo, " ", 1);
while ((n = read(fileno(fin), buffer, PIPE_BUF)) > 0) {
write(privatefifo, buffer, n);
memset(buffer, 0x0, PIPE_BUF);
}
pclose(fin);
close(privatefifo);
done = 1;
}
} while (++n < 5 && !done);
if (!done) {
write(fileno(stderr),
" NOTE: SERVER ** NEVER ** accessed private FIFO ", 48);
return 2;
}
}
return 0;
}
Explanation / Answer
Hi,
I have modified the code to include the duplex commuincation .
There are two types of communication:-
a)Half Duplex
b)Full Duplex
The code is modified for Half Duplex communication.Below is the modified code
Modified lines of code can be seen in bold.
CODE:-
=======================================================================================
#include <iostream>
using namespace std;
/* The client process
*/
#define _GNU_SOURCE
#include "local.h"
#define HALF_DUPLEX "/tmp/halfduplex"
int
main( ){
int n, privatefifo, publicfifo;
static char buffer[PIPE_BUF];
struct message msg;
sprintf(HALF_DUPLEX, "/tmp/fifo%d", getpid( ));
if (mknod(HALF_DUPLEX, S_IFIFO | 0666, 0) < 0) {
perror(HALF_DUPLEX);
return 1;
}
if ((publicfifo = open(HALF_DUPLEX, O_WRONLY)) == -1) {
perror(HALF_DUPLEX);
return 2;
}
while ( 1 ) {
write(fileno(stdout), " cmd>", 6);
memset(msg.cmd_line, 0x0, B_SIZ);
n = read(fileno(stdin), msg.cmd_line, B_SIZ);
if (!strncmp("quit", msg.cmd_line, n - 1))
break;
write(publicfifo, (char *) &msg, sizeof(msg));
if ((privatefifo = open(HALF_DUPLEX, O_RDONLY)) == -1) {
perror(HALF_DUPLEX);
return 3;
}
while ((n = read(privatefifo, buffer, PIPE_BUF)) > 0) {
write(fileno(stderr), buffer, n);
}
close(privatefifo);
}
close(publicfifo);
unlink(HALF_DUPLEX);
return 0;
}
/* The server process
*/
#define _GNU_SOURCE
#include "local.h"
#define HALF_DUPLEX "/tmp/halfduplex"
int
main( ){
int n, done, dummyfifo, publicfifo, privatefifo;
struct message msg;
FILE *fin;
static char buffer[PIPE_BUF];
mknod(HALF_DUPLEX, S_IFIFO | 0666, 0);
if ((publicfifo = open(HALF_DUPLEX, O_RDONLY)) == -1 ||
(dummyfifo = open(HALF_DUPLEX, O_WRONLY | O_NDELAY)) == -1 ) {
perrorHALF_DUPLEX);
return 1;
}
while (read(publicfifo, (char *) &msg, sizeof(msg)) > 0) {
n = done = 0;
do {
if ((privatefifo=open(HALF_DUPLEX,
O_WRONLY|O_NDELAY)) == -1)
sleep(3);
else {
fin = popen(msg.cmd_line, "r");
write(privatefifo, " ", 1);
while ((n = read(fileno(fin), buffer, PIPE_BUF)) > 0) {
write(privatefifo, buffer, n);
memset(buffer, 0x0, PIPE_BUF);
}
pclose(fin);
close(privatefifo);
done = 1;
}
} while (++n < 5 && !done);
if (!done) {
write(fileno(stderr),
" NOTE: SERVER ** NEVER ** accessed private FIFO ", 48);
return 2;
}
}
return 0;
}
=======================================================================================
Please let me know in case of any queries.