pa.general

I have this as a server:

Code Block

using System;

using System.Collections.Generic;

using System.Text;

using System.Runtime.InteropServices;

using Microsoft.Win32.SafeHandles;

using System.IO;

using System.Threading;

namespace Server

{

class ServerPipe

{

public const string PIPE_NAME = "\\\\.\\pipe\\namedPipe";

public const int BUFFER_SIZE = 4096;

SafeFileHandle clientPipeHandle;

[DllImport("kernel32.dll", SetLastError = true)]

public static extern SafeFileHandle CreateNamedPipe(

String pipeName,

uint dwOpenMode,

uint dwPipeMode,

uint nMaxInstances,

uint nOutBufferSize,

uint nInBufferSize,

uint nDefaultTimeOut,

IntPtr lpSecurityAttributes

);

[DllImport("kernel32.dll", SetLastError = true)]

public static extern int ConnectNamedPipe(

SafeFileHandle hNamedPipe,

IntPtr lpOverlapped

);

public void Listen()

{

while (true)

{

clientPipeHandle = CreateNamedPipe(

PIPE_NAME,

3 | 0x40000000,

0,

255,

BUFFER_SIZE,

BUFFER_SIZE,

0,

IntPtr.Zero

);

//failed to create named pipe

if (clientPipeHandle.IsInvalid)

break;

else

Console.WriteLine("Pipe created, waiting for a client to connect");

int success = ConnectNamedPipe(clientPipeHandle, IntPtr.Zero);

Console.WriteLine("A client connected");

//failed to connect client pipe

if (success != 1)

break;

//client connection successfull

//handle client communication

Console.WriteLine("Got this far, attempting to read");

Thread readThread = new Thread(new ThreadStart(this.Read));

readThread.Start();

//this.Read();

}

}

private void Read()

{

byte[] buffer = new byte[BUFFER_SIZE];

ASCIIEncoding encoder = new ASCIIEncoding();

FileStream fStream = new FileStream(clientPipeHandle, FileAccess.Read, BUFFER_SIZE, true);

while (true)

{

int bytesRead = fStream.Read(buffer, 0, BUFFER_SIZE);

//could not read from file stream

if (bytesRead == 0)

{

Console.WriteLine("Nothing came through");

break;

}

System.Diagnostics.Debug.WriteLine( encoder.GetString(buffer) );

Console.WriteLine(encoder.GetString(buffer));

}

}

private void Write(string message)

{

FileStream fStream = new FileStream(clientPipeHandle, FileAccess.Write, BUFFER_SIZE, true);

ASCIIEncoding encoder = new ASCIIEncoding();

byte[] sendBuffer = encoder.GetBytes(message);

fStream.Write(sendBuffer, 0, sendBuffer.Length);

}

}

}

And this as a client:

Code Block

using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.Win32.SafeHandles;

using System.Runtime.InteropServices;

using System.IO;

namespace Client

{

class ClientPipe

{

public const string PIPE_NAME = "\\\\.\\pipe\\namedPipe";

public const int BUFFER_SIZE = 4096;

FileStream fStream;

[DllImport("kernel32.dll", SetLastError = true)]

public static extern SafeFileHandle CreateFile(

String pipeName,

uint dwDesiredAccess,

uint dwShareMode,

IntPtr lpSecurityAttributes,

uint dwCreationDisposition,

uint dwFlagsAndAttributes,

IntPtr hTemplate

);

public void Connect()

{

SafeFileHandle pipeHandle = CreateFile(PIPE_NAME,

0,

0,

IntPtr.Zero,

3,

0 | 0x40000000,

IntPtr.Zero

);

//could not get a handle to the named pipe

if (pipeHandle.IsInvalid)

return;

fStream = new FileStream(pipeHandle, FileAccess.Write, BUFFER_SIZE, true);

Write("Hello Server!");

}

private void Write(string message)

{

ASCIIEncoding encoder = new ASCIIEncoding();

byte[] sendBuffer = encoder.GetBytes(message);

fStream.Write(sendBuffer, 0, sendBuffer.Length);

Console.WriteLine("Wrote: " + sendBuffer.Length + ", check server output");

}

}

}

The problem is that even though the client claims that it sent the byte array (gets to the: Console.WriteLine("Wrote: " + sendBuffer.Length + ", check server output"))

The server gives the following IOException (and of course doesn't get the message):

Code Block

Waiting for a process to open the other end of the pipe.

I can't figure out what's wrong.


Re: Visual C# General Named pipes problem

pa.general

Anyone




Re: Visual C# General Named pipes problem

Ji Cheng Wang - MSFT

Hi pa,

I checked your error message saying "Waiting for a process to open the other end of the pipe" and found this was an informational message and no user action was needed - http://msdn2.microsoft.com/en-us/library/ms838094.aspx.

Could you please clarify more details about IOException message you get in the named pipe communication

For your concern, I recommend you checking out the following articles about "Inter-Process Communication in .NET Using Named Pipes" for reference:

http://www.codeproject.com/cs/threads/dotnetnamedpipespart1.asp

http://blog.paranoidferret.com/index.php/2007/07/25/interprocess-communication-using-named-pipes-in-csharp/

Try to get more information about named pipe from this document - http://msdn2.microsoft.com/en-us/library/aa365590.aspx

Hope this helps,

Regards,

This response contains links reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you.
Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.
There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.