Named Pipe Communcation C#

  • Thread starter Thread starter williamtholmes
  • Start date Start date
W

williamtholmes

Guest
Hello,


I am working on a client server process that uses named pipes to communicate. I used an MSDN example a starting point. What I am trying to do that is not working is verify both the server to the client and then the client to the server.

In the client code below I am first reading the server's signature and if its correct I am sending the client signature to the server with this:


if (serverMessage.Equals("MyPipeServer"))
{
try
{
Console.WriteLine("Trying to tell the server who we are");
ss.WriteString("MyPipeClient");


Then on the server I have following code to read the client signature:

StreamString ss = new StreamString(pipeServer);
// Verify our identity to the connected client using a
// string that the client anticipates.
ss.WriteString("MyPipeServer");
string readString = ss.ReadString();
if (readString == "MyPipeClient")
{



The client receives the Server Signature but when the server reads (or attempts to read) the client signature using: string readString = ss.ReadString();


A null value is returned rather than the signature I sent out from the client using:

ss.WriteString("MyPipeClient");

Can anyone tell me what I might be doing wrong with this. Again I started with an example that I downloaded from MSDN at: How to: Use Named Pipes for Network Interprocess Communication


Thanks



Here is the client code Client Code:

using System;
using System.Collections;
using System.Data;
using System.IO;
using System.IO.Pipes;
using System.Text;
using System.Security.Principal;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;
namespace Pip
{
public class PipeClient
{
public static void Main(string[] Args)
{
NamedPipeClientStream pipeClient = new NamedPipeClientStream(
".",
"MyPipe",
PipeDirection.InOut,
PipeOptions.None,
TokenImpersonationLevel.Impersonation
);
Console.WriteLine("Connecting to server...\n");
pipeClient.Connect();
StreamString ss = new StreamString(pipeClient);
// Validate the server's signature string
string serverMessage = ss.ReadString();
Console.WriteLine("The server identified itself with this string: {0}", serverMessage);
if (serverMessage.Equals("MyPipeServer"))
{
try
{
Console.WriteLine("Trying to tell the server who we are");
ss.WriteString("MyPipeClient");
PSCommand zz = new PSCommand();
zz.command = "A-Command";
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(pipeClient, zz);
}
catch (Exception exp)
{
Console.WriteLine(exp.Message);
}
}
else
{
Console.WriteLine("Server could not be verified.");
}
pipeClient.Close();
}

{
private Stream ioStream;
private UnicodeEncoding streamEncoding;
public StreamString(Stream ioStream)
{
this.ioStream = ioStream;
streamEncoding = new UnicodeEncoding();
}
public string ReadString()
{
int len;
len = ioStream.ReadByte() * 256;
len += ioStream.ReadByte();
byte[] inBuffer = new byte[len];
ioStream.Read(inBuffer, 0, len);
return streamEncoding.GetString(inBuffer);
}
public int WriteString(string outString)
{
byte[] outBuffer = streamEncoding.GetBytes(outString);
int len = outBuffer.Length;
if (len > UInt16.MaxValue)
{
len = (int)UInt16.MaxValue;
}
ioStream.WriteByte((byte)(len / 256));
ioStream.WriteByte((byte)(len & 255));
ioStream.Write(outBuffer, 0, len);
ioStream.Flush();
return outBuffer.Length + 2;
}
}
}
[Serializable]
public class PSCommand
{
public string command
{
get;
set;
}
public bool confirm
{
get;
set;
}
public Hashtable commandArguments
{
get;
set;
}
public DataTable commandResultsTable
{
get;
set;
}
public Hashtable commandResults
{
get;
set;
}
}
}

Here is the server Code:


using System;
using System.Collections;
using System.Data;
using System.IO;
using System.IO.Pipes;
using System.Security.AccessControl;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading;
using Pip;
public class PipeServer
{
private static int numThreads = 4;
public static void Main()
{
int i;
Thread[] servers = new Thread[numThreads];
Console.WriteLine("Waiting for client connect...\n");
for (i = 0; i < numThreads; i++)
{
servers = new Thread(ServerThread);
servers.Start();
}
Thread.Sleep(250);
while (i > 0)
{
for (int j = 0; j < numThreads; j++)
{
if (servers[j] != null)
{
if (servers[j].Join(250))
{
Console.WriteLine("Server thread[{0}] finished.", servers[j].ManagedThreadId);
servers[j] = null;
i--; // decrement the thread watch count
}
}
}
}
Console.WriteLine("\nServer threads exhausted, exiting.");
}
private static void ServerThread(object data)
{
PipeSecurity ps = new PipeSecurity();
ps.AddAccessRule(new PipeAccessRule("Users", PipeAccessRights.ReadWrite | PipeAccessRights.CreateNewInstance, AccessControlType.Allow));
ps.AddAccessRule(new PipeAccessRule("CREATOR OWNER", PipeAccessRights.FullControl, AccessControlType.Allow));
ps.AddAccessRule(new PipeAccessRule("SYSTEM", PipeAccessRights.FullControl, AccessControlType.Allow));
NamedPipeServerStream pipeServer = new NamedPipeServerStream(
"MyPipe",
PipeDirection.InOut,
numThreads,
PipeTransmissionMode.Byte,
PipeOptions.WriteThrough,
0,
0,
ps);
int threadId = Thread.CurrentThread.ManagedThreadId;
// Wait for a client to connect
pipeServer.WaitForConnection();
Console.WriteLine("Client connected on thread[{0}].", threadId);
try
{
// Read the request from the client. Once the client has
// written to the pipe its security token will be available.
StreamString ss = new StreamString(pipeServer);
// Verify our identity to the connected client using a
// string that the client anticipates.
ss.WriteString("MyPipeServer");
string readString = ss.ReadString();
if (readString == "MyPipeClient")
{
IFormatter formatter = new BinaryFormatter();
PSCommand zz = (PSCommand)formatter.Deserialize(pipeServer);
Console.WriteLine("Received: " + zz.command + " from client..." );
}
else
{
Console.WriteLine("ERROR: {0}", "Invalid Client");
}
}
// Catch the IOException that is raised if the pipe is broken
// or disconnected.
catch (IOException e)
{
Console.WriteLine("ERROR: {0}", e.Message);
}
pipeServer.Close();
}
}
// Defines the data protocol for reading and writing strings on our stream
public class StreamString
{
private Stream ioStream;
private UnicodeEncoding streamEncoding;
public StreamString(Stream ioStream)
{
this.ioStream = ioStream;
streamEncoding = new UnicodeEncoding();
}
public string ReadString()
{
int len = 0;
len = ioStream.ReadByte() * 256;
if (len > 0)
{
len += ioStream.ReadByte();
byte[] inBuffer = new byte[len];
ioStream.Read(inBuffer, 0, len);
return streamEncoding.GetString(inBuffer);
}
else
{
return null;
}
}
public int WriteString(string outString)
{
try
{
byte[] outBuffer = streamEncoding.GetBytes(outString);
int len = outBuffer.Length;
if (len > UInt16.MaxValue)
{
len = (int)UInt16.MaxValue;
}
ioStream.WriteByte((byte)(len / 256));
ioStream.WriteByte((byte)(len & 255));
ioStream.Write(outBuffer, 0, len);
ioStream.Flush();
return outBuffer.Length + 2;
}
catch (Exception exp)
{
throw exp;
}
}
}

Continue reading...
 

Similar threads

P
Replies
0
Views
176
Policy standard local admin account with Active Di
P
Back
Top