APM_01

命名管道

服务端

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)
        {
            //每个CPU启动一个服务器
            for (int i = 0; i < Environment.ProcessorCount; i++)
            {
                new PipeServer();
            }
            Console.WriteLine("Press <Enter> to terminate this server application.");
            Console.ReadLine();
        }      
    }
}

启动服务端

客户端

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();//必须先连接才能设置ReadMode
            _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)
        {
            //现在向服务器发出100个客户端请求
            for (int i = 0; i < 100; i++)
            {
                new PipeClient("localhost", "Request #" + i);
            }
            //由于所有请求都是异步发出的,所以构造器可能在全部请求都完成之前返回,
            //以下调用用于避免应有程序在我们看到全部响应之前终止
            Console.ReadLine();
        }
    }
}

启动客户端

异常

using System;
using System.Net;

namespace ProcessException
{
    class Program
    {
        static void Main(string[] args)
        {
            //尝试访问一个无效的IP地址
            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();
                }
            }
        }
    }
}

异常信息

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值