
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
906
|
Chapter 16: Networking
class NamedPipeClientConsole
{
static void Main(string[] args)
{
// Client test code - commented out as it should go in a separate
// console test app
// Create our pipe client.
NamedPipeClient _pc =
new NamedPipeClient("\\\\.\\PIPE\\mypipe");
if (_pc != null)
{
using (_pc)
{
// Connect to the server.
_pc.Connect( );
// Set up a dummy message.
string testString = "This is my message!";
// Turn it into a byte array.
byte[] writebuffer = Encoding.Unicode.GetBytes(testString);
uint len = Convert.ToUInt32(writebuffer.Length);
// Write the message ten times.
for (int i = 0; i < 10; i++)
{
MemoryStream response = _pc.WriteMessage(writebuffer, len);
if (response == null)
{
Debug.Assert(false,
"Failed to write message!");
}
else
{
WriteMessageResponse(response);
}
}
}
}
Console.WriteLine("Press Enter to exit...");
Console.ReadLine( );
}
static void WriteMessageResponse(MemoryStream responseStream)
{
string response =
Encoding.Unicode.GetString(responseStream.ToArray( ));
Console.WriteLine("Received response: {0}", response);
}
}
}
Example 16-6. Using the NamedPipeClient class (continued)