频道栏目
首页 > 资讯 > 其他综合 > 正文

介绍开源的.net通信框架NetworkComms框架之二 传递类

16-08-22        来源:[db:作者]  
收藏   我要投稿

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(1)]
        public string Message { get; set; }
 

    }
}
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProtoBuf;

namespace Demo1.Business
{
    [ProtoContract]
    public class User
    {
        public User()
        { }

        [ProtoMember(1)]
        public string UserID { get; set; }

        [ProtoMember(2)]
        public string Name { get; set; }
        
    }
}
View Code

 

在Demo1.Client 和Demo1.Server中添加对Demo1.Business类的引用

 

客户端:

客户端代码:

 User theUser = new User();
            theUser.UserID = txtName.Text.Trim();
            theUser.Name = txtPsw.Text.Trim();

            ResMessage res = newTcpConnection.SendReceiveObject("UserLong", "ResLogin", 5000, 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("GetName", IncomingMsgHandle);

            NetworkComms.AppendGlobalIncomingPacketHandler("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 ();

            if (theUser.UserID == "1000" && theUser.Name == "张三")

                msg.Message = "登录成功";
            else
                msg.Message = "用户不存在";

            connection.SendObject("ResLogin", msg);
        }
    }
}

 

这样使用protobuf进行序列化的通信就完成了。因为neworkcomms默认使用protobuf进行通信,所以其他无需额外设置。如果使用其他序列化器,也可以很方便的设置。

源码:http://pan.baidu.com/s/1dFNrMtN

相关TAG标签
上一篇:ASP.NET Core 中文文档 第三章 原理(14)服务器
下一篇:介绍开源的.net通信框架NetworkComms框架之一 首字节验证
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站