windows上的pipe有兩種
1. anonymous pipe
2. named pipe
顧名思義,前者的pipe沒有名字,後者有名字
anonymous pipe主要用在parent process和child process之間的溝通,但只能用在單一電腦上。至於named pipe就比較強大,可以用在跨網路上
本文以windows上的named pipe為範例
C# server
using System.IO.Pipes;
using (NamedPipeServerStream pipeStream = new NamedPipeServerStream("mypipe"))
{
pipeStream.WaitForConnection();
StreamReader sr = new StreamReader(pipeStream);
string tmp;
while ((tmp = sr.ReadLine()) != null)
pipeMsg.Text += tmp + "\r\n";
}
C++ client
#include <windows.h>
#include <stdio.h>
hFile = CreateFile("//./pipe/mypipe", GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE , NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
if(hFile == INVALID_HANDLE_VALUE)
{
DWORD dw = GetLastError();
printf("CreateFile failed for Named Pipe client\n:" );
break;
}
else
{
flg = WriteFile(hFile, s.c_str(), s.length(), &dwWrite, NULL);
if (FALSE == flg)
printf("WriteFile failed for Named Pipe client\n");
CloseHandle(hFile);
}
C#已經把windows的named pipe API包得好好的,因此程式碼看起來比較平易近人。至於C++這邊要注意的是,連到named pipe時的路徑,要在pipe名稱前面加上
//./pipe/
否則會連不上唷
以下為完整可編譯的專案 (VS 2012)
https://docs.google.com/file/d/0Byr8Dpnq_aK8cUN3UjhPbUdsTUE/edit?usp=sharing
參考資料
http://www.cnblogs.com/yukaizhao/archive/2011/08/04/system-io-pipes.html
小弟幫你把範例code,基本的可以編過
回覆刪除server:
https://gist.github.com/petershen0307/3752598555604fdc90ea7264f4ec649c
client:
https://gist.github.com/petershen0307/c51b02ea26b201588bfe501299510023
https://gist.github.com/petershen0307/d08d406e10876ac8811cccba2eecd688
刪除c ++编程示例
回覆刪除二叉搜索树