.NET Socket 簡單入門

說到Socket,想必你們都或多或少有所涉及,從最初的計算機網絡課程,講述了tcp協議,而Socket就是對協議的進一步封裝,使咱們開發人員可以更加容易輕鬆的進行軟件之間的通訊。javascript

這個星期恰好接受一個共享車位鎖的項目,須要使用Socket與硬件進行通訊控制,說白了也就是給鎖發送指令,控制其打開或者關閉,再就是對App開放操做接口,使其方便測試以及用戶的使用。這其中核心就是Socket的使用,再開發出這個功能以後,我發現使用起來很不方便,因而耗時2天抽象其核心功能並封裝成框架,最後使用這個框架將原來的項目重構並上線,極大的提升了軟件的可拓展性,健壯性,容錯率。java

我的堅信的原則:萬物皆對象git

好了,不廢話了,下面進入正文github

 

正文:服務器

一、首先簡單講下C#中Socket的簡單使用。網絡

第一步:服務端監聽某個端口框架

第二步:客戶端向服務端地址和端口發起Socket鏈接請求異步

第三步:服務端收到鏈接請求後建立Socket鏈接,並維護這個鏈接隊列。socket

第四步:客戶端和服務端已經創建雙工通訊(即雙向通訊),客戶端和服務端能夠輕鬆方便的給彼此發送信息。async

至於簡單使用的具體實現代碼所有被我封裝到項目中了,若是須要學習簡單的實現,能夠看個人源碼,也能夠自行百度,有不少的教程

 

二、核心,框架的使用

其實,說其爲框架,可能有點牽強,由於每一個人對框架都有本身的理解,可是類庫和框架又有什麼本質區別呢?所有都是代碼~哈哈,扯遠了

首先,空說無憑,先放上全部的代碼:

服務端源文件:

