如果客户端和服务端都是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 - 11 - 22 - 33 - 4
工程文件下载(不含通信框架)
喜欢networkComms.cn的朋友可以探讨下
原标题:基于NetworkComms V3 使用二进制数据通信
关键词:net