简体   繁体   English

NamedPipeClientStream一次连接

[英]NamedPipeClientStream one time connection

i have windows service such that it receives data from a TCP connection and then sends same data immediately to a web service through named pipes. 我有Windows服务,以便它从TCP连接接收数据,然后通过命名管道立即将相同数据发送到Web服务。 can anyone suggest me how i can set the pipe such that i connect one time to the pipe and then use same pipe for all the incoming connection. 谁能建议我如何设置管道,使我一次连接到管道,然后对所有传入连接使用相同的管道。

This is part of my code on the windows service: 这是我在Windows服务上的代码的一部分:

    pipeStream = new NamedPipeClientStream(".", pipename, PipeDirection.Out);
    while (true)
        {
            byte[] data = new byte[100];
            int recv = newTCP.Receive(data, ref tmpRemote);
            try
            {
                pipeStream.Connect(3);
                pipeStream.Write(data,0,recv);
        }

Move pipeStream.Connect(3); 移动pipeStream.Connect(3); right before the while(true); 就在while(true)之前; So now you have the stream that you can use over and over for writing. 因此,现在您有了可以反复使用的流来进行编写。 Now, after every write, if you want the data to go right away, make sure to call the flush method: 现在,在每次写入之后,如果您希望数据立即发送,请确保调用flush方法:

pipeStream = new NamedPipeClientStream(".", pipename, PipeDirection.Out);
pipeStrea.Connect(3);
while(true)
{
    ... // Get your TCP data
    pipeStream.Write(data,0,recv);
    pipeStream.Flush();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM