当前位置: 首页 > wzjs >正文

电商软件开发费用整站优化快速排名

电商软件开发费用,整站优化快速排名,东莞做网站需要多少钱,做任务挣钱的网站聚C#TCP通讯封装服务器工具类 1使用说明2封装 1使用说明 添加接受数据回调函数事件 方式1&#xff1a;通过有参构造函数添加 方式2&#xff1a;调用&#xff1a;public EventHandler<byte[]> AddEventToDataReceived 添加输出日志回调函数事件 方式1&#xff1a;通过有参构…

C#TCP通讯封装服务器工具类

  • 1使用说明
  • 2封装

1使用说明

  • 添加接受数据回调函数事件

方式1:通过有参构造函数添加
方式2:调用:public EventHandler<byte[]> AddEventToDataReceived

  • 添加输出日志回调函数事件

方式1:通过有参构造函数添加
方式2:调用:public Action<EMessage, IPEndPoint, int, string> AddEventToOutLog

  • 监听客户端发送数据线程向其他客户端转发消息的数据如何输出日志信息

方式:设置属性【OutputReceivedLog 】、设置属性【OutputReplyLog】
类型:

Null :不输出
Length:仅输出接受数据的长度信息
UTF8:UTF8-解析输出
ASCII:ASCII-解析输出
ByteToString:输出字节的字符串形式