SocketServer.cs
複製代碼
using System; using System.Collections.Generic; using System.Net; using System.Net.Sockets; namespace Coldairarrow.Util.Sockets { /// <summary> /// Socket服務端 /// </summary> public class SocketServer { #region 構造函數 /// <summary> /// 構造函數 /// </summary> /// <param name="ip">監聽的IP地址</param> /// <param name="port">監聽的端口</param> public SocketServer(string ip, int port) { _ip = ip; _port = port; } /// <summary> /// 構造函數,監聽IP地址默認爲本機0.0.0.0 /// </summary> /// <param name="port">監聽的端口</param> public SocketServer(int port) { _ip = "0.0.0.0"; _port = port; } #endregion #region 內部成員 private Socket _socket = null; private string _ip = ""; private int _port = 0; private bool _isListen = true; 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<byte[], 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) }; 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(_ip); //建立網絡節點對象包含ip和port IPEndPoint endpoint = new IPEndPoint(address, _port); //將 監聽套接字綁定到 對應的IP和端口  _socket.Bind(endpoint); //設置監聽隊列長度爲Int32最大值(同時可以處理鏈接請求數量) _socket.Listen(int.MaxValue); //開始監聽客戶端  StartListen(); HandleServerStarted?.Invoke(this); } catch (Exception ex) { HandleException?.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; } #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<byte[], 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.Net.Sockets; using System.Text; namespace Coldairarrow.Util.Sockets { /// <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 * 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); //處理消息 HandleRecMsg?.Invoke(recBytes, this, _server); } else Close(); } catch (Exception ex) { HandleException?.Invoke(ex); Close(); } }, null); } catch (Exception ex) { HandleException?.Invoke(ex); Close(); } } /// <summary> /// 發送數據 /// </summary> /// <param name="bytes">數據字節</param> public 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) { 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); _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<byte[], 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; } #endregion } }
複製代碼
複製代碼
using System; using System.Net; using System.Net.Sockets; using System.Text; namespace Coldairarrow.Util.Sockets { /// <summary> /// Socket客戶端 /// </summary> public class SocketClient { #region 構造函數 /// <summary> /// 構造函數,鏈接服務器IP地址默認爲本機127.0.0.1 /// </summary> /// <param name="port">監聽的端口</param> public SocketClient(int port) { _ip = "127.0.0.1"; _port = port; } /// <summary> /// 構造函數 /// </summary> /// <param name="ip">監聽的IP地址</param> /// <param name="port">監聽的端口</param> public SocketClient(string ip, int port) { _ip = ip; _port = port; } #endregion #region 內部成員 private Socket _socket = null; private string _ip = ""; private int _port = 0; private bool _isRec=true; 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); //處理消息 HandleRecMsg?.Invoke(recBytes, this); } else Close(); } catch (Exception ex) { HandleException?.Invoke(ex); Close(); } }, null); } catch (Exception ex) { 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(_ip); //建立網絡節點對象 包含 ip和port IPEndPoint endpoint = new IPEndPoint(address, _port); //將 監聽套接字 綁定到 對應的IP和端口 _socket.BeginConnect(endpoint, asyncResult => { try { _socket.EndConnect(asyncResult); //開始接受服務器消息  StartRecMsg(); HandleClientStarted?.Invoke(this); } catch (Exception ex) { HandleException?.Invoke(ex); } }, null); } catch (Exception ex) { HandleException?.Invoke(ex); } } /// <summary> /// 發送數據 /// </summary> /// <param name="bytes">數據字節</param> public void Send(byte[] bytes) { try { _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<byte[], 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> HandleException { get; set; } #endregion } }
複製代碼

 

上面放上的是框架代碼,接下來介紹下如何使用

首先,服務端使用方式:

複製代碼
using Coldairarrow.Util.Sockets; using System; using System.Text; namespace Console_Server { class Program { static void Main(string[] args) { //建立服務器對象,默認監聽本機0.0.0.0,端口12345 SocketServer server = new SocketServer(12345); //處理從客戶端收到的消息 server.HandleRecMsg = new Action<byte[], SocketConnection, SocketServer>((bytes, client, theServer) => { string msg = Encoding.UTF8.GetString(bytes); Console.WriteLine($"收到消息:{msg}"); }); //處理服務器啓動後事件 server.HandleServerStarted = new Action<SocketServer>(theServer => { Console.WriteLine("服務已啓動************"); }); //處理新的客戶端鏈接後的事件 server.HandleNewClientConnected = new Action<SocketServer, SocketConnection>((theServer, theCon) => { Console.WriteLine($@"一個新的客戶端接入,當前鏈接數:{theServer.ClientList.Count}"); }); //處理客戶端鏈接關閉後的事件 server.HandleClientClose = new Action<SocketConnection, SocketServer>((theCon, theServer) => { Console.WriteLine($@"一個客戶端關閉,當前鏈接數爲:{theServer.ClientList.Count}"); }); //處理異常 server.HandleException = new Action<Exception>(ex => { Console.WriteLine(ex.Message); }); //服務器啓動  server.StartServer(); while (true) { Console.WriteLine("輸入:quit,關閉服務器"); string op = Console.ReadLine(); if (op == "quit") break; } } } }
複製代碼

客戶端使用方式:

複製代碼
using Coldairarrow.Util.Sockets; using System; using System.Text; namespace Console_Client { class Program { static void Main(string[] args) { //建立客戶端對象,默認鏈接本機127.0.0.1,端口爲12345 SocketClient client = new SocketClient(12345); //綁定當收到服務器發送的消息後的處理事件 client.HandleRecMsg = new Action<byte[], SocketClient>((bytes, theClient) => { string msg = Encoding.UTF8.GetString(bytes); Console.WriteLine($"收到消息:{msg}"); }); //綁定向服務器發送消息後的處理事件 client.HandleSendMsg = new Action<byte[], SocketClient>((bytes, theClient) => { string msg = Encoding.UTF8.GetString(bytes); Console.WriteLine($"向服務器發送消息:{msg}"); }); //開始運行客戶端  client.StartClient(); while (true) { Console.WriteLine("輸入:quit關閉客戶端,輸入其它消息發送到服務器"); string str = Console.ReadLine(); if (str == "quit") { client.Close(); break; } else { client.Send(str); } } } } }
複製代碼

最後運行測試截圖:

 

總結:

其最方便之處在於,將如何建立鏈接封裝掉,使用人員只需關注鏈接後發送什麼數據,接收到數據後應該如何處理,等等其它的不少事件的處理,這其中主要依託於匿名委託的使用,Lambda表達式的使用。

框架裏面主要使用了異步通信,以及如何控制鏈接,詳細我就很少說了,你們應該一看就懂,我只但願能給你們帶來便利,最後你們有任何問題、意見、想法,均可以給我留言。

最後,附上全部源碼項目地址,若以爲有必定價值,還請點贊~

GitHub地址:https://github.com/Coldairarrow/Sockets

相關文章
相關標籤/搜索