In the program running on the server terminal, the pipe np1 is opened in the read only mode. Thereby, allowing it to read the information being sent by the client terminal. In the program running on the client terminal, the pipe np2 is opened in the read only mode so the information being written by the server can be read by the client. Whenever the information from the pipe is being read, it is temporarily stored in a buffer before being printed out on the screen.
PROGRAM LISTING
fullduplex.h
//Reference: http://developers.sun.com/solaris/articles/named_pipes.html
#define NP1 "/tmp/np1"
#define NP2 "/tmp/np2"
#define MAX_BUF_SIZE 255
server.c
//Reference : http://developers.sun.com/solaris/articles/named_pipes.html
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "fullduplex.h" /* For name of the named-pipe */
int main()
{
int ReadFromPipe, WriteToPipe, return_val, NumChar;
char buffer[MAX_BUF_SIZE];
// Create the first named pipe
return_val = mkfifo(NP1, 0666);
if ( return_val == -1 && errno != EEXIST)
{
perror("Error creating the named pipe");
exit(1);
}
//creates second named pipe
return_val = mkfifo(NP2, 0666);
if (return_val == -1 && errno != EEXIST)
{
perror("Error creating the named pipe");
exit(1);
}
/* Open the first named pipe for reading */
/* Open the second named pipe for writing */
ReadFromPipe = open(NP1, O_RDONLY);
WriteToPipe = open(NP2, O_WRONLY);
while(1)
{
/* Read from the first pipe */
NumChar = read(ReadFromPipe, buffer, MAX_BUF_SIZE);
buffer[NumChar] = 0; //ends the string by placing a null character in the last slot
printf("Client says: %s\nYou say:", buffer);
gets (buffer);
/* Writes to the second pipe*/
write(WriteToPipe, buffer, strlen(buffer));
}
}
client.c
//Reference: http://developers.sun.com/solaris/articles/named_pipes.html
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "fullduplex.h" /* For name of the named-pipe */
int main()
{
int WriteToPipe, ReadFromPipe, NumChar;
char rdbuffer[MAX_BUF_SIZE];
/* Open the first named pipe for writing*/
/* Open the second named pipe for reading */
WriteToPipe = open(NP1, O_WRONLY);
ReadFromPipe = open(NP2, O_RDONLY);
printf("Start conversation. Say something: ");
while(1)
{
gets(rdbuffer);
/* Write to the pipe */
write(WriteToPipe, rdbuffer, strlen(rdbuffer));
/* Reads from the second pipe */
NumChar = read(ReadFromPipe, rdbuffer, MAX_BUF_SIZE);
rdbuffer[NumChar] = 0; //ends the string by placing '0' in the next slot
printf("Server says: %s\nYou say: ", rdbuffer);
}
}
SCREENSHOTS