简体   繁体   中英

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. 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:

    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); right before the 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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