C# networkcomms 3.0實(shí)現(xiàn)模擬登陸總結(jié)
最近項(xiàng)目需要做一個(gè)客戶查詢狀態(tài)系統(tǒng),當(dāng)前上位機(jī)缺少服務(wù)功能,于是找到了networkcomms 開源框架,作為項(xiàng)目使用.
最新版networkcomms 下載地址:https://github.com/MarcFletcher/NetworkComms.Net
下載直接vs打開
新建服務(wù)器端
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) { //服務(wù)器開始監(jiān)聽客戶端的請(qǐng)求 Connection.StartListening(ConnectionType.TCP, new IPEndPoint(IPAddress.Parse(txtIP.Text), int.Parse(txtPort.Text))); //服務(wù)器開始監(jiān)聽客戶端的請(qǐng)求 //IPEndPoint thePoint = new IPEndPoint(IPAddress.Parse(txtIP.Text), int.Parse(txtPort.Text)); //TCPConnection.StartListening(thePoint, false); button1.Text = "監(jiān)聽中"; button1.Enabled = false; //button1.Text = "監(jiān)聽中"; //button1.Enabled = false; //此方法中包含服務(wù)器具體的處理方法。 StartListening(); } private void StartListening() { //開啟日志記錄 //配置日志記錄器 //ILogger logger = new LiteLogger(LiteLogger.LogMode.ConsoleAndLogFile, "ServerLogFile_" + NetworkComms.NetworkIdentifier + ".txt"); //NetworkComms.EnableLogging(logger); //禁用日志記錄 服務(wù)器端正式使用時(shí),贏禁用日志記錄 NetworkComms.DisableLogging(); //服務(wù)器端處理收到的消息 //為簡(jiǎn)單起見,此示例中我們只處理字符類型的信息,也返回字符類型的信息。 //處理的信息可以使自定義類,具體見下一個(gè)Demo NetworkComms.AppendGlobalIncomingPacketHandler<LoginContract>("ReqLogin", IncomingLoginRequest); } //處理某個(gè)具體的請(qǐng)求 private void IncomingLoginRequest(PacketHeader header, Connection connection, LoginContract loginContract) { try { string resMsg = ""; //為了簡(jiǎn)單,這里不調(diào)用數(shù)據(jù)庫(kù),而是模擬一下登錄 if (loginContract.UserID == "1000" && loginContract.PassWord == "123") resMsg = "登錄成功"; else resMsg = "用戶名密碼錯(cuò)誤"; //把返回結(jié)果寫入到契約類中,后面返回給客戶端 //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"); } } } }
在別的幫助中往往少了這行:導(dǎo)致出現(xiàn)客戶端發(fā)送時(shí),類型打包出現(xiàn)問(wèn)題. 這行代碼是客戶端服務(wù)器兩端都要加上的,是指定傳輸方式
SendReceiveOptions aboveOptions = new SendReceiveOptions(DPSManager.GetDataSerializer<ProtobufSerializer>(), null, null);
就是這個(gè)報(bào)錯(cuò)了
一下是客戶端
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(); } //連接信息對(duì)象 public ConnectionInfo connInfo = null; //連接對(duì)象 Connection newTcpConnection; SendReceiveOptions aboveOptions = new SendReceiveOptions(DPSManager.GetDataSerializer<ProtobufSerializer>(), null, null); private void button1_Click(object sender, EventArgs e) { //給連接信息對(duì)象賦值 connInfo = new ConnectionInfo(txtIP.Text, int.Parse(txtPort.Text)); //如果不成功,會(huì)彈出異常信息 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; //向服務(wù)器發(fā)送登錄信息并獲取登錄結(jié)果 ResMsgContract resMsg = newTcpConnection.SendReceiveObject<LoginContract, ResMsgContract>("ReqLogin", "ResLogin", 5000, contract); //向服務(wù)器發(fā)送登錄信息并獲取登錄結(jié)果 // ResMsgContract resMsg = newTcpConnection.SendReceiveObject<ResMsgContract>("ReqLogin", "ResLogin", 5000, contract); if (resMsg.Message == "登錄成功") { MessageBox.Show("登錄成功"); } else { MessageBox.Show("用戶名密碼錯(cuò)誤"); } } } }
契約類
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MessageContract { [ProtoContract] public class LoginContract { [ProtoMember(1)] public string UserID { get; set; } [ProtoMember(2)] 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(1)] public string Message; public ResMsgContract() { } public ResMsgContract(string message) { this.Message = message; } } }
注意:
使用這個(gè)框架要配合谷歌的protobuf 要選好版本.本人沒(méi)重復(fù)測(cè)試最高版本,因?yàn)樵谡{(diào)試登錄過(guò)程中出現(xiàn)別的問(wèn)題過(guò)程中,也順改了protobuf 的版本,至今未測(cè)試最高版本是否存在兼容問(wèn)題.本人成功的使用的是2.0.0.668
protobuf簡(jiǎn)介protobuf是google提供的一個(gè)開源序列化框架,類似于XML,JSON這樣的數(shù)據(jù)表示語(yǔ)言,其最大的特點(diǎn)是基于二進(jìn)制,因此比傳統(tǒng)的XML表示高效短小
vs nuget添加方式
輸入
版本選擇自己指定一下,加大項(xiàng)目的契約類里邊.這是自己定義傳輸對(duì)象的方式.
結(jié)果:
以上所述是小編給大家介紹的C# networkcomms 3.0實(shí)現(xiàn)模擬登陸總結(jié),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
UpdateLayeredWindow實(shí)現(xiàn)任意異形窗口使用詳解
這篇文章主要為大家介紹了UpdateLayeredWindow實(shí)現(xiàn)任意異形窗口使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09c#中string的特性介紹及注意事項(xiàng)小結(jié)
這篇文章主要給大家介紹了關(guān)于c#中string的特性介紹及注意事項(xiàng)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用c#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11C#實(shí)現(xiàn)位圖轉(zhuǎn)換成圖標(biāo)的方法
這篇文章主要介紹了C#實(shí)現(xiàn)位圖轉(zhuǎn)換成圖標(biāo)的方法,可實(shí)現(xiàn)將bmp格式位圖轉(zhuǎn)換成ico格式圖標(biāo)的功能,需要的朋友可以參考下2015-06-06Unity實(shí)現(xiàn)弧形移動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)弧形移動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06C#實(shí)現(xiàn)多個(gè)計(jì)時(shí)器記錄不同定時(shí)時(shí)間
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)多個(gè)計(jì)時(shí)器記錄不同定時(shí)時(shí)間,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12