如果客戶端和服務端都是C#語言,可以使用 NetworkComms V3 框架自帶的,包裝的protobuf序列化方式,或者BinaryFormater的方式序列化,都很方便。
剛才和朋友探討基於networkComms V3與其他語言通信,客戶端和服務器端之間傳遞二進制數組。
本例實現了C#客戶端與服務器端傳遞二進制數據,以此模仿與其他語言的通信
服務器端代碼:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using NetworkCommsDotNet; using NetworkCommsDotNet.DPSBase; using NetworkCommsDotNet.Tools; using NetworkCommsDotNet.Connections; using NetworkCommsDotNet.Connections.TCP; using System.Net; namespace RawServer { public partial class Form1 : Form { SendReceiveOptions sro = new SendReceiveOptions(DPSManager.GetDataSerializer<NullSerializer>(), null, null); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //服務器開始監聽客戶端的請求 //開始監聽某端口 //與其他語言通信 禁用ApplicationLayerProtocolStatus TCPConnectionListener listener=new TCPConnectionListener (sro,ApplicationLayerProtocolStatus.Disabled); Connection.StartListening(listener, new IPEndPoint(IPAddress.Parse(txtIP.Text), int.Parse(txtPort.Text))); button1.Text = "監聽中"; button1.Enabled = false; //此方法中包含服務器具體的處理方法。 StartListening(); } private void StartListening() { NetworkComms.DisableLogging(); // array 收到字節 NetworkComms.AppendGlobalIncomingUnmanagedPacketHandler((header, connection, array) => { //可以在這個方法中解析字節數組,解析出後按不同的類型再進行處理 LogInfo.LogMessage("創建日志文件", "日志.txt"); for (int i = 0; i < array.Length; i++) { LogInfo.LogMessage(i.ToString() + " - " + array[i].ToString(), "日志.txt"); } //收到服務器的消息後,如果需要返回數據給客戶端,可以用類似以下的代碼 //模擬一個數據 //byte[] dataToSend = new byte[] { 5,6,7,8 }; //connection.SendUnmanagedBytes(dataToSend); }); } private void Form1_FormClosing_1(object sender, FormClosingEventArgs e) { NetworkComms.Shutdown(); Environment.Exit(Environment.ExitCode); } } } View Code客戶端代碼:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using NetworkCommsDotNet; using NetworkCommsDotNet.DPSBase; using NetworkCommsDotNet.Tools; using NetworkCommsDotNet.Connections; using NetworkCommsDotNet.Connections.TCP; using System.Net; namespace RawClient { public partial class Form1 : Form { SendReceiveOptions sro = new SendReceiveOptions(DPSManager.GetDataSerializer<NullSerializer>(), null, null); //連接信息類 public ConnectionInfo connInfo = null; //連接類 Connection newTcpConnection; public Form1() { InitializeComponent(); } private void button1_Click_1(object sender, EventArgs e) { connInfo = new ConnectionInfo(txtIP.Text, int.Parse(txtPort.Text),ApplicationLayerProtocolStatus.Disabled); //如果不成功,會彈出異常信息 newTcpConnection = TCPConnection.GetConnection(connInfo,sro); button1.Enabled = false; button1.Text = "連接成功"; } private void button2_Click(object sender, EventArgs e) { byte[] dataToSend = new byte[] { 1, 2, 3, 4 }; newTcpConnection.SendUnmanagedBytes(dataToSend); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { NetworkComms.Shutdown(); Environment.Exit(Environment.ExitCode); } private void Form1_Load(object sender, EventArgs e) { } //如果需要處理從服務器發來的消息,可以模仿服務器端的寫法,進行監聽並分析數據 } } View Code服務器端監聽:
//與其他語言通信 禁用ApplicationLayerProtocolStatus TCPConnectionListener listener=new TCPConnectionListener (sro,ApplicationLayerProtocolStatus.Disabled); Connection.StartListening(listener, new IPEndPoint(IPAddress.Parse(txtIP.Text), int.Parse(txtPort.Text))); button1.Text = "監聽中"; button1.Enabled = false; //此方法中包含服務器具體的處理方法。 StartListening();
監聽方法:
private void StartListening() { NetworkComms.DisableLogging(); // array 收到字節 NetworkComms.AppendGlobalIncomingUnmanagedPacketHandler((header, connection, array) => { //可以在這個方法中解析字節數組,解析出後按不同的類型再進行處理 LogInfo.LogMessage("創建日志文件", "日志.txt"); for (int i = 0; i < array.Length; i++) { LogInfo.LogMessage(i.ToString() + " - " + array[i].ToString(), "日志.txt"); } //收到服務器的消息後,如果需要返回數據給客戶端,可以用類似以下的代碼 //模擬一個數據 //byte[] dataToSend = new byte[] { 5,6,7,8 }; //connection.SendUnmanagedBytes(dataToSend); }); }
客戶端的連接服務器的代碼:
connInfo = new ConnectionInfo(txtIP.Text, int.Parse(txtPort.Text),ApplicationLayerProtocolStatus.Disabled); //如果不成功,會彈出異常信息 newTcpConnection = TCPConnection.GetConnection(connInfo,sro); button1.Enabled = false; button1.Text = "連接成功";
客戶端發送byte字節數據的代碼:
private void button2_Click(object sender, EventArgs e) { byte[] dataToSend = new byte[] { 1, 2, 3, 4 }; newTcpConnection.SendUnmanagedBytes(dataToSend); }
服務器端收到的數據,為了觀察方便,寫入了文本文件中:
創建日志文件 0 - 1 1 - 2 2 - 3 3 - 4
工程文件下載(不含通信框架)
喜歡networkComms.cn的朋友可以探討下