原文网址: http://www.cnblogs.com/csdev
Networkcomms 是一款C# 语言编写的TCP/UDP通信框架 作者是英国人 以前是收费的 目前作者已经开源 开源地址是:https://github.com/MarcFletcher/NetworkComms.Net
使用networkcomms框架通信时,客户端发送消息,服务器端回复消息。
在介绍开源的.net通信框架NetworkComms 一文中,我们介绍了如何从客户端发送字符串给服务器端,以及如何从服务器端接收发回来的字符串。
本文介绍一下,如何发送自定义的类数据给服务器端,和如何获取从服务端返回的数据。
新建一个类库
添加对Protobuf.dll的引用,这个文件中在MarcF-networkcomms.net-8e01e19f827f\packages\protobuf-net.2.0.0.668\lib相关文件夹中
添加这个引用,是因为通信时,使用了Protobuf进行序列化
添加2个可以序列化的类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using ProtoBuf; namespace Demo1.Business { [ProtoContract] public class ResMessage { public ResMessage() { } [ProtoMember()] public string Message { get; set; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using ProtoBuf; namespace Demo1.Business { [ProtoContract] public class User { public User() { } [ProtoMember()] public string UserID { get; set; } [ProtoMember()] public string Name { get; set; } } }
在Demo1.Client 和Demo1.Server中添加对Demo1.Business类的引用
客户端:
客户端代码:
User theUser = new User(); theUser.UserID = txtName.Text.Trim(); theUser.Name = txtPsw.Text.Trim(); ResMessage res = newTcpConnection.SendReceiveObject<User, ResMessage>(, theUser); if (res.Message == "验证成功") { MessageBox.Show("用户验证成功"); } else { MessageBox.Show(res.Message); }
服务器端代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using NetworkCommsDotNet.Connections; using NetworkCommsDotNet; using NetworkCommsDotNet.DPSBase; using NetworkCommsDotNet.Tools; using NetworkCommsDotNet.Connections.TCP; using System.Net; using Demo1.Business; namespace Demo1.Server { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //IP地址和端口 IPEndPoint thePoint = new IPEndPoint(IPAddress.Parse(txtIP.Text), int.Parse(txtPort.Text)); //开始监听此IP和端口 使用TCP协议 Connection.StartListening(ConnectionType.TCP, thePoint); NetworkComms.AppendGlobalIncomingPacketHandler<string>("GetName", IncomingMsgHandle); NetworkComms.AppendGlobalIncomingPacketHandler<User>("UserLong", IncoingHandleLogin); button1.Text = "已经开始监听"; } private void IncomingMsgHandle(PacketHeader header, Connection connection, string msg) { try { string resMsg = ""; if (msg == "星期一") resMsg = "Monday"; else if (msg == "星期二") resMsg = "Tuesday"; else if (msg == "星期三") resMsg = "Wednesday"; else if (msg == "星期四") resMsg = "Thursday"; else if (msg == "星期五") resMsg = "Friday"; else if (msg == "星期六") resMsg = "Saturday"; else if (msg == "星期日") resMsg = "Sunday"; connection.SendObject("ResName", resMsg); } catch (Exception ex) { } } private void IncoingHandleLogin(PacketHeader header, Connection connection, User theUser) { ResMessage msg=new ResMessage (); " && theUser.Name == "张三") msg.Message = "登录成功"; else msg.Message = "用户不存在"; connection.SendObject("ResLogin", msg); } } }
这样使用protobuf进行序列化的通信就完成了。因为neworkcomms默认使用protobuf进行通信,所以其他无需额外设置。如果使用其他序列化器,也可以很方便的设置。
原文地址 www.cnblogs.com/csdev
源码:http://pan.baidu.com/s/1dFNrMtN