我需要帮助我正在制作的应用程序.这是一个响应命令行参数的简单程序.如果第一次调用该应用程序,它将在另一个专用于它的线程上作为管道服务器(阻塞,非重叠)启动,而主线程执行其他操作.现在,用户仍然可以使用相同的应用程序可执行文件和命令行参数调用应用程序,但由于它不是应用程序的第一个实例,因此它使用管道将命令行参数传递给第一个实例,然后杀死自身.所以,它就像一个模式术语中的单例过程.
理想情况下,应该是这样的:
app.exe "first" // starts app.exe as a pipe server and prints "first"
app.exe "second" // client process causes server instance to print "second"
app.exe "third" // client process causes server instance to print "third"
app.exe "fourth" // client process causes server instance to print "fourth"
app.exe "fifth" // client process causes server instance to print "fifth"
app.exe -quit // client process causes server instance to terminate.
Run Code Online (Sandbox Code Playgroud)
现在,我唯一的问题是,当我执行上述操作时会发生这种情况:
app.exe "first" // starts app.exe as a pipe server and prints …Run Code Online (Sandbox Code Playgroud) 我目前正在使用ASP.NET 3.5和C#学习Windows中的命名管道.我写了一个小型服务器程序,它创建了一个命名管道:
using (NamedPipeServerStream pipeStream = new NamedPipeServerStream(pipeName))
{
pipeStream.WaitForConnection();
// do sth.
}
Run Code Online (Sandbox Code Playgroud)
和一个打开管道的客户端应用程序如下:
using (NamedPipeClientStream pipeStream = new NamedPipeClientStream(pipeName))
{
pipeStream.Connect();
// do sth.
}
Run Code Online (Sandbox Code Playgroud)
只要只有一个客户端连接到管道,这就很有效.它既可以读写,也可以写入.如果我尝试连接第二个客户端,代码永远不会超过该行
pipeStream.Connect();
Run Code Online (Sandbox Code Playgroud)
服务器和所有客户端都在同一台计算机上运行.有任何想法吗?
非常感谢你提前!
我正在尝试使用NamedPipeServerStream.Net 4中创建命名管道服务器.我正在BeginWaitForConnection等待连接,以便我可以在服务器关闭时中止等待.
一切都适用于第一个客户端---确认连接,收到数据,发送响应正常.但是,在客户端断开连接后,一切都会中断.我BeginWaitForConnection再次呼吁等待一个新的连接,但这是一个IOException说"管道坏了" 的说法.
我怎样才能在同一个管道上等待第二个客户端?
我之前关于同一主题的问题:C#:异步NamedPipeServerStream理解 现在我有下一个:
private void StartListeningPipes()
{
try
{
isPipeWorking = true;
namedPipeServerStream = new NamedPipeServerStream(PIPENAME, PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous, BUFFERSIZE, BUFFERSIZE);
Console.Write("Waiting for client connection...");
while(isPipeWorking)
{
IAsyncResult asyncResult = namedPipeServerStream.BeginWaitForConnection(this.WaitForConnectionAsyncCallback, null);
Thread.Sleep(3*1000);
}
}
//// Catch the IOException that is raised if the pipe is broken or disconnected.
catch (IOException e)
{
Console.WriteLine("IOException: {0}. Restart pipe server...", e.Message);
StopListeningPipes();
StartListeningPipes();
}
//// Catch ObjectDisposedException if server was stopped. Then do nothing.
catch (ObjectDisposedException)
{
}
}
private void …Run Code Online (Sandbox Code Playgroud) 使用类时遇到的问题NamedPipeServerStream是,对于每个传入的连接,我需要创建新对象并调用它的方法WaitForConnection.
我想要做的是创建一个NamedPipeServerStream对象,然后在while循环中重复调用上述方法,如下所示:
NamedPipeServerStream s2;
using (s2 = new NamedPipeServerStream("pipe_name", PipeDirection.InOut)) {
while(true) {
ss2.WaitForConnection();
//do something here
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我这样做时,我得到了消息
流已断开连接.
有什么建议?