最近项目需要做一个客户查询状态系统,当前上位机缺少服务功能,于是找到了networkcomms 开源框架,作为项目使用.

最新版networkcomms 下载地址:https://github.com/MarcFletcher/NetworkComms.Net

下载直接vs打开

新建服务器端

using MessageContract;
using NetworkCommsDotNet;
using NetworkCommsDotNet.Connections;
using NetworkCommsDotNet.Connections.TCP;
using NetworkCommsDotNet.DPSBase;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms; namespace AppServer
{
public partial class MaiForm : Form
{
public MaiForm()
{
InitializeComponent();
}
SendReceiveOptions aboveOptions = new SendReceiveOptions(DPSManager.GetDataSerializer<ProtobufSerializer>(), null, null);
private void button1_Click(object sender, EventArgs e)
{
//服务器开始监听客户端的请求
Connection.StartListening(ConnectionType.TCP, new IPEndPoint(IPAddress.Parse(txtIP.Text), int.Parse(txtPort.Text)));
//服务器开始监听客户端的请求
//IPEndPoint thePoint = new IPEndPoint(IPAddress.Parse(txtIP.Text), int.Parse(txtPort.Text));
//TCPConnection.StartListening(thePoint, false);
button1.Text = "监听中";
button1.Enabled = false;
//button1.Text = "监听中";
//button1.Enabled = false; //此方法中包含服务器具体的处理方法。
StartListening();
} private void StartListening()
{
//开启日志记录
//配置日志记录器
//ILogger logger = new LiteLogger(LiteLogger.LogMode.ConsoleAndLogFile, "ServerLogFile_" + NetworkComms.NetworkIdentifier + ".txt");
//NetworkComms.EnableLogging(logger); //禁用日志记录 服务器端正式使用时,赢禁用日志记录
NetworkComms.DisableLogging(); //服务器端处理收到的消息
//为简单起见,此示例中我们只处理字符类型的信息,也返回字符类型的信息。
//处理的信息可以使自定义类,具体见下一个Demo
NetworkComms.AppendGlobalIncomingPacketHandler<LoginContract>("ReqLogin", IncomingLoginRequest); }
//处理某个具体的请求
private void IncomingLoginRequest(PacketHeader header, Connection connection, LoginContract loginContract)
{
try
{
string resMsg = ""; //为了简单,这里不调用数据库,而是模拟一下登录
if (loginContract.UserID == "" && loginContract.PassWord == "") resMsg = "登录成功"; else resMsg = "用户名密码错误";
//把返回结果写入到契约类中,后面返回给客户端
//ResMsgContract contract = new ResMsgContract();
//contract.Message = resMsg; //connection.SendObject<ResMsgContract>("ResLogin", contract);
ResMsgContract contract = new ResMsgContract();
contract.Message = resMsg; connection.SendObject("ResLogin", contract);
}
catch (Exception ex)
{
// LogTools.LogException(ex, "IncomingMsgHandle");
}
}
}
}

在别的帮助中往往少了这行:导致出现客户端发送时,类型打包出现问题. 这行代码是客户端服务器两端都要加上的,是指定传输方式

 SendReceiveOptions aboveOptions = new SendReceiveOptions(DPSManager.GetDataSerializer<ProtobufSerializer>(), null, null);

就是这个报错了

一下是客户端

