一个server与client通信的例子
/*---------------- program begin ----------------*/
/* the first part is the head file of the program, which contains a server and a client communicating through pipes.
* Name: pipe5.h
* total 3 files: pipe5.h, pipe5_client.c, pipe5_server.c
* 07-11-09 */
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
#include<limits.h>
#define PUBLIC "/tmp/PUBLIC"
#define B_SIZE (PIPE_BUF/2)
struct message{
char fifo_name[B_SIZE];
char cmd_line[B_SIZE];
}
/* server part, Name:pipe5_server.c
* run server first,and then client.
*/
#include "pipe5.h"
int main(void){
int n,done,dummyfifo,publicfifo,privatefifo;
struct message msg;
FILE *fin;
static char buffer[PIPE_BUF];
/*gererate public FIFO*/
mknod(PUBLIC,S_IFIFO|0666,0);
/*open public FIFO and read*/
if(((publicfifo=open(PUBLIC,O_RDONLY))==-1)||
((dummyfifo=open(PUBLIC,O_WRONLY|O_NDELAY))==-1)) {
perror(PUBLIC);
exit(1);
}
/*read info from public FIFO */
while(read(publicfifo,(char *) &msg,sizeof(msg)) > 0){
n = done = 0; //clear the counter and flag.
do{
if((privatefifo =open(msg.fifo_name,O_WRONLY|O_NDELAY))==-1)
sleep(3);
else{ //open fifo success
fin = popen(msg.cmd_line,"r"); //exec cmd.
write(privatefifo,"/n",1); //formate output
while((n =read(fileno(fin),buffer,PIPE_BUF))>0){
write(privatefifo,buffer,n); //write private
memset(buffer,0x0,PIPE_BUF); //clear
}//end of while.
pclose(fin);
close(privatefifo);
done = 1; //record success
}//end of if-else.
}while(++n<5 && !done);//end of do-while.
if(!done) //record failed.
write(fileno(stderr),"/nNote:Server Never Accessed Private FIFO/n",48);
}//end of while.
return 0;
}//end of main().
/* client part, to run after server. Name: pipe5_client.c
* 07-11-09,pm.*/
#include "pipe5.h"
int main(void){
int n,privatefifo,publicfifo;
static char buffer[PIPE_BUF];
struct message msg;
/*generate name of private pipe */
sprintf(msg.fifo_name,"/tmp/fifo%d",getpid());
/*generate private FIFO */
if(mknod(msg.fifo_name,S_IFIFO| 0666, 0) < 0){
/*mknod will generate a FIFO file with a privilege of "0666"*/
perror(msg.fifo_name);
exit(1);
}//end of if.
if((publicfifo = open(PUBLIC,O_WRONLY))==-1){
/*open public FIFO for writing */
perror(PUBLIC);
exit(2);
}//end of if.
while(1){
write(fileno(stdout),"/ncmd>",6); /*prompt information*/
memset(msg.cmd_line,0x0,B_SIZE); /*clear buffer*/
n = read(fileno(stdin),msg.cmd_line,B_SIZE);/*get cmd */
if(! strncmp("quit",msg.cmd_line,n-1)) /*set quit condition*/
break;
write(publicfifo,(char*) &msg, sizeof(msg));
/*open private FIFO,and read the results of cmd */
if( (privatefifo = open(msg.fifo_name,O_RDONLY))==-1){
perror(msg.fifo_name);
exit(3);
}//end of if.
/*read from private FIFO and display on std error */
while((n=read(privatefifo,buffer,PIPE_BUF)) >0){
write(fileno(stderr),buffer,n);
}//end of while.
close(privatefifo);
}//end of while(1).
close(publicfifo);
unlink(msg.fifo_name);//delete private pipe.
exit(0);
}
/* ---------- over --------------*/