首頁 > 後端開發 > C#.Net教程 > C#中networkcomms3.0如何實現模擬登陸的程式碼分享(圖)

C#中networkcomms3.0如何實現模擬登陸的程式碼分享(圖)

黄舟
發布: 2017-06-18 10:16:18
原創
2480 人瀏覽過

這篇文章主要介紹了C# networkcomms 3.0實現模擬登陸總結,需要的朋友可以參考下

最近專案需要做一個客戶查詢狀態系統,當前上位機缺少服務功能,於是找到了networkcomms 開源框架,作為專案使用.

最新版networkcomms 下載位址:https://github.com/MarcFletcher/NetworkComms.Net

下載直接vs開啟

新伺服器端


#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

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 == "1000" && loginContract.PassWord == "123")

          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");

      }

    }

  }

}

登入後複製

在別的幫助中往往少了這行:導致出現客戶端發送時,類型打包出現問題. 這行程式碼是客戶端伺服器兩端都要加上的,是指定傳輸方式


1

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

登入後複製

就是這個報錯了

一下是客戶端


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

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", 5000, contract);

      //向服务器发送登录信息并获取登录结果

      // ResMsgContract resMsg = newTcpConnection.SendReceiveObject<resmsgcontract>("ReqLogin", "ResLogin", 5000, contract);

      if (resMsg.Message == "登录成功")

      {

        MessageBox.Show("登录成功");

      }

      else

      {

        MessageBox.Show("用户名密码错误");

      }

    }

  }

}</resmsgcontract></logincontract,>

登入後複製

契約類別


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

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;

    }

  }

}

登入後複製

注意:

使用這個框架要配合Google的protobuf   要選好版本.本人沒重複測試最高版本,因為在調試登入過程中出現別的問題過程中,也順改了protobuf 的版本,至今未測試最高版本是否存在兼容問題.本人成功的使用的是2.0.0.668

    protobuf簡介protobuf是google提供的一個開源序列化框架,類似於XML,JSON這樣的資料表示語言,其最大的特點是基於二進位,因此比傳統的XML表示高效短小

#vs nuget新增方式

輸入

#版本選擇自己指定一下,加大專案的契約類裡邊.這是自己定義傳輸物件的方式.

 結果:

以上所述是小編給大家介紹的C# networkcomms 3.0實現模擬登陸總結,希望對大家有幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對腳本之家網站的支持!

以上是C#中networkcomms3.0如何實現模擬登陸的程式碼分享(圖)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板