using MessageContract;
using NetworkCommsDotNet;
using NetworkCommsDotNet.Connections;
using NetworkCommsDotNet.Connections.TCP;
using NetworkCommsDotNet.DPSBase;
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; namespace AppClient
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
//连接信息对象
public ConnectionInfo connInfo = null;
//连接对象
Connection newTcpConnection;
SendReceiveOptions aboveOptions = new SendReceiveOptions(DPSManager.GetDataSerializer<ProtobufSerializer>(), null, null);
private void button1_Click(object sender, EventArgs e)
{
//给连接信息对象赋值
connInfo = new ConnectionInfo(txtIP.Text, int.Parse(txtPort.Text)); //如果不成功,会弹出异常信息
newTcpConnection = TCPConnection.GetConnection(connInfo); button1.Enabled = false;
button1.Text = "连接成功";
} private void btnlogin_Click(object sender, EventArgs e)
{
//给契约类赋值
LoginContract contract = new LoginContract(txtUserName.Text, txtPassword.Text);
//contract.UserID = txtUserName.Text;
//contract.PassWord = txtPassword.Text;
//向服务器发送登录信息并获取登录结果
ResMsgContract resMsg = newTcpConnection.SendReceiveObject<LoginContract, ResMsgContract>("ReqLogin", "ResLogin", , contract);
//向服务器发送登录信息并获取登录结果
// ResMsgContract resMsg = newTcpConnection.SendReceiveObject<ResMsgContract>("ReqLogin", "ResLogin", 5000, contract); if (resMsg.Message == "登录成功")
{
MessageBox.Show("登录成功"); }
else
{
MessageBox.Show("用户名密码错误");
}
}
}
}

契约类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace MessageContract
{
[ProtoContract]
public class LoginContract
{
[ProtoMember()]
public string UserID { get; set; } [ProtoMember()]
public string PassWord { get; set; } public LoginContract() { } public LoginContract(string userID, string passWord)
{
this.UserID = userID;
this.PassWord = passWord;
}
}
}
using ProtoBuf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace MessageContract
{
[ProtoContract]
public class ResMsgContract
{
[ProtoMember()]
public string Message;
public ResMsgContract() { }
public ResMsgContract(string message)
{
this.Message = message;
}
}
}

注意:

使用这个框架要配合谷歌的protobuf   要选好版本.本人没重复测试最高版本,因为在调试登录过程中出现别的问题过程中,也顺改了protobuf 的版本,至今未测试最高版本是否存在兼容问题.本人成功的使用的是2.0.0.668

    protobuf简介protobuf是google提供的一个开源序列化框架,类似于XML,JSON这样的数据表示语言,其最大的特点是基于二进制,因此比传统的XML表示高效短小

vs nuget添加方式

输入

版本选择自己指定一下,加大项目的契约类里边.这是自己定义传输对象的方式.

结果:

