APM_01

本文详细介绍了如何使用C#实现基于命名管道的并发服务器与客户端,包括服务器端监听连接、读取和响应客户端请求的过程,以及客户端如何发起请求并接收响应。实现了每个CPU启动一个服务器实例,支持高并发请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

命名管道

服务端

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();
                }
            }
        }
    }
}

异常信息

逐行解析一下这些打印XOS#2025/05/28 16:44:15 informational: WMAC_AC: Dis-assocation frame received from station with MAC address 92:aa:0e:84:c5:22 and reason code 3 2025/05/28 16:44:15 informational: WMAC_AC: Sending DEL-STA to AP 1 by CAPWAP with station MAC address 92:aa:0e:84:c5:22 BSSID 6c:ef:c6:65:ab:d1 2025/05/28 16:44:15 notifications: WMAC_AC: [IPC] Sending DELETE-STA to AP by CAPWAP with station MAC 92:aa:0e:84:c5:22. asso seq [0x1000] 2025/05/28 16:44:15 errors : WMAC_AC: Failed to notify L2F to delete sta with MAC address 92:aa:0e:84:c5:22 | vlan 1 | dynamic vlan -1. 2025/05/28 16:44:15 informational: WMAC_AC: STA 92:aa:0e:84:c5:22 - event 2 notification 2025/05/28 16:44:15 informational: WMAC_AC: Unauthorizing port for station 92:aa:0e:84:c5:22. 2025/05/28 16:44:15 informational: WMAC_AC: STA 92:aa:0e:84:c5:22 - MLME-DISASSOCIATE.indication(92:aa:0e:84:c5:22, 3) 2025/05/28 16:44:15 informational: WMAC_AC: Receiving authention frame from station 92:07:d3:da:91:62. 2025/05/28 16:44:15 informational: WMAC_AC: Receiving authention frame from station 92:07:d3:da:91:62 in bssid 6c:ef:c6:65:ab:d7. auth_alg 0, auth_transaction 1 2025/05/28 16:44:15 informational: WMAC_AC: Authentication OK (Open-System) with bssid 6c:ef:c6:65:ab:d7 . 2025/05/28 16:44:15 informational: WMAC_AC: Receiving association request from sta 92:07:d3:da:91:62 in bssid 6c:ef:c6:65:ab:d7 2025/05/28 16:44:15 warnings : WMAC_AC: IEEE 802.11 element parse ignored unknown element (id=191 elen=12) 2025/05/28 16:44:15 informational: WMAC_AC: Station association succeed with AID: 1, SSID: a-wangjian-portal, BSSID: 6c:ef:c6:65:ab:d7. 2025/05/28 16:44:15 informational: WMAC_AC: Vlan 1 station increasing, count 3. 2025/05/28 16:44:15 notifications: WMAC_AC: [IPC] Sending ADD-STATION to AP by CAPWAP with station MAC 92:07:d3:da:91:62, AID 1 and APID 1 RID 2. assoseq [0x1000] 2025/05/28 16:44:15 informational: APM: APM_Cloud_Server_Sendto(line:3313):send sta online 2025/05/28 16:44:15 informational: APM: APM_Cloud_Server_Sendto(line:3319):sta mac:92:07:d3:da:91:62 2025/05/28 16:44:15 informational: APM: APM_Cloud_Server_Sendto(line:3324):AP mac:6c:ef:c6:65:ab:c0 2025/05/28 16:44:15 errors : APM: APM_WmacSocket_Recv_Msg(line:975):!!! RECV MSG WMAC_GET_TRAFFIC_LIMIT, StaMac[92:07:d3:da:91:62] 2025/05/28 16:44:15 errors : APM: APM_SendStaTraffic(line:794):APM_SendStaTraffic uplimit 0, downlimit 0 2025/05/28 16:44:15 debugging : APM: APM_WMAC_SendStaTrafficLimit(line:1059):APM_WMAC_SendStaTrafficLimit success 2025/05/28 16:44:15 informational: APM: APM_DecMsgHead(line:71):MessageType 8 MessageLen 50 2025/05/28 16:44:15 informational: APM: APM_Recv_Pkt_Handle(line:3467):Recv MsgType: STA_AUTH_SUCCESS 2025/05/28 16:44:15 informational: APM: APM_DecPkt_Data(line:2970):store id :1 2025/05/28 16:44:15 informational: APM: APM_DecPkt_Data(line:2829):Sta mac:92:07:d3:da:91:62 2025/05/28 16:44:15 informational: APM: APM_DecPkt_Data(line:2708):AP mac:6c:ef:c6:65:ab:c0 2025/05/28 16:44:15 informational: APM: APM_DecPkt_Data(line:3061):usProfileId 2 2025/05/28 16:44:15 informational: APM: APM_DecPkt_Data(line:2855):STA pass time:586 2025/05/28 16:44:15 informational: APM: APM_DecPkt_Data(line:3206):APM Portal username:admin 2025/05/28 16:44:15 informational: APM: APM_RecvPktProcess(line:2509):[EasyPortal] user $MAC:92:7:d3:da:91:62 auth success, pass time is 600 s 2025/05/28 16:44:15 informational: APM: APM_AddUserNode(line:836):APM_AddUserNode:: User Node is exist (mac = 9207.d3da.9162) 2025/05/28 16:44:15 informational: APM: APM_AddUserNode(line:851):Create user Node timer 2025/05/28 16:44:15 errors : APM: APM_SendStaTraffic(line:794):APM_SendStaTraffic uplimit 0, downlimit 0 2025/05/28 16:44:15 debugging : APM: APM_WMAC_SendStaTrafficLimit(line:1059):APM_WMAC_SendStaTrafficLimit success 2025/05/28 16:44:15 informational: APM: APM_DecMsgHead(line:71):MessageType 25 MessageLen 54 2025/05/28 16:44:15 informational: APM: APM_Recv_Pkt_Handle(line:3467):Recv MsgType: User trafic limit 2025/05/28 16:44:15 informational: APM: APM_DecPkt_Data(line:2970):store id :1 2025/05/28 16:44:15 informational: APM: APM_DecPkt_Data(line:2829):Sta mac:92:07:d3:da:91:62 2025/05/28 16:44:15 informational: APM: APM_DecPkt_Data(line:2732):AP input limit:0 2025/05/28 16:44:15 informational: APM: APM_DecPkt_Data(line:2738):AP output limit:0 2025/05/28 16:44:15 informational: APM: APM_DecPkt_Data(line:2708):AP mac:6c:ef:c6:65:ab:c0 2025/05/28 16:44:15 debugging : APM: APM_SetStaTraffic(line:2399):STA[92:07:d3:da:91:62] inputlimit 0 outlimit 0 2025/05/28 16:44:15 informational: APM: APM_DecMsgHead(line:71):MessageType 7 MessageLen 53 2025/05/28 16:44:15 informational: APM: APM_Recv_Pkt_Handle(line:3467):Recv MsgType: STA_ONLINE_RESP 2025/05/28 16:44:15 informational: APM: APM_DecPkt_Data(line:2970):store id :1 2025/05/28 16:44:15 informational: APM: APM_DecPkt_Data(line:2829):Sta mac:92:07:d3:da:91:62 2025/05/28 16:44:15 informational: APM: APM_DecPkt_Data(line:2708):AP mac:6c:ef:c6:65:ab:c0 2025/05/28 16:44:15 informational: APM: APM_DecPkt_Data(line:2843):STA Privilege:0 2025/05/28 16:44:15 informational: APM: APM_DecPkt_Data(line:3061):usProfileId 2 2025/05/28 16:44:15 informational: APM: APM_DecPkt_Data(line:2849):STA Auth Mode:1 2025/05/28 16:44:15 informational: APM: APM_DecPkt_Data(line:2855):STA pass time:600 2025/05/28 16:44:15 informational: APM: APM_RecvPktProcess(line:2500):sta online response 2025/05/28 16:44:15 informational: APM: APM_UpdateUserNode(line:702):APM_AddUserNode:: User Node is exist (mac = 9207.d3da.9162) 2025/05/28 16:44:15 informational: APM: APM_UpdateUserNode(line:752):APM_AddUserNode:: Create user Node timer 2025/05/28 16:44:16 informational: WMAC_AC: STA 92:aa:0e:84:c5:22 - deauthenticated due to inactivity 2025/05/28 16:44:16 informational: WMAC_AC: STA 92:aa:0e:84:c5:22 - MLME-DEAUTHENTICATE.indication(92:aa:0e:84:c5:22, 2) 2025/05/28 16:44:16 informational: WMAC_AC: Trying to free station with MAC address 92:aa:0e:84:c5:22 and BSSID 6c:ef:c6:65:ab:d1. 2025/05/28 16:44:16 informational: WMAC_AC: Vlan 1 station degression, count 2. 2025/05/28 16:44:16 informational: WMAC_AC: Sending DEL-STA to AP 1 by CAPWAP with station MAC address 92:aa:0e:84:c5:22 BSSID 6c:ef:c6:65:ab:d1 2025/05/28 16:44:16 notifications: WMAC_AC: [IPC] Sending DELETE-STA to AP by CAPWAP with station MAC 92:aa:0e:84:c5:22. asso seq [0x1000] 2025/05/28 16:44:16 errors : WMAC_AC: Failed to notify L2F to delete sta with MAC address 92:aa:0e:84:c5:22 | vlan 1 | dynamic vlan -1.
最新发布
05-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值