管道通讯

//pthread线程等待数据输入并通过管道发送将数据发送出去,另一线程thread1则时刻等待发送过来的数据,并对数据进行解析。


#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
#include<stdlib.h>
#include <string.h>
#include <errno.h>

pthread_t pthread;
pthread_t pthread1;
#define LEN 50

typedef struct pipe_object_struct {
    pthread_mutex_t mutex;
    int             pipes[2];
} pipe_object, *pipe_handle;

pipe_handle pipe_create(void);
int pipe_get(pipe_handle h_pipe, void *data, const int num);
int get_data_from_pipe(void *buf,int len);
void pipe_handld();

static pipe_handle Dat;

pipe_handle pipe_create(void)	//管道创建
{
    pipe_handle h_pipe;

    h_pipe = calloc(1, sizeof(pipe_object));

    if (h_pipe == NULL) {
        printf( "Failed to allocate space for pipe object\n");
        return NULL;
    }

    if (pipe(h_pipe->pipes)) {
        free(h_pipe);
        return NULL;
    }
    return h_pipe;
}

int pipe_get(pipe_handle h_pipe, void *data, const int num) //得到管道中的buf
{
    int ret = 0;
    ret = read(h_pipe->pipes[0], data, num);
    if (ret != num) {
        perror("pipe read");
        
        return -1;
    } else {
       
    }
    return 0;
}

int pipe_put(pipe_handle h_pipe, void *data, int num)//输入buf
{
    int ret=  0;
    ret = write(h_pipe->pipes[1], data, num);
    if (ret != num) {     
        return -1;
    } else {
       
    }
    return 0;
}

int put_data_to_pipe(void *buf,int len)//要发送的buf
{
	//char buf[]="a";
	return pipe_put(Dat,buf,len);
}

int get_data_from_pipe(void*buf,const int len)//得到buf
{
	return pipe_get(Dat,buf,len);
}

void pipe_handld(){	//接收到数据,并解析
	printf("pipe_handld thread create!!!!!!!!!!!!!!!\n");
	while(1){
		char buf[LEN];
		if(get_data_from_pipe(buf,LEN)==0){
			switch(buf[0]){
				case 'a':
				printf("get a\n");
				break;
				
				case 'b':
				printf("get b\n");
				break;
				
				case 'c':
				printf("get c\n");
				break;
				
				default:
				printf("not exit\n");
				break;
			}
		}	
		bzero(buf,LEN);
	}
	printf("exit pipe_handld\n");	
}

void pipe_input(){  //输入buf
	char buf[LEN];
	while(1){
		fgets(buf,LEN,stdin);
		put_data_to_pipe(buf,LEN);
		//sleep(3);
		//put_data_to_pipe(buf1,LEN);
		bzero(buf,LEN);
	}
	return;
}

int main(void)
{
	char buf1[]="b";
	Dat=pipe_create();	
	
	pthread_create(&pthread1,NULL,(void *)&pipe_input,NULL);
	pthread_create(&pthread,NULL,(void *)&pipe_handld,NULL);
	
	printf("after main\n");
	
	while(1){
		sleep(1);
	}
	return 0;
}

执行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的 C# 封装好的管道通讯实例: ```csharp using System; using System.IO.Pipes; using System.Text; namespace PipeCommunication { public class PipeClient { private NamedPipeClientStream pipeClient; public PipeClient(string pipeName) { pipeClient = new NamedPipeClientStream(".", pipeName, PipeDirection.InOut); } public void Connect() { pipeClient.Connect(); } public void Write(string message) { byte[] buffer = Encoding.UTF8.GetBytes(message); pipeClient.Write(buffer, 0, buffer.Length); pipeClient.Flush(); } public string Read() { byte[] buffer = new byte[pipeClient.InBufferSize]; pipeClient.Read(buffer, 0, buffer.Length); return Encoding.UTF8.GetString(buffer).TrimEnd('\0'); } public void Close() { pipeClient.Close(); } } public class PipeServer { private NamedPipeServerStream pipeServer; public PipeServer(string pipeName) { pipeServer = new NamedPipeServerStream(pipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Byte); } public void Start() { pipeServer.WaitForConnection(); } public void Write(string message) { byte[] buffer = Encoding.UTF8.GetBytes(message); pipeServer.Write(buffer, 0, buffer.Length); pipeServer.Flush(); } public string Read() { byte[] buffer = new byte[pipeServer.InBufferSize]; pipeServer.Read(buffer, 0, buffer.Length); return Encoding.UTF8.GetString(buffer).TrimEnd('\0'); } public void Close() { pipeServer.Close(); } } } ``` 使用该类进行管道通讯的示例如下: ```csharp using System; namespace PipeCommunication { class Program { static void Main(string[] args) { // 创建管道服务器 PipeServer server = new PipeServer("mypipe"); // 启动服务器 server.Start(); // 读取客户端发送的消息 string message = server.Read(); Console.WriteLine("客户端发送的消息:{0}", message); // 向客户端发送消息 server.Write("你好,客户端!"); // 关闭服务器 server.Close(); Console.ReadKey(); } } } ``` 客户端代码示例: ```csharp using System; namespace PipeCommunication { class Program { static void Main(string[] args) { // 创建管道客户端 PipeClient client = new PipeClient("mypipe"); // 连接服务器 client.Connect(); // 向服务器发送消息 client.Write("你好,服务器!"); // 读取服务器发送的消息 string message = client.Read(); Console.WriteLine("服务器发送的消息:{0}", message); // 关闭客户端 client.Close(); Console.ReadKey(); } } } ``` 这个示例是一个简单的管道通讯实现,你可以根据实际需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值