Код сервера:
Синтаксис:
Используется csharp
using System;
using System.IO;
using System.IO.Pipes;
namespace NPipesManager
{
public class Server : IPipesEvent, IDisposable
{
public bool IsConnecting { get; set; }
private NamedPipeServerStream _server;
private const int MaxBuffSize = 10000000;
private byte[] _b;
static string _str = "";
public Server(string name)
{
_b = new byte[MaxBuffSize];
try
{
_server = new NamedPipeServerStream(
name,
PipeDirection.InOut,
10,
PipeTransmissionMode.Byte,
PipeOptions.Asynchronous
);
}
catch (Exception ex)
{
_str = string.Concat(DateTime.Now.ToLongTimeString(),"\n",ex.ToString());
}
_server.BeginWaitForConnection(OnConnect, _server);
}
#region IPipesEvent Members
public event OnReceive OnReceiveHandler = delegate { };
public void Fire(byte[] bb)
{
OnReceiveHandler(bb);
}
#endregion
private void OnConnect(IAsyncResult ar)
{
NamedPipeServerStream s = (NamedPipeServerStream) ar.AsyncState;
s.EndWaitForConnection(ar);
IsConnecting = s.IsConnected;
s.BeginRead(_b, 0, _b.Length, OnRead, s);
}
private void OnRead(IAsyncResult ar)
{
NamedPipeServerStream s = (NamedPipeServerStream)ar.AsyncState;
int c = s.EndRead(ar);
byte[] bb = new byte[c];
Array.Copy(_b, 0, bb, 0, c);
if (bb.Length > 0)
{
Fire(bb);
}
try
{
s.Flush();
s.BeginRead(_b, 0, _b.Length, OnRead, s);
}
catch(Exception ex)
{
_str = string.Concat(_str, Environment.NewLine, ex);
}
}
public void Dispose()
{
try
{
_server.Disconnect();
_server.Close();
_server.Dispose();
}
catch (Exception ex)
{
_str = string.Concat(_str, Environment.NewLine, ex);
File.WriteAllText("PipeServer_All_Error.txt", _str);
}
}
}
}
using System.IO;
using System.IO.Pipes;
namespace NPipesManager
{
public class Server : IPipesEvent, IDisposable
{
public bool IsConnecting { get; set; }
private NamedPipeServerStream _server;
private const int MaxBuffSize = 10000000;
private byte[] _b;
static string _str = "";
public Server(string name)
{
_b = new byte[MaxBuffSize];
try
{
_server = new NamedPipeServerStream(
name,
PipeDirection.InOut,
10,
PipeTransmissionMode.Byte,
PipeOptions.Asynchronous
);
}
catch (Exception ex)
{
_str = string.Concat(DateTime.Now.ToLongTimeString(),"\n",ex.ToString());
}
_server.BeginWaitForConnection(OnConnect, _server);
}
#region IPipesEvent Members
public event OnReceive OnReceiveHandler = delegate { };
public void Fire(byte[] bb)
{
OnReceiveHandler(bb);
}
#endregion
private void OnConnect(IAsyncResult ar)
{
NamedPipeServerStream s = (NamedPipeServerStream) ar.AsyncState;
s.EndWaitForConnection(ar);
IsConnecting = s.IsConnected;
s.BeginRead(_b, 0, _b.Length, OnRead, s);
}
private void OnRead(IAsyncResult ar)
{
NamedPipeServerStream s = (NamedPipeServerStream)ar.AsyncState;
int c = s.EndRead(ar);
byte[] bb = new byte[c];
Array.Copy(_b, 0, bb, 0, c);
if (bb.Length > 0)
{
Fire(bb);
}
try
{
s.Flush();
s.BeginRead(_b, 0, _b.Length, OnRead, s);
}
catch(Exception ex)
{
_str = string.Concat(_str, Environment.NewLine, ex);
}
}
public void Dispose()
{
try
{
_server.Disconnect();
_server.Close();
_server.Dispose();
}
catch (Exception ex)
{
_str = string.Concat(_str, Environment.NewLine, ex);
File.WriteAllText("PipeServer_All_Error.txt", _str);
}
}
}
}
Код клиента:
Синтаксис:
Используется csharp
using System;
using System.IO.Pipes;
using System.IO;
using System.Threading;
namespace NPipesManager
{
public class Client : IDisposable, IPipesEvent
{
public bool IsConnected { get; set; }
private readonly NamedPipeClientStream _client;
static string _str = "";
private const int MaxBuffSize = 256;
private readonly byte[] _b;
public Client(string serverName, string pipeName)
{
_client = new NamedPipeClientStream(
serverName,
pipeName,
PipeDirection.InOut,
PipeOptions.Asynchronous
);
_b = new byte[MaxBuffSize];
Connecting();
}
void Connecting()
{
try
{
_client.Connect();
}
catch (Exception ex)
{
_str = string.Concat(DateTime.Now.ToLongTimeString(),"\n");
_str = string.Concat(_str, ex.ToString());
}
}
#region IDisposable Members
public void Dispose()
{
try
{
if(_str!="")
File.WriteAllText("ERROR_named_pipe_client.txt", _str);
_client.Close();
_client.Dispose();
}
catch (Exception ex)
{
string str = DateTime.Now.ToLongTimeString() + Environment.NewLine;
str = string.Concat(str, ex.ToString());
File.WriteAllText("ERROR_DISPOSE_named_pipe_client.txt", str);
}
}
#endregion
public void Send(byte[] b)
{
IsConnected = _client.IsConnected;
if (!IsConnected)
{
Connecting();
}
try
{
_client.BeginWrite(b, 0, b.Length, OnWrite, _client);
}
catch(Exception ex)
{
_str = string.Concat(_str, "\n", ex.ToString());
}
}
private static void OnWrite(IAsyncResult ar)
{
NamedPipeClientStream c = (NamedPipeClientStream) ar.AsyncState;
try
{
c.EndWrite(ar);
c.Flush();
c.WaitForPipeDrain();
}
catch(IOException ex)
{
_str = string.Concat(_str,"\n",ex.ToString());
}
}
public event OnReceive OnReceiveHandler = delegate { };
public void Fire(byte[] bb)
{
OnReceiveHandler(bb);
}
}
}
using System.IO.Pipes;
using System.IO;
using System.Threading;
namespace NPipesManager
{
public class Client : IDisposable, IPipesEvent
{
public bool IsConnected { get; set; }
private readonly NamedPipeClientStream _client;
static string _str = "";
private const int MaxBuffSize = 256;
private readonly byte[] _b;
public Client(string serverName, string pipeName)
{
_client = new NamedPipeClientStream(
serverName,
pipeName,
PipeDirection.InOut,
PipeOptions.Asynchronous
);
_b = new byte[MaxBuffSize];
Connecting();
}
void Connecting()
{
try
{
_client.Connect();
}
catch (Exception ex)
{
_str = string.Concat(DateTime.Now.ToLongTimeString(),"\n");
_str = string.Concat(_str, ex.ToString());
}
}
#region IDisposable Members
public void Dispose()
{
try
{
if(_str!="")
File.WriteAllText("ERROR_named_pipe_client.txt", _str);
_client.Close();
_client.Dispose();
}
catch (Exception ex)
{
string str = DateTime.Now.ToLongTimeString() + Environment.NewLine;
str = string.Concat(str, ex.ToString());
File.WriteAllText("ERROR_DISPOSE_named_pipe_client.txt", str);
}
}
#endregion
public void Send(byte[] b)
{
IsConnected = _client.IsConnected;
if (!IsConnected)
{
Connecting();
}
try
{
_client.BeginWrite(b, 0, b.Length, OnWrite, _client);
}
catch(Exception ex)
{
_str = string.Concat(_str, "\n", ex.ToString());
}
}
private static void OnWrite(IAsyncResult ar)
{
NamedPipeClientStream c = (NamedPipeClientStream) ar.AsyncState;
try
{
c.EndWrite(ar);
c.Flush();
c.WaitForPipeDrain();
}
catch(IOException ex)
{
_str = string.Concat(_str,"\n",ex.ToString());
}
}
public event OnReceive OnReceiveHandler = delegate { };
public void Fire(byte[] bb)
{
OnReceiveHandler(bb);
}
}
}