如何解决NamedPipe 不会执行
我有两个命名管道,一个是 ServerPipe,另一个是 ClientPipe。
当我手动执行它们时,它们工作正常,但是例如,当我手动执行服务器并通过从批处理文件调用它来执行客户端时,它不会执行
服务器管道代码:
using System;
using System.IO;
using System.IO.Pipes;
using System.Threading;
class PipeServer
{
static void Main()
{
using (NamedPipeServerStream pipeServer =
new NamedPipeServerStream("TP"))
{
Console.WriteLine("NamedPipeSe rverStream object created.");
// Wait for a client to connect
Console.Write("Waiting for client connection...");
pipeServer.WaitForConnection();
Console.WriteLine("Client connected.");
try
{
// Read user input and send that to the client process.
/* using (StreamWriter sw = new StreamWriter(pipeServer))
{
sw.AutoFlush = true;
Console.Write("Enter text: ");
sw.WriteLine(Console.ReadLine());
}*/
using (StreamReader sr = new StreamReader(pipeServer))
{
// display the read text to the console
string temp;
while ((temp = sr.ReadLine()) != null)
{
Console.WriteLine("Received from client: {0}",temp);
}
}
}
// Catch the IOException that is raised if the pipe is broken
// or disconnected.
catch (IOException e)
{
Console.WriteLine("ERROR: {0}",e.Message);
}
Console.WriteLine("It reached this");
Console.WriteLine(pipeServer.IsConnected);
Thread.Sleep(5000);
pipeServer.dispose();
}
}
}
ServerPipe 代码:
using System;
using System.IO;
using System.IO.Pipes;
using System.Threading;
class PipeClient
{
static void Main(string[] args)
{
string path = @"D:\MyTest.txt";
// This text is added only once to the file.
if (!File.Exists(path))
{
// Create a file to write to.
string createText = "test1" + Environment.NewLine;
File.WriteallText(path,createText);
}
using (NamedPipeClientStream pipeClient =
new NamedPipeClientStream("TP"))
{
// Connect to the pipe or wait until the pipe is available..
Console.Write("Attempting to connect to pipe...");
pipeClient.Connect();
string path2 = @"D:\MyTest2.txt";
// This text is added only once to the file.
if (!File.Exists(path2))
{
// Create a file to write to.
string createText = "test2" + Environment.NewLine;
File.WriteallText(path2,createText);
}
Console.WriteLine("Connected to pipe.");
Console.WriteLine("There are currently {0} pipe server instances open.",pipeClient.NumberOfServerInstances);
/*using (StreamReader sr = new StreamReader(pipeClient))
{
// display the read text to the console
string temp;
while ((temp = sr.ReadLine()) != null)
{
Console.WriteLine("Received from server: {0}",temp);
}
}*/
string path3 = @"D:\MyTest3.txt";
// This text is added only once to the file.
if (!File.Exists(path3))
{
// Create a file to write to.
string createText = "test3" + Environment.NewLine;
File.WriteallText(path3,createText);
}
using (StreamWriter sw = new StreamWriter(pipeClient))
{
sw.AutoFlush = true;
Console.Write("Enter text: ");
sw.WriteLine("123");
}
string path4 = @"D:\MyTest4.txt";
// This text is added only once to the file.
if (!File.Exists(path4))
{
// Create a file to write to.
string createText = "test4" + Environment.NewLine;
File.WriteallText(path4,createText);
Console.WriteLine("It reached this");
}
pipeClient.dispose();
}
}
}
批处理文件:
@ECHO OFF
ECHO Congratulations! Your first batch file executed successfully.
cd D:\PipeExSer\PipeExClient\bin\Debug
PipeExClient.exe
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。