开发中用到,免得忘记,记录一下
Server:
private void IPCServer()
{
pipeServer = new NamedPipeServerStream("自定义管道名", PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
pipeServer.BeginWaitForConnection(PipeCallback, pipeServer);
}
private void PipeCallback(IAsyncResult ar)
{
var pipeServer = (NamedPipeServerStream)ar.AsyncState;
pipeServer.EndWaitForConnection(ar);
var data = new byte[1024];
var count = pipeServer.Read(data, 0, 1024);
if (count > 0)
{
string message = Encoding.UTF8.GetString(data, 0, count);
//TODO:
}
pipeServer.Disconnect();//一定要断开链接
pipeServer.BeginWaitForConnection(PipeCallback, pipeServer);
}
Send: