client.exeとserver.exeの2つの実行可能ファイルがあります。
名前付きパイプを使用して相互に通信します。サーバーがクライアント要求のみをリッスンし、クライアントに応答を送信する場合、これらは正常に機能します。
ただし、現在の要件は、両方のexeにクライアントパイプとサーバーパイプが含まれていることです。したがって、各exeには2つのメソッドが含まれています:serverpipe()
とclientpipe()
。これらのメソッドはスレッド内にあり、相互に通信しません。
最初のクライアントとサーバーは適切に通信します。
Client.EXE:
public static void Main(string[] Args)
{
PipeClient obj=new PipeClient();
obj.client();
Thread startserver = new Thread(new ThreadStart(obj.server));
startserver.Start();
}
public void client()
{
int cnt =0;
while (true)
{
System.IO.Pipes.NamedPipeClientStream pipeClient = new System.IO.Pipes.NamedPipeClientStream(".", "\\\\.\\pipe\\SamplePipe1", System.IO.Pipes.PipeDirection.InOut, System.IO.Pipes.PipeOptions.None);
if (pipeClient.IsConnected != true) { pipeClient.Connect(); }
StreamReader sr = new StreamReader(pipeClient);
StreamWriter sw = new StreamWriter(pipeClient);
string temp;
temp = sr.ReadLine();
if (temp == "Waiting")
{
try
{
cnt++;
sw.WriteLine("clientSide Message " + cnt);
sw.Flush();
Thread.Sleep(10000);
}
catch (Exception ex) { throw ex; }
}
else
{
}
pipeClient.Close();
}
}
public void server()
{
StreamWriter sw1 = new StreamWriter("C:\\ServerEXE\\serverdataFile0.txt", true);
System.IO.Pipes.NamedPipeServerStream pipeServer = new System.IO.Pipes.NamedPipeServerStream("\\\\.\\pipe\\SamplePipeSend1", System.IO.Pipes.PipeDirection.InOut, 4);
sw1 = new StreamWriter("C:\\ServerEXE\\serverdataFile2.txt", true);
sw1.Close();
StreamReader sr = new StreamReader(pipeServer);
StreamWriter sw = new StreamWriter(pipeServer);
do
{
try
{
pipeServer.WaitForConnection();
string test;
sw.WriteLine("Waiting");
sw.Flush();
pipeServer.WaitForPipeDrain();
test = sr.ReadLine();
sw1 = new StreamWriter("C:\\ServerEXE\\serverdataFile2.txt",true);
sw1.WriteLine(test);
sw1.Close();
}
catch (Exception ex)
{ throw ex; }
finally
{
pipeServer.WaitForPipeDrain();
if (pipeServer.IsConnected) { pipeServer.Disconnect(); }
}
} while (true);
}
Server.EXE:
public static void Main()
{
PipeServer obj = new PipeServer();
obj.server();
Thread startserver = new Thread(new ThreadStart(obj.client));
startserver.Start();
}
public void server()
{
System.IO.Pipes.NamedPipeServerStream pipeServer = new System.IO.Pipes.NamedPipeServerStream("\\\\.\\pipe\\SamplePipe1", System.IO.Pipes.PipeDirection.InOut, 4);
StreamReader sr = new StreamReader(pipeServer);
StreamWriter sw = new StreamWriter(pipeServer);
do
{
try
{
pipeServer.WaitForConnection();
string test;
sw.WriteLine("Waiting");
sw.Flush();
pipeServer.WaitForPipeDrain();
test = sr.ReadLine();
Console.WriteLine(test);
}
catch (Exception ex)
{ throw ex; }
finally
{
pipeServer.WaitForPipeDrain();
if (pipeServer.IsConnected) { pipeServer.Disconnect(); }
}
} while (true);
}
public void client()
{
int cnt = 0;
while (true)
{
System.IO.Pipes.NamedPipeClientStream pipeClient = new System.IO.Pipes.NamedPipeClientStream(".", "\\\\.\\pipe\\SamplePipeSend1", System.IO.Pipes.PipeDirection.InOut, System.IO.Pipes.PipeOptions.None);
if (pipeClient.IsConnected != true) { pipeClient.Connect(); }
StreamReader sr = new StreamReader(pipeClient);
StreamWriter sw = new StreamWriter(pipeClient);
string temp;
temp = sr.ReadLine();
if (temp == "Waiting")
{
try
{
cnt++;
sw.WriteLine("clientSide Message " + cnt);
sw.Flush();
Thread.Sleep(10000);
}
catch (Exception ex) { throw ex; }
}
else
{
pipeClient.Close();
}
}
}