命名管道
服务端
using System;
using System.IO.Pipes;
using System.Text;
namespace Server
{
internal sealed class PipeServer
{
private readonly NamedPipeServerStream _pipe = new NamedPipeServerStream("Echo", PipeDirection.InOut, -1, PipeTransmissionMode.Message, PipeOptions.Asynchronous | PipeOptions.WriteThrough);
public PipeServer()
{
_pipe.BeginWaitForConnection(ClientConnected, null);
}
private void ClientConnected(IAsyncResult result)
{
new PipeServer();
_pipe.EndWaitForConnection(result);
byte[] data = new byte[1000];
_pipe.BeginRead(data, 0, data.Length, GotRequest, data);
}
private void GotRequest(IAsyncResult result)
{
Int32 bytesRead = _pipe.EndRead(result);
byte[] data = (byte[])result.AsyncState;
data = Encoding.UTF8.GetBytes(
Encoding.UTF8.GetString(data, 0, bytesRead).ToUpper().ToCharArray());
_pipe.BeginWrite(data, 0, data.Length, WriteDone, null);
}
private void WriteDone(IAsyncResult result)
{
_pipe.EndWrite(result);
_pipe.Close();
}
}
}
using System;
namespace Server
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < Environment.ProcessorCount; i++)
{
new PipeServer();
}
Console.WriteLine("Press <Enter> to terminate this server application.");
Console.ReadLine();
}
}
}
![启动服务端](https://img-blog.csdn.net/20150517112718827)
客户端
using System;
using System.IO.Pipes;
using System.Text;
namespace Client
{
internal class PipeClient
{
private readonly NamedPipeClientStream _pipe;
public PipeClient(string serverName, string message)
{
_pipe = new NamedPipeClientStream(serverName, "Echo", PipeDirection.InOut, PipeOptions.Asynchronous | PipeOptions.WriteThrough);
_pipe.Connect();
_pipe.ReadMode = PipeTransmissionMode.Message;
byte[] output = Encoding.UTF8.GetBytes(message);
_pipe.BeginWrite(output, 0, output.Length, WriteDone, null);
}
private void WriteDone(IAsyncResult result)
{
_pipe.EndWrite(result);
byte[] data=new byte[1000];
_pipe.BeginRead(data, 0, data.Length, GotResponse, data);
}
private void GotResponse(IAsyncResult result)
{
int bytesRead = _pipe.EndRead(result);
byte[] data = (byte[])result.AsyncState;
Console.WriteLine("Server response: " + Encoding.UTF8.GetString(data, 0, bytesRead));
_pipe.Close();
}
}
}
using System;
namespace Client
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 100; i++)
{
new PipeClient("localhost", "Request #" + i);
}
Console.ReadLine();
}
}
}
![启动客户端](https://img-blog.csdn.net/20150517112942913)
异常
using System;
using System.Net;
namespace ProcessException
{
class Program
{
static void Main(string[] args)
{
WebRequest webRequest = WebRequest.Create("http://0.0.0.0/");
webRequest.BeginGetResponse(ProcessWebResponse, webRequest);
Console.ReadLine();
}
private static void ProcessWebResponse(IAsyncResult result)
{
WebRequest webRequest = (WebRequest)result.AsyncState;
WebResponse response = null;
try
{
response = webRequest.EndGetResponse(result);
Console.WriteLine("Content Length: " + response.ContentLength);
}
catch (WebException we)
{
Console.WriteLine(we.GetType() + ": " + we.Message);
}
finally
{
if (response != null)
{
response.Close();
}
}
}
}
}
![异常信息](https://img-blog.csdn.net/20150517113008852)