送信側
using System; using System.IO.Pipes; using System.Text; using System.Threading; public class NamedPipeSend { const string NAME = "test"; public static void Main(string[] args) { //1.パイプ作成 NamedPipeClientStream client = new NamedPipeClientStream(".",NAME,PipeDirection.Out); //2.サーバに接続 client.Connect(); byte[] sendData = new byte[1024]; while(true) { //3.処理 Thread.Sleep(1000); DateTime d = DateTime.Now; Encoding enc = Encoding.UTF8; sendData = enc.GetBytes(d.ToString()); client.Write(sendData,0,sendData.Length); client.Flush(); } } }
受信側
using System; using System.IO.Pipes; using System.Text; public class NamedPipeReceive { const string NAME = "test"; public static void Main(string[] args) { //1.パイプ作成 NamedPipeServerStream server = new NamedPipeServerStream(NAME,PipeDirection.In); //2.受信待ち server.WaitForConnection(); byte[] rData = new byte[4096]; int len; while(true) { rData = new byte[4096]; //データ受信 len = server.Read(rData,0,rData.Length); if(len == 0) { break; } Array.Resize(ref rData,len); Encoding enc = Encoding.UTF8; System.Console.WriteLine(enc.GetString(rData)); } } }
おまけ(名前付きパイプの一覧表示)
public class NamedPipeList { public static void Main() { string[] pipeList = System.IO.Directory.GetFiles("\\\\.\\pipe\\"); foreach(string pipe in pipeList) { System.Console.WriteLine(pipe); } } }
0 件のコメント:
コメントを投稿