Tag: 命名管道

多个管道服务器实例的异步NamedPipes

我正在使用本文中的代码,区别在于,在NamedPipeServerStream构造函数中, maxNumberOfServerInstances设置为-1 (具有相同管道名称的服务器实例的数量仅受系统资源限制) 异步侦听方法[侦听服务器类]: class PipeServer { string _pipeName; public void Listen(string PipeName) { try { // Set to class level var so we can re-use in the async callback method _pipeName = PipeName; // Create the new async pipe NamedPipeServerStream pipeServer = new NamedPipeServerStream(PipeName, PipeDirection.In, -1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous); // Wait for a connection pipeServer.BeginWaitForConnection […]

C#套接字与管道

目前我正在开发Windows上的多进程桌面应用程序。 此应用程序将是一个收缩包装的应用程序,它将部署在世界各地的客户端计算机上。 虽然我们可以对机器有广泛的规格 – 例如带有.Net 4.0 CF的Windows XP SP3,但我们无法控制它们,我们无法对其配置过于具体 – 例如我们无法指定机器必须具有cuda 1.4图形处理器等等 其中一些进程是托管的(.Net 4.0),其他进程是非托管的(C ++ Win32)。 这些流程需要共享数据。 我迄今评估的选项是 Tcp套接字 命名管道 管道看起来好一点,但是对于我们的需求 – 两者的性能都是可以接受的。 套接字为我们提供了跨越机器(和操作系统 – 我们最终支持非Microsoft操作系统)边界的灵活性,因此我们倾向于使用套接字。 但是 – 我主要担心的是 – 如果我们使用Tcp套接字 – 我们是否可能遇到防火墙问题? 是否有其他人部署了使用TCP进行IPC和经历过问题的桌面应用程序/程序? 如果是这样 – 什么样的? 我知道这是一个相当开放的问题,我很乐意改写。 但我真的想知道我们可能会遇到什么样的潜在问题。 编辑:为了更轻松 – 我们只运送一些POD,整数,浮点数和字符串。 我们构建了一个抽象层,提供了两种范例 – 请求/响应和订阅。 传输层已被抽象出来,目前我们有两个实现 – 基于管道和基于TCP。

C#:异步NamedPipeServerStream管道正在关闭exception

我之前关于同一主题的问题: 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}. […]

C#所有管道实例都很忙

以下代码创建一个新线程,首先作为命名管道客户端发送参数,然后作为服务器检索结果。 之后,它在另一个充当命名管道服务器的AppDomain中执行一个函数,之后作为客户端发回结果。 public OrderPrice DoAction() { Task t = Task.Factory.StartNew(NamedPipeClient, parameters); if (domain == null) { domain = AppDomain.CreateDomain(DOMAINNAME); } domain.DoCallBack(AppDomainCallback); return t.Result; } static OrderPrice NamedPipeClient(object parameters) { OrderPrice price = null; using (NamedPipeClientStream stream = new NamedPipeClientStream(PIPE_TO)) { stream.Connect(); SerializeToStream(stream, parameters); } using (NamedPipeServerStream stream = new NamedPipeServerStream(PIPE_BACK)) { stream.WaitForConnection(); price = (OrderPrice)DeserializeFromStream(stream); […]

命名管道 – 异步偷看

当在异步模式下打开的System.IO.Pipe.NamedPipeServerStream有更多可用于读取的数据时,我需要找到一种通知方式 – WaitHandle将是理想的。 我不能简单地使用BeginRead()来获取这样的句柄,因为我可能会被另一个想要写入管道的线程发出信号 – 所以我必须释放管道上的锁并等待写入完成,和NamedPipeServerStream没有CancelAsync方法。 我也尝试调用BeginRead(),然后如果线程被发出信号,则在管道上调用win32函数CancelIO,但我不认为这是一个理想的解决方案,因为如果在数据到达和处理时调用CancelIO,它将会被删除 – 我仍然希望保留这些数据,但是在写入之后稍后处理它。 我怀疑win32函数PeekNamedPipe可能有用,但我想避免不得不用它连续轮询新数据。 在上面的文字有点不清楚的情况下,这里大致是我想要做的… NamedPipeServerStream pipe; ManualResetEvent WriteFlag; //initialise pipe lock (pipe) { //I wish this method existed WaitHandle NewDataHandle = pipe.GetDataAvailableWaithandle(); Waithandle[] BreakConditions = new Waithandle[2]; BreakConditions[0] = NewDataHandle; BreakConditions[1] = WriteFlag; int breakcode = WaitHandle.WaitAny(BreakConditions); switch (breakcode) { case 0: //do a read on the pipe […]

System.IO.Exception:管道已损坏

我有两个.NET应用程序通过命名管道相互通信。 第一次发送时一切都很好,但是在发送第一条消息之后,服务器将再次监听, WaitForConnection()方法抛出System.IO.Exception ,消息管道已断开。 为什么我在这里得到这个例外? 这是我第一次使用管道,但类似的模式在过去使用套接字对我有用。 代码啊! 服务器: using System.IO.Pipes; static void main() { var pipe = new NamedPipeServerStream(“pipename”, PipeDirection.In); while (true) { pipe.Listen(); string str = new StreamReader(pipe).ReadToEnd(); Console.Write(“{0}”, str); } } 客户: public void sendDownPipe(string str) { using (var pipe = new NamedPipeClientStream(“.”, “pipename”, PipeDirection.Out)) { using (var stream = new StreamWriter(pipe)) { […]

C#:异步NamedPipeServerStream的理解

我试图找到异步NamedPipeServerStream的任何好的和明确的例子,找不到任何适合我的。 我想让NamedPipe Server异步接受来自客户端的消息。 客户很简单,对我来说很好。 但是我找不到服务器的例子,或者无法理解它是如何工作的。 现在据我所知,我需要创建NamedPipeServerStream对象。 我们开工吧: namedPipeServerStream = new NamedPipeServerStream(PIPENAME, PipeDirection.In, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous, BUFFERSIZE, BUFFERSIZE); 似乎工作。 但我不知道,我是否必须使用PipeSecurity或PipeAccessRule? 我呢? 我的服务器将在本地系统中用作Windows服务。 接下来是什么? 我想我需要使用BeginWaitForConnection进行异步连接。 让我们来看看: namedPipeServerStream.BeginWaitForConnection(WaitForConnectionAsyncCallback, ); 问题1 :这有什么“奇怪的东西”? 如何使用它? 问题2 :我应该这样做 while(true) { namedPipeServerStream.BeginWaitForConnection(WaitForConnectionAsyncCallback, ); } 让我的服务器总是等待连接? 或者我需要以其他方式做到这一点? 然后……让我们来看看WaitForConnectionAsyncCallback函数: private void WaitForConnectionAsyncCallback(IAsyncResult result) { Console.WriteLine(“Client connected.”); byte[] buff = new byte[BUFFERSIZE]; namedPipeServerStream.Read(buff, 0, namedPipeServerStream.InBufferSize); string […]

如何在网络上使用命名管道?

我正在尝试通过命名管道在网络上创建连接。 我正如msdn中所说的那样做。 我用function创建管道服务器端。 CreateNamedPipe( “\\\\.\\pipe\\myNamedPipe”, DUPLEX | FILE_FLAG_OVERLAPPED, 0, 255, BUFFER_SIZE, BUFFER_SIZE, 0, IntPtr.Zero); 并尝试通过CreateFile()函数进行连接 CreateFile( “\\\\10.0.0.29\\pipe\\myNamedPipe”, GENERIC_READ | GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, IntPtr.Zero); 10.0.0.29是服务器机器ip。 如果我试图在服务器计算机上运行客户端程序,管道名称为“\\。\ pipe \ myNamedPipe”或“\\ 10.0.0.29 \ pipe \ myNamedPipe”(10.0.0.29是服务器IP)或“\\ localhost \ pipe \ myNamedPipe“它工作正常。 那么如何在网络上使用命名管道呢?

SqlException:System.Data.SqlClient.SqlException(0x80131904)

我在C#中编写了一个代码,它在我的计算机上运行得很好,使用Windows 7(MS SQL Server 2008)但在Windows Vista(MS SQL Server 2005)中却没有。 我无法在第二台计算机上更改系统;)我正在使用Visual Studio 2010。 所以这是代码的一部分,来自我的类​​“obSQL”: private SqlConnection connection; public obSQL(string user, string pass, string instance, string dbdir) //sql server authentication { connection = new SqlConnection(); connection.ConnectionString = “user id=” + user + “;” + “password=” + pass + “;Data Source=” + instance + “;” + “Trusted_Connection=no;” + […]

WCF NamedPipe CommunicationException – “管道已结束。 (109,0x6d)。“

我正在编写带有“状态工具”的Windows服务。 该服务托管一个名为管道端点的WCF,用于进程间通信。 通过命名管道,状态工具可以定期查询服务以获取最新的“状态”。 在我的开发机器上,我有多个IP地址; 其中一个是具有192.168.1.XX地址的“本地”网络。 另一个是“公司”网络,具有10.0.X.XX地址。 Windows服务在单个IP地址上收集UDP多播流量。 到目前为止,Windows服务只要使用“192.168.1.XX”地址就可以正常工作。 它始终正确地向客户报告状态。 一旦我切换到另一个“公司”IP地址(10.0.X.XX)并重新启动服务,我在检索状态时会得到连续的“CommunicationExceptions”: “There was an error reading from the pipe: The pipe has been ended. (109, 0x6d).” 现在,我不认为UDP客户端的“声明的”IP地址应该与Named-Pipe接口的function有关; 它们完全是应用程序的独立部分! 以下是相关的WCF配置部分: //On the Client app: string myNamedPipe = “net.pipe://127.0.0.1/MyNamedPipe”; ChannelFactory proxyFactory = new ChannelFactory( new NetNamedPipeBinding(), new EndpointAddress(myNamedPipe)); //On the Windows Service: string myNamedPipe = “net.pipe://127.0.0.1/MyNamedPipe”; myService = […]