c# networkcomms 3.0实现模拟登陆总结的更多相关文章

  1. c# networkcomms 3.0实现模拟登陆总结 转载https://www.cnblogs.com/zuochanzi/p/7039636.html

    最近项目需要做一个客户查询状态系统,当前上位机缺少服务功能,于是找到了networkcomms 开源框架,作为项目使用. 最新版networkcomms 下载地址:https://github.com ...

  2. NetworkComms V3 模拟登陆

    演示NetworkComms V3的用法 例子很简单 界面如下: 服务器端代码: 开始监听: //服务器开始监听客户端的请求 Connection.StartListening(ConnectionT ...

  3. Python 爬虫模拟登陆知乎

    在之前写过一篇使用python爬虫爬取电影天堂资源的博客,重点是如何解析页面和提高爬虫的效率.由于电影天堂上的资源获取权限是所有人都一样的,所以不需要进行登录验证操作,写完那篇文章后又花了些时间研究了 ...

  4. Python模拟登陆新浪微博

    上篇介绍了新浪微博的登陆过程,这节使用Python编写一个模拟登陆的程序.讲解与程序如下: 1.主函数(WeiboMain.py): import urllib2 import cookielib i ...

  5. PHP 之 CURL 模拟登陆并获取数据

    1.CURL模拟登陆的流程和步骤 2.tempnam 创建一个临时文件 3.使用CURL模拟登陆到PHP100论坛 <?php $cookie_file = tempnam('./temp',' ...

  6. pytho简单爬虫_模拟登陆西电流量查询_实现一键查询自己的校园网流量

    闲来无事,由于校园内网络是限流量的,查询流量很是频繁,于是萌生了写一个本地脚本进行一键查询自己的剩余流量. 整个部分可以分为三个过程进行: 对登陆时http协议进行分析 利用python进行相关的模拟 ...

  7. python模拟登陆知乎并爬取数据

    一些废话 看了一眼上一篇日志的时间 已然是5个月前的事情了 不禁感叹光阴荏苒其实就是我懒 几周前心血来潮想到用爬虫爬些东西 于是先后先重写了以前写过的求绩点代码 爬了草榴贴图,妹子图网,后来想爬婚恋网 ...

  8. php模拟登陆的两种实现方法分析

    php模拟登陆的实现方法分析 本文实例分析了php模拟登陆的实现方法.分享给大家供大家参考.具体分析如下: php模拟登陆的实现方法,这里分别列举两种方法实现模拟登陆人人网.具体实例代码如下: 1)使 ...

  9. python 模拟登陆,请求包含cookie信息

    需求: 1.通过GET方法,访问URL地址一,传入cookie参数 2.根据地址一返回的uuid,通过POST方法,传入cooki参数 实现思路: 1.理解http的GET和POST差别 (网上有很多 ...

随机推荐

  1. 我拖拖拖--H5拖放API基础篇

    不要搞错,本文不是讲如何拖地的.看过<javascript精粹>朋友应该知道,他实现拖放的过程比较复杂,现在时代不同了,我们用H5的新的拖放API就能非常方便的实现拖放效果了.最近在园子见 ...

  2. stl map容器 学习

    #include<map> 1.map的声明: map<string,int>map_1; map_1 就是一个string对int的映射. 2.map的用法(映射): map ...

  3. 单例模式——Java EE设计模式解析与应用

    单例模式 目录: 一.何为单例 二.使用Java EE实现单例模式 三.使用场景 一.何为单例 确保一个类只有一个实例,并且提供了实例的一个全局访问点 1.1 单例模式类图               ...

  4. Docker - 访问仓库

    仓库与注册服务器 仓库(Repository)集中存放镜像的项目或目录. 注册服务器(Registry)管理仓库的服务器,服务器上可以有多个仓库,每个仓库有多个镜像. 例如:在仓库地址docker.i ...

  5. Centos7 ftp环境搭建

    没玩过linux,折腾了半天的ftp,好不容易亲测通过了.不容易啊. 操作环境:vm虚拟机 centos7 首先:搞定网络问题:默认情况下使用ifconfig可以看到虚拟机下是无网络的.(注:虚拟机网 ...

  6. 如何应对苹果app 的ipv6 时代?腾讯专家教您进行环境改造

    WeTest 导读 WWDC2015苹果宣布在ios9支持纯IPv6的网络服务,并且要求2016年提交到app store的应用必须兼容纯IPv6的网络,要求适配的系统版本是ios9以上(包括ios9 ...

  7. angular4 JavaScript内存溢出问题 (FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory)

    最近在写基于angular4的项目的时候,在build --prod的时候,突然措手不及的蹦出个报错,大致错误如下: 70% building modules 1345/1345 modules 0 ...

  8. 8611 大牛之路I

    #include<stdio.h> ]; int main() { int i, n, m, sum, x, y; scanf("%d%d", &n, & ...

  9. java虚拟机学习-JVM调优总结-新一代的垃圾回收算法(11)

    垃圾回收的瓶颈 传统分代垃圾回收方式,已经在一定程度上把垃圾回收给应用带来的负担降到了最小,把应用的吞吐量推到了一个极限.但是他无法解决的一个问题,就是Full GC所带来的应用暂停.在一些对实时性要 ...

  10. Metrics

    系统开发到一定的阶段,线上的机器越来越多,就需要一些监控了,除了服务器的监控,业务方面也需要一些监控服务.Metrics作为一款监控指标的度量类库,提供了许多工具帮助开发者来完成自定义的监控工作. 举 ...