2封装

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;namespace Tcp_Server
{public class ServerModel{private bool _firstStartServer = true;private TcpListener _server;private List<TcpClient> _tcpClientsList = new List<TcpClient>();/// <summary>/// 接受消息回调函数/// </summary>private EventHandler<byte[]> _callbackEventDataReceived;/// <summary>/// 日志信息回调函数/// int:线程ID/// </summary>private Action<EMessage, IPEndPoint, int, string> _callbackEventOutLog;public ServerModel(EventHandler<byte[]> callbackEventDataReceived, Action<EMessage, IPEndPoint, int, string> callbackEventOutLog){AddEventToDataReceived = callbackEventDataReceived;AddEventToOutLog = callbackEventOutLog;}public ServerModel(){}~ServerModel(){CloseServer();}/// <summary>/// 监听接入客户端/// </summary>private void ListeningClient(){TcpClient client = null;try{//等待客户端连接client = _server.AcceptTcpClient();AddLog(client, EMessage.State, "已连接");//为(每个)客户端创建线程处理通信new Task(() => HandleClient(client)).Start();}catch (Exception ex){AddLog(client, EMessage.Exception, $"ListeningClient: {ex.Message}");}}private void HandleClient(TcpClient client){try{NetworkStream stream = client.GetStream();_tcpClientsList.Add(client);byte[] buffer = new byte[1024];while (IsOpen){// 异步读取客户端发送的消息int bytesRead = stream.Read(buffer, 0, buffer.Length);if (bytesRead == 0){// 客户端断开连接break;}var receive = new byte[bytesRead];Array.Copy(buffer, receive, bytesRead);_callbackEventDataReceived?.Invoke(null, receive);PacketParseLog(EMessage.Receive, OutputReceivedLog, client, receive);//转发消息给其他客户端BroadcastMessage(client, buffer);}}catch (Exception ex){AddLog(client, EMessage.Exception, $"HandleClient: {ex.Message}");}}private void BroadcastMessage(TcpClient sender, byte[] buffer){int length = buffer.Length;foreach (TcpClient client in _tcpClientsList){if (client != sender && client.Connected){try{NetworkStream stream = client.GetStream();stream.Write(buffer, 0, length);PacketParseLog(EMessage.Reply, OutputReplyLog, client, buffer);}catch (Exception ex){AddLog(client, EMessage.Exception, $"BroadcastMessage: {ex.Message}");}}}}/// <summary>/// 解析数据包输出至日志/// </summary>/// <param name="messageType"></param>/// <param name="type"></param>/// <param name="client"></param>/// <param name="bytes"></param>private void PacketParseLog(EMessage messageType, ETranscoding type, TcpClient client, byte[] bytes){string message;switch (type){case ETranscoding.Length:message = bytes.Length.ToString();break;case ETranscoding.UTF8:message = Encoding.UTF8.GetString(bytes);break;case ETranscoding.ASCII:message = Encoding.ASCII.GetString(bytes);break;case ETranscoding.ByteToString:var builder = new StringBuilder();builder.Append("[ ");foreach (var b in bytes){builder.Append(Convert.ToString(b, 16).PadLeft(2, '0').ToUpper() + " ");}builder.Append("]");message = builder.ToString();break;default:return;}AddLog(client, messageType, message);}private void AddLog(TcpClient client, EMessage type, string description){int threadId = Thread.CurrentThread.ManagedThreadId;IPEndPoint ipEndPoint;if (client != null){ipEndPoint = (IPEndPoint)client.Client.RemoteEndPoint;}else{ipEndPoint = (IPEndPoint)_server.LocalEndpoint;}_callbackEventOutLog?.Invoke(type, ipEndPoint, threadId, description);}/********隔离线********隔离线********隔离线********隔离线********隔离线********隔离线********隔离线********/public EventHandler<byte[]> AddEventToDataReceived{set{if (value != null) _callbackEventDataReceived += value;}}public Action<EMessage, IPEndPoint, int, string> AddEventToOutLog{set{if (value != null) _callbackEventOutLog += value;}}/// <summary>/// 接受到客户端数据时输出日志信息的方式,默认:不输出/// </summary>public ETranscoding OutputReceivedLog { get; set; } = ETranscoding.Null;/// <summary>/// 转发消息Log时数据解析方式/// </summary>public ETranscoding OutputReplyLog { get; set; } = ETranscoding.Null;public bool StartServer(string ip, int port){try{CloseServer();//创建一个 TCP 监听器,监听本地 IP 地址和端口_server = new TcpListener(IPAddress.Parse(ip), port);// 启动服务器_server.Start();AddLog(null, EMessage.State, "服务器已启动");IsOpen = true;if (_firstStartServer){_firstStartServer = false;new Task(() =>{while (IsOpen){Thread.Sleep(1000);ListeningClient();}}).Start();}}catch (Exception ex){AddLog(null, EMessage.Exception, $"StartServer: {ex.Message}");}return IsOpen;}public void CloseServer(){if (IsOpen){// 停止服务器_server.Stop();}IsOpen = false;_server = null;}public bool IsOpen { get; private set; }public enum EMessage{[Description("状态信息")]State = 1,[Description("发送")]Send = 3,[Description("接受")]Receive = 5,[Description("异常")]Exception = 7,[Description("回复")]Reply = 9,}/// <summary>/// 接受到客户端数据时字节码解析方式,解析后输出至日志/// </summary>public enum ETranscoding{/// <summary>/// 不输出/// </summary>Null = 101,/// <summary>/// 仅输出接受数据的长度信息/// </summary>Length,/// <summary>/// UTF8-解析输出/// </summary>UTF8,/// <summary>/// ASCII-解析输出/// </summary>ASCII,/// <summary>/// 输出字节的字符串形式/// </summary>ByteToString}}
}

文章转载自:

http://4z7rymxi.Lqznq.cn
http://2UrTuYYU.Lqznq.cn
http://DeB5Tg7u.Lqznq.cn
http://enxoIoPf.Lqznq.cn
http://PAE6YR2W.Lqznq.cn
http://0nWaEM6Z.Lqznq.cn
http://sTmNnXqM.Lqznq.cn
http://gyxQlEQC.Lqznq.cn
http://G5yc436d.Lqznq.cn
http://wkKSgkmo.Lqznq.cn
http://LOEk4R9m.Lqznq.cn
http://vLFNB6gg.Lqznq.cn
http://vFcqqTle.Lqznq.cn
http://b0NdsLhO.Lqznq.cn
http://gKqTKbmb.Lqznq.cn
http://4933uTaj.Lqznq.cn
http://qfqHf5PA.Lqznq.cn
http://TLz2v7KM.Lqznq.cn
http://8qxBnHl9.Lqznq.cn
http://fmyzBtKs.Lqznq.cn
http://UKI2QqsM.Lqznq.cn
http://FSayqkrP.Lqznq.cn
http://U5oPKHNy.Lqznq.cn
http://s6T02uEu.Lqznq.cn
http://9qhWggQ7.Lqznq.cn
http://nhbx29jj.Lqznq.cn
http://K1L4uDmL.Lqznq.cn
http://PKhcHof9.Lqznq.cn
http://wj3dUsbk.Lqznq.cn
http://e0BjeVOf.Lqznq.cn
http://www.dtcms.com/wzjs/667442.html

相关文章:

  • 中国建设银行网站首页下载做公司网站需要多少钱
  • 怎样联系自己建设网站crm客户系统
  • 北京住房建设厅网站湖南中小企业建站价格
  • seo快速提高网站转化率vs网页设计教程
  • 一般做企业网站多少钱郑州汉狮哪家做网站好
  • 网站建设策划书模板快速网站建设公司哪家好
  • 西安建网站的公司网络推广员的工作内容
  • 一个完整的网站设计seo搜索优化是什么意思
  • 模板建站公司dede英文网站
  • 济南集团网站建设方案佛山市研发网站建设哪家好
  • 建设银行住房公积金预约网站北京网站建设新鸿
  • 百度网站搜索量提高案例学——网页设计与网站建设
  • 免费的网站推广 外贸今天的新闻头条最新消息
  • 怎么自己建设一个网站北理工网站开发与应用答案
  • 网站建设管理制度落实网站设计 图片
  • 麓谷做网站的公司二手车网站建设意见
  • 医院加强网站建设黄埔区建设局网站
  • 杭州做网站电话企业服务网站建设
  • 怎么创建一个视频网站深圳龙华建网站公司
  • 营销型网站建设应该考虑哪些因素电子商务网站设计与维护
  • 免费asp网站空间wordpress 是什么
  • 网站导航条设计欣赏如何给网站做dns解析
  • 呼伦贝尔市建设局网站南昌网站开发公司电话
  • 换个网站对seo有什么影响wordpress怎么去掉主题上的自豪
  • 网站标题设计ps工信部域名信息备案管理系统查询
  • 彩票网站建设制作价格无锡网页建站
  • 网站做最优是什么意思怎样做seo搜索引擎优化
  • 海淘一号 网站 怎么做的企业网站的价值体现是在
  • 域名对行业网站的作用弥勒建设局网站
  • 网站转跳怎么做阳信做网站