參考資料 http://www.javashuo.com/article/p-kwrvcmrv-db.htmlhtml
根據.NET Socket 簡單實用框架進行了改造,這個代碼對socket通訊封裝仍是不錯的。簡單,邏輯清晰,資料中的代碼惟一問題發送信息很頻繁,會致使接收信息發生問題。改造後的代碼以下git
服務端源文件:json
SocketServer.cs
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; namespace Face.SocketServer { /// <summary> /// Socket服務端 /// </summary> public class SocketServer { #region 內部成員 private Socket _socket = null; private bool _isListen = true; public static ManualResetEvent allDone = new ManualResetEvent(false); private void StartListen() { try { _socket.BeginAccept(asyncResult => { try { Socket newSocket = _socket.EndAccept(asyncResult); //立刻進行下一輪監聽,增長吞吐量 if (_isListen) StartListen(); SocketConnection newClient = new SocketConnection(newSocket, this) { HandleRecMsg = HandleRecMsg == null ? null : new Action<string, SocketConnection, SocketServer>(HandleRecMsg), HandleClientClose = HandleClientClose == null ? null : new Action<SocketConnection, SocketServer>(HandleClientClose), HandleSendMsg = HandleSendMsg == null ? null : new Action<byte[], SocketConnection, SocketServer>(HandleSendMsg), HandleException = HandleException == null ? null : new Action<Exception>(HandleException), HandleSendException= HandleSendException == null ? null : new Action<Exception>(HandleSendException) }; newClient.StartRecMsg(); ClientList.AddLast(newClient); HandleNewClientConnected?.Invoke(this, newClient); } catch (Exception ex) { HandleException?.Invoke(ex); } }, null); } catch (Exception ex) { HandleException?.Invoke(ex); } } #endregion #region 外部接口 /// <summary> /// 開始服務,監聽客戶端 /// </summary> public void StartServer() { try { //實例化套接字(ip4尋址協議,流式傳輸,TCP協議) _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //建立ip對象 IPAddress address = IPAddress.Parse(ApplicationConfig.Ins.SocketIP); //建立網絡節點對象包含ip和port IPEndPoint endpoint = new IPEndPoint(address, ApplicationConfig.Ins.SocketPort); //將 監聽套接字綁定到 對應的IP和端口 _socket.Bind(endpoint); //設置監聽隊列長度爲Int32最大值(同時可以處理鏈接請求數量) _socket.Listen(int.MaxValue); //開始監聽客戶端 StartListen(); HandleServerStarted?.Invoke(this); } catch (Exception ex) { StartException?.Invoke(ex); } } /// <summary> /// 全部鏈接的客戶端列表 /// </summary> public LinkedList<SocketConnection> ClientList { get; set; } = new LinkedList<SocketConnection>(); /// <summary> /// 關閉指定客戶端鏈接 /// </summary> /// <param name="theClient">指定的客戶端鏈接</param> public void CloseClient(SocketConnection theClient) { theClient.Close(); } #endregion #region 公共事件 /// <summary> /// 異常處理程序 /// </summary> public Action<Exception> HandleException { get; set; } /// <summary> /// 發送消息異常處理程序 /// </summary> public Action<Exception> HandleSendException { get; set; } /// <summary> /// 啓動異常程序 /// </summary> public Action<Exception> StartException { get; set; } #endregion #region 服務端事件 /// <summary> /// 服務啓動後執行 /// </summary> public Action<SocketServer> HandleServerStarted { get; set; } /// <summary> /// 當新客戶端鏈接後執行 /// </summary> public Action<SocketServer, SocketConnection> HandleNewClientConnected { get; set; } /// <summary> /// 服務端關閉客戶端後執行 /// </summary> public Action<SocketServer, SocketConnection> HandleCloseClient { get; set; } #endregion #region 客戶端鏈接事件 /// <summary> /// 客戶端鏈接接受新的消息後調用 /// </summary> public Action<string, SocketConnection, SocketServer> HandleRecMsg { get; set; } /// <summary> /// 客戶端鏈接發送消息後回調 /// </summary> public Action<byte[], SocketConnection, SocketServer> HandleSendMsg { get; set; } /// <summary> /// 客戶端鏈接關閉後回調 /// </summary> public Action<SocketConnection, SocketServer> HandleClientClose { get; set; } #endregion } }
using System; using System.Collections.Generic; using System.Linq; using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; namespace Face.SocketServer { /// <summary> /// Socket鏈接,雙向通訊 /// </summary> public class SocketConnection { #region 構造函數 public SocketConnection(Socket socket, SocketServer server) { _socket = socket; _server = server; } #endregion #region 私有成員 private readonly Socket _socket; private bool _isRec = true; private SocketServer _server = null; private bool IsSocketConnected() { bool part1 = _socket.Poll(1000, SelectMode.SelectRead); bool part2 = (_socket.Available == 0); if (part1 && part2) return false; else return true; } #endregion #region 外部接口 /// <summary> /// 開始接受客戶端消息 /// </summary> public void StartRecMsg() { try { byte[] container = new byte[1024 * 1024 * 6]; _socket.BeginReceive(container, 0, container.Length, SocketFlags.None, asyncResult => { try { int length = _socket.EndReceive(asyncResult); ///asyncResult.AsyncWaitHandle.Close(); //立刻進行下一輪接受,增長吞吐量 if (length > 0 && _isRec && IsSocketConnected()) StartRecMsg(); if (length > 0) { byte[] recBytes = new byte[length]; Array.Copy(container, 0, recBytes, 0, length); string msgJson = Encoding.UTF8.GetString(recBytes); if (msgJson.Contains("¤€") && msgJson.Contains("€¤")) { string[] arrymsg = msgJson.Replace("¤€", "卍").Split('卍'); foreach (string msgitem in arrymsg) { if (string.IsNullOrEmpty(msgitem)) continue; if (msgitem.Substring(msgitem.Length - 2, 2) == "€¤") { string msgitemjson = msgitem.Substring(0, msgitem.Length - 2); //處理消息 HandleRecMsg?.Invoke(msgitemjson, this, _server); } } } else { HandleException?.Invoke(new Exception($"接收到錯誤指令,具體指令爲:{msgJson}")); } } else Close(); } catch (Exception ex) { HandleException?.Invoke(ex); Close(); } }, null); } catch (Exception ex) { HandleException?.Invoke(ex); Close(); } } /// <summary> /// 發送數據 /// </summary> /// <param name="bytes">數據字節</param> private void Send(byte[] bytes) { try { _socket.BeginSend(bytes, 0, bytes.Length, SocketFlags.None, asyncResult => { try { int length = _socket.EndSend(asyncResult); HandleSendMsg?.Invoke(bytes, this, _server); } catch (Exception ex) { HandleSendException?.Invoke(ex); } }, null); } catch (Exception ex) { HandleSendException?.Invoke(ex); } } /// <summary> /// 發送字符串(默認使用UTF-8編碼) /// </summary> /// <param name="msgStr">字符串</param> public void Send(string msgStr) { Send(Encoding.UTF8.GetBytes("¤€" + msgStr + "€¤")); } /// <summary> /// 發送字符串(使用自定義編碼) /// </summary> /// <param name="msgStr">字符串消息</param> /// <param name="encoding">使用的編碼</param> public void Send(string msgStr, Encoding encoding) { Send(encoding.GetBytes("¤€" + msgStr + "€¤")); } /// <summary> /// 傳入自定義屬性 /// </summary> public object Property { get; set; } /// <summary> /// 關閉當前鏈接 /// </summary> public void Close() { try { _isRec = false; _socket.Disconnect(false); _server.ClientList.Remove(this); HandleClientClose?.Invoke(this, _server); _socket.Close(); _socket.Dispose(); GC.Collect(); } catch (Exception ex) { HandleException?.Invoke(ex); } } #endregion #region 事件處理 /// <summary> /// 客戶端鏈接接受新的消息後調用 /// </summary> public Action<string, SocketConnection, SocketServer> HandleRecMsg { get; set; } /// <summary> /// 客戶端鏈接發送消息後回調 /// </summary> public Action<byte[], SocketConnection, SocketServer> HandleSendMsg { get; set; } /// <summary> /// 客戶端鏈接關閉後回調 /// </summary> public Action<SocketConnection, SocketServer> HandleClientClose { get; set; } /// <summary> /// 異常處理程序 /// </summary> public Action<Exception> HandleException { get; set; } /// <summary> /// 發送消息到客戶端異常 /// </summary> public Action<Exception> HandleSendException { get; set; } #endregion } }
上面放上的是框架代碼,接下來介紹下如何使用服務器
首先,服務端使用方式:網絡
//建立服務器對象,默認監聽本機0.0.0.0,端口12345 SocketServer server = new SocketServer(); faceLogic = new FaceLogic(); //處理從客戶端收到的消息 server.HandleRecMsg = new Action<string, SocketConnection, SocketServer>((msg, client, theServer) => { //Log($"調用次數", true); lock (obj) { paramList.Add(new ParamEntity() { ParamFace = msg, socketClient = client }); if (IsRun == false) { IsRun = true; HandleMsg(); } } }); //處理服務器啓動後事件 server.HandleServerStarted = new Action<SocketServer>(theServer => { Log("服務已啓動************", true); }); //處理新的客戶端鏈接後的事件 server.HandleNewClientConnected = new Action<SocketServer, SocketConnection>((theServer, theCon) => { Log($@"一個新的客戶端接入,當前鏈接數:{theServer.ClientList.Count}", true); }); //處理客戶端鏈接關閉後的事件 server.HandleClientClose = new Action<SocketConnection, SocketServer>((theCon, theServer) => { Log($@"一個客戶端關閉,當前鏈接數爲:{theServer.ClientList.Count}", true); }); //處理異常 server.HandleException = new Action<Exception>(ex => { Log("Socket處理異常:" + ex.Message, true); Logs.Instance.Error("Socket處理異常:" + ex.Message); }); //處理異常 server.HandleSendException = new Action<Exception>(ex => { Log("Socket發送消息處理異常:" + ex.Message, true); Logs.Instance.Error("Socket發送消息處理異常:" + ex.Message); }); ///啓動異常 server.StartException = new Action<Exception>(ex => { Log("Socket服務啓動失敗:" + ex.Message, true); Logs.Instance.Error("Socket服務啓動失敗:" + ex.Message); }); //服務器啓動 server.StartServer(); Log("啓動Socket通訊服務端完成。");
客戶端使用方式:框架
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; namespace Face.SocketClient { /// <summary> /// Socket客戶端 /// </summary> public class SocketClient { #region 內部成員 private Socket _socket = null; private bool _isRec = true; private bool _IsRun = false; public bool IsRun { get { return _IsRun; } } private bool IsSocketConnected() { bool part1 = _socket.Poll(1000, SelectMode.SelectRead); bool part2 = (_socket.Available == 0); if (part1 && part2) return false; else return true; } /// <summary> /// 開始接受客戶端消息 /// </summary> public void StartRecMsg() { try { byte[] container = new byte[1024 * 1024 * 2]; _socket.BeginReceive(container, 0, container.Length, SocketFlags.None, asyncResult => { try { int length = _socket.EndReceive(asyncResult); //立刻進行下一輪接受,增長吞吐量 if (length > 0 && _isRec && IsSocketConnected()) StartRecMsg(); if (length > 0) { byte[] recBytes = new byte[length]; Array.Copy(container, 0, recBytes, 0, length); string msgJson = Encoding.UTF8.GetString(recBytes); if (msgJson.Contains("¤€") && msgJson.Contains("€¤")) { string[] arrymsg = msgJson.Replace("¤€", "卍").Split('卍'); foreach (string msgitem in arrymsg) { if (string.IsNullOrEmpty(msgitem)) continue; if (msgitem.Substring(msgitem.Length - 2, 2) == "€¤") { string msgitemjson = msgitem.Substring(0, msgitem.Length - 2); //處理消息 HandleRecMsg?.Invoke(msgitemjson, this); } } } else { HandleException?.Invoke(new Exception($"接收到錯誤指令,具體指令爲:{msgJson}")); } } else Close(); } catch (Exception ex) { if (ex.Message.Contains("遠程主機強迫關閉")) { _IsRun = false; } HandleException?.Invoke(ex); Close(); } }, null); } catch (Exception ex) { if (ex.Message.Contains("遠程主機強迫關閉")) { _IsRun = false; } HandleException?.Invoke(ex); Close(); } } #endregion #region 外部接口 /// <summary> /// 開始服務,鏈接服務端 /// </summary> public void StartClient() { try { //實例化 套接字 (ip4尋址協議,流式傳輸,TCP協議) _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //建立 ip對象 IPAddress address = IPAddress.Parse(ApplicationConfig.Ins.ServerIP); //建立網絡節點對象 包含 ip和port IPEndPoint endpoint = new IPEndPoint(address, ApplicationConfig.Ins.ServerPort); //將 監聽套接字 綁定到 對應的IP和端口 _socket.BeginConnect(endpoint, asyncResult => { try { _socket.EndConnect(asyncResult); //開始接受服務器消息 StartRecMsg(); _IsRun = true; HandleClientStarted?.Invoke(this); } catch (Exception ex) { _IsRun = false; StartException?.Invoke(ex); } }, null); } catch (Exception ex) { _IsRun = false; StartException?.Invoke(ex); } } /// <summary> /// 發送數據 /// </summary> /// <param name="bytes">數據字節</param> private void Send(byte[] bytes) { try { //Thread.Sleep(250); _socket.BeginSend(bytes, 0, bytes.Length, SocketFlags.None, asyncResult => { try { int length = _socket.EndSend(asyncResult); HandleSendMsg?.Invoke(bytes, this); } catch (Exception ex) { HandleException?.Invoke(ex); } }, null); } catch (Exception ex) { HandleException?.Invoke(ex); } } /// <summary> /// 發送字符串(默認使用UTF-8編碼) /// </summary> /// <param name="msgStr">字符串</param> public void Send(string msgStr) { Send(Encoding.UTF8.GetBytes("¤€" + msgStr+ "€¤")); } /// <summary> /// 發送字符串(使用自定義編碼) /// </summary> /// <param name="msgStr">字符串消息</param> /// <param name="encoding">使用的編碼</param> public void Send(string msgStr, Encoding encoding) { Send(encoding.GetBytes("¤€" + msgStr + "€¤")); } /// <summary> /// 傳入自定義屬性 /// </summary> public object Property { get; set; } /// <summary> /// 關閉與服務器的鏈接 /// </summary> public void Close() { try { _isRec = false; _socket.Disconnect(false); HandleClientClose?.Invoke(this); } catch (Exception ex) { HandleException?.Invoke(ex); } } #endregion #region 事件處理 /// <summary> /// 客戶端鏈接創建後回調 /// </summary> public Action<SocketClient> HandleClientStarted { get; set; } /// <summary> /// 處理接受消息的委託 /// </summary> public Action<string, SocketClient> HandleRecMsg { get; set; } /// <summary> /// 客戶端鏈接發送消息後回調 /// </summary> public Action<byte[], SocketClient> HandleSendMsg { get; set; } /// <summary> /// 客戶端鏈接關閉後回調 /// </summary> public Action<SocketClient> HandleClientClose { get; set; } /// <summary> /// 啓動時報錯誤 /// </summary> public Action<Exception> StartException { get; set; } /// <summary> /// 異常處理程序 /// </summary> public Action<Exception> HandleException { get; set; } #endregion } }