日期:2014-05-16  浏览次数:20618 次

一个LINUX下管道通信的例子

client端------现在有问题...........

#include <unistd.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define FIFO_HANDLE_NAME	"/tmp/fifo_handle "
#define FIFO_CLIENT_NAME	"/tmp/fifo_client_%d "

struct ps_fifo_struct{
	pid_t pid;
	char str[64];
};

int main()
{
	int fifo_handle, fifo_client;		//fifo_handle is use as write_pipe
	struct ps_fifo_struct ps_fifo;
	char client_fifo_name[64];

	fifo_handle = open(FIFO_HANDLE_NAME, O_WRONLY);		//判断管道文件是否已经存在
	if (fifo_handle == -1)							
	{
		fprintf(stderr, "Open handle fifo failed\n");
		exit(EXIT_FAILURE);	
	}
	
	ps_fifo.pid = getpid();
	memset(client_fifo_name, 0, sizeof(client_fifo_name));
	sprintf(client_fifo_name, FIFO_CLIENT_NAME, ps_fifo.pid);
	if (access(client_fifo_name, F_OK) == -1)
	{
		if (mkfifo(client_fifo_name, 0777) != 0)		//创建读管道
		{
			fprintf(stderr, "Could not create fifo %s\n", client_fifo_name);
			exit(EXIT_FAILURE);
		}
	}

	sprintf(ps_fifo.str, "hi, I'm %d.", ps_fifo.pid);
	printf("%d sent: \'%s\'.\n", ps_fifo.pid, ps_fifo.str);
	write(fifo_handle, &ps_fifo, sizeof(ps_fifo));			//Write to write_pipe:fifo_handle,
	
	close(fifo_handle);

	fifo_client = open(client_fifo_name, O_RDONLY);			//打开读管道
	if (fifo_client != -1)
	{
		if (read(fifo_client, &ps_fifo, sizeof(ps_fifo)) > 0)	//从读管道中读取消息
			printf("received from %d: %s\n", ps_fifo.pid, ps_fifo.str);
		close(fifo_client);
	}
	
//	close(fifo_handle);
	unlink(client_fifo_name);	
	exit(EXIT_SUCCESS);
}










server.c

#include <unistd.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define FIFO_HANDLE_NAME	"/tmp/fifo_handle "
#define FIFO_CLIENT_NAME	"/tmp/fifo_client_%d"

struct ps_fifo_struct{
	pid_t pid;
	char str[64];
};

int main()
{
	int fifo_handle, fifo_client;		//fifo_handle为要读的管道
	struct ps_fifo_struct ps_fifo;
	int read_len;
	char client_fifo_name[64];
	char answer_str[64];
	
	if (access(FIFO_HANDLE_NAME, F_OK) == -1)
	{
		if (mkfifo(FIFO_HANDLE_NAME, 0777) != 0)
		{
			fprintf(stderr, "Could not create fifo %s\n", FIFO_HANDLE_NAME);
			exit(EXIT_FAILURE);
		}
	}

	fifo_handle = open(FIFO_HANDLE_NAME, O_RDONLY);
	if (fifo_handle = -1)
	{
		fprintf(stderr, "Handle fifo failure\n");
		exit(EXIT_FAILURE);
	}

	do {
		read_len = read(fifo_handle, &ps_fifo, sizeof(ps_fifo));		//从读管道中读取数据
		memset(answer_str, 0, 64);
		if (read_len > 0)
		{
			sprintf(answer_str, "hi, %d, I have received the string: '%s'.", ps_fifo.pid, ps_fifo.str);
			printf("received from %d:%s\n", ps_fifo.pid, ps_fifo.str);
			memset(client_fifo_name, 0, 64);
			sprintf(client_fifo_name, FIFO_CLIENT_NAME, ps_fifo.pid);
			fifo_client = open(client_fifo_name, O_WRONLY);				//打开client要读取的文件,即向其写入
			ps_fifo.pid = getpid();
			strcpy(ps_fifo.str, answer_str);
			//sprintf(ps_fifo.str, answer_str);
			if (fifo_client != -1)
			{
				write(fifo_client, &ps_fifo, sizeof(ps_fifo));	
				close(fifo_client);
			}			
		}
	} while(read_len > 0);

	close(fifo_handle);
	unlink(FIFO_HANDLE_NAME);
	exit(EXIT_SUCCESS);
}