在文章AgileEAS.NET SOA 中間件平臺Socket/Tcp通訊框架介紹一文之中咱們對AgileEAS.NET SOA中間Socket/Tcp框架進行了整體的介紹,咱們知道git
AgileEAS.NET SOA中間件Socket/Tcp框架是一套Socket通訊的消息中間件:程序員
咱們接下來實現一個簡單的例子,例子的場景很是的簡單,客戶端向服務端發送一個請求消息,服務段收到消息以後顯示消息內容,並對客戶端發送一個應答消息,客戶端收到應答消息以後顯示這個應答消息,對於本例子,咱們使用WinForm應用來實現。github
首先咱們下載AgileEAS.NET SOA 中間件最新版本5.0/5.1,下載地址:http://www.smarteas.net/page/EAS_NET_Downloads.aspx。服務器
首先咱們打開VS2010或者VS2010建立一個類爲項目Socket.Demo.Messages:app
刪除默認的Class1.cs,引用AgileEAS.NET SOA 中間件的EAS.MicroKernel.dll程序集文件,而後添加一個類請求消息類RequestMessage:框架
1: [Serializable]
2: [Message("14DB99D2-190C-48E9-879A-EA833247F24A", "請求消息")]
3: public class RequestMessage:IMessage
4: {
5: /// <summary>
6: /// 消息內容。
7: /// </summary>
8: public string Content
9: {
10: get;
11: set;
12: }
13:
14: public void Load(EAS.IO.BufferReader reader)
15: {
16: this.Content = reader.ReadString();
17: }
18:
19: public void WriteTo(EAS.IO.BufferWriter writer)
20: {
21: writer.Write(this.Content);
22: }
23: }
以上代碼咱們定義了一個請求消息,消息ID爲"14DB99D2-190C-48E9-879A-EA833247F24A",Load方法實現從消息讀取器之中讀出消息內容,WriteTo方法實現把消息內容寫入消息編寫器,轉換爲字節流供通訊引擎進行收發,接下來咱們再增長一個應答消息類ResponseMessage:ide
1: [Serializable]
2: [Message("D94C51C4-8423-4BF6-A011-184265BBA62C", "應答消息")]
3: public class ResponseMessage : RequestMessage, IMessage
4: {
5: }
在本例子之中應答消息與請求消息其內容沒有本質的區別,都是文本通訊,因此就直接繼承了RequestMessage,變動了MessageID和消息說明。佈局
到此爲止,咱們已經完成了請求消息與應答消息的定義Socket.Demo.Messages項目結構下:post
接下來咱們來實現服務端程序,向解決方案添加一個Socket.Demo.Server的Windows窗體應用程序:測試
一樣,咱們要引用AgileEAS.NET SOA 中間件的EAS.MicroKernel.dll程序集文件,而且還要引用Socket.Demo.Messages項目,而後重命名Form1爲MainForm,並作如下界面佈局:
接下來咱們向服務端加載請求消息的處理器RequestMessageHandler,用於服務端收到請求消息以後的處理動做:
1: class RequestMessageHandler:AbstractMessageHandler<RequestMessage>
2: {
3: public override void Process(NetSession context, uint instanceId, RequestMessage message)
4: {
5: //1.顯示消息。
6: MainForm.Instance.ShowMessage(message);
7:
8: //2.向客戶端回覆一條應當消息。
9: string content = string.Format("{0}已收到,OK!", message.Content);
10: ResponseMessage responseMessage = new ResponseMessage { Content = content };
11: context.Reply(responseMessage);
12: }
13: }
而後咱們實現MainForm窗體的功能代碼:
1: using System;
2: using System.Collections.Generic;
3: using System.ComponentModel;
4: using System.Data;
5: using System.Drawing;
6: using System.Linq;
7: using System.Text;
8: using System.Windows.Forms;
9: using EAS.Sockets;
10: using Socket.Demo.Messages;
11:
12: namespace Socket.Demo.Server
13: {
14: public partial class MainForm : Form
15: {
16: public static MainForm Instance = null;
17: private SocketServer m_SocketServer = null;
18:
19: public MainForm()
20: {
21: InitializeComponent();
22: Instance = this;
23:
24: //實例化對象。
25: this.m_SocketServer = new SocketServer();
26:
27: //端口號爲6610
28: this.m_SocketServer.Port = 6610;
29:
30: //註冊消息處理器。
31: this.m_SocketServer.AddHander<RequestMessage>(new RequestMessageHandler());
32:
33: this.m_SocketServer.ServerStarted += new EventHandler(m_SocketServer_ServerStarted);
34: this.m_SocketServer.ServerStopped+=new EventHandler(m_SocketServer_ServerStopped);
35: }
36:
37: void m_SocketServer_ServerStarted(object sender, EventArgs e)
38: {
39: this.btnStart.Enabled = false;
40: this.btnStop.Enabled = true;
41: }
42:
43: void m_SocketServer_ServerStopped(object sender, EventArgs e)
44: {
45: this.btnStart.Enabled = true;
46: this.btnStop.Enabled = false;
47: }
48:
49: internal void ShowMessage(Messages.RequestMessage message)
50: {
51: Action UI = () =>
52: {
53: this.tbMessage.AppendText(message.Content);
54: this.tbMessage.AppendText("\r\n");
55: this.tbMessage.ScrollToCaret();
56: };
57:
58: this.Invoke(UI);
59: }
60:
61: private void btnStart_Click(object sender, EventArgs e)
62: {
63: if (this.m_SocketServer.ServerState == ServerState.Stoped)
64: {
65: this.m_SocketServer.StartServer();
66: }
67: }
68:
69: private void btnStop_Click(object sender, EventArgs e)
70: {
71: if (this.m_SocketServer.ServerState != ServerState.Stoped)
72: {
73: this.m_SocketServer.StopServer();
74: }
75: }
76: }
77: }
其中代碼this.m_SocketServer.AddHander<RequestMessage>(new RequestMessageHandler());完成向服務器註冊請求消息的處理器對象,internal void ShowMessage(Messages.RequestMessage message)方法用於顯示服務器收到的請求消息。
接下來咱們來實現客戶端程序,向解決方案添加一個Socket.Demo.Client的Windows窗體應用程序,同前面同樣樣,咱們要引用AgileEAS.NET SOA 中間件的EAS.MicroKernel.dll程序集文件,而且還要引用Socket.Demo.Messages項目,而後重命名Form1爲MainForm,並作如下界面佈局:
接下來咱們向客戶端加載應答消息的處理器ResponseMessageHandler,用於客戶端收到應答消息以後的處理動做:
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5: using EAS.Sockets.Messages;
6: using Socket.Demo.Messages;
7: using EAS.Sockets;
8:
9: namespace Socket.Demo.Client
10: {
11: class ResponseMessageHandler : AbstractMessageHandler<ResponseMessage>
12: {
13: public override void Process(NetSession context, uint instanceId, ResponseMessage message)
14: {
15: //1.顯示消息。
16: MainForm.Instance.ShowMessage(message);
17: }
18: }
19: }
而後咱們實現MainForm窗體的功能代碼:
1: using System;
2: using System.Collections.Generic;
3: using System.ComponentModel;
4: using System.Data;
5: using System.Drawing;
6: using System.Linq;
7: using System.Text;
8: using System.Windows.Forms;
9: using EAS.Sockets;
10: using Socket.Demo.Messages;
11:
12: namespace Socket.Demo.Client
13: {
14: public partial class MainForm : Form
15: {
16: public static MainForm Instance = null;
17: SocketClient m_SocketClient;
18:
19: public MainForm()
20: {
21: InitializeComponent();
22: Instance = this;
23: this.m_SocketClient = new SocketClient();
24: this.m_SocketClient.IPAddress = "127.0.0.1";
25: this.m_SocketClient.Port = 6610;
26: this.m_SocketClient.AddHander<ResponseMessage>(new ResponseMessageHandler());
27:
28: this.m_SocketClient.Connected += new EventHandler(m_SocketClient_Connected);
29: this.m_SocketClient.Closed += new EventHandler(m_SocketClient_Closed);
30: }
31:
32: void m_SocketClient_Connected(object sender, EventArgs e)
33: {
34: this.btnConnect.Enabled = false;
35: this.btnClose.Enabled = true;
36: this.btnSend.Enabled = false;
37: }
38:
39: void m_SocketClient_Closed(object sender, EventArgs e)
40: {
41: this.btnConnect.Enabled = true;
42: this.btnClose.Enabled = false;
43: this.btnSend.Enabled = true;
44: }
45:
46: internal void ShowMessage(ResponseMessage message)
47: {
48: Action UI = () =>
49: {
50: this.tbMessage.AppendText(message.Content);
51: this.tbMessage.AppendText("\r\n");
52: this.tbMessage.ScrollToCaret();
53: };
54:
55: this.Invoke(UI);
56: }
57:
58: private void btnConnect_Click(object sender, EventArgs e)
59: {
60: if (this.m_SocketClient.ClientState == ClientState.Closed)
61: {
62: this.m_SocketClient.Connect();
63: }
64: }
65:
66: private void btnClose_Click(object sender, EventArgs e)
67: {
68: if (this.m_SocketClient.ClientState != ClientState.Closed)
69: {
70: this.m_SocketClient.Connect();
71: }
72: }
73:
74: private void btnSend_Click(object sender, EventArgs e)
75: {
76: if (this.tbSend.Text.Trim().Length == 0)
77: {
78: MessageBox.Show("不能發空消息");
79: return;
80: }
81:
82: if (this.m_SocketClient.ClientState != ClientState.Closed)
83: {
84: RequestMessage rm = new RequestMessage();
85: rm.Content = this.tbSend.Text.Trim();
86: this.m_SocketClient.Send(rm);
87: }
88: }
89: }
90: }
OK,這個簡單的例子咱們已經開發完成了,接下來,咱們來編譯、測試一下這個例子!
首先咱們啓動服務端程序、啓動服務器,而後啓動客戶段並鏈接服務器,從客戶段向服務器發送消息,服務端會顯示客戶端發送的消息會向客戶端發送一個應答消息。
本文例子源代碼請經過http://42.121.30.77/downloads/eas/examples/Socket.Demo.rar進行下載,有關於AgileEAS.NET SOA 中間件平臺請經過AgilleEAS.NET SOA 中站件平臺官方網站的最新下載頁面下載。
爲了完善、改進和推廣AgileEAS.NET而成立了敏捷軟件工程實驗室,是一家研究、推廣和發展新技術,並致力於提供具備自主知識產權的業務基礎平臺軟件,以及基於業務基礎平臺了開發的管理軟件的專業軟件提供商。主要業務是爲客戶提供軟件企業研發管理解決方案、企業管理軟件開發,以及相關的技術支持,管理及技術諮詢與培訓業務。
AgileEAS.NET平臺自2004年秋呱呱落地一來,我就一直在逐步完善和改進,也被應用於保險、醫療、電子商務、房地產、鐵路、教育等多個應用,但一直都是以我我的在推廣,2010年由於我辭職休息,我就想到把AgileEAS.NET推向市場,讓更多的人使用。
個人技術團隊成員都是合做多年的老朋友,由於這個平臺是免費的,因此也沒有什麼收入,都是由程序員的那種理想與信念堅持,在此我感謝一塊兒奮鬥的朋友。
AgileEAS.NET網站:http://www.agileeas.net
官方博客:http://eastjade.cnblogs.com
github:https://github.com/agilelab/eas
QQ:47920381
AgileEAS.NET QQ羣:
113723486(AgileEAS SOA 平臺)/上限1000人
199463175(AgileEAS SOA 交流)/上限1000人
120661978(AgileEAS.NET 平臺交流)/上限1000人
212867943(AgileEAS.NET研究)/上限500人
147168308(AgileEAS.NET應用)/上限500人
172060626(深度AgileEAS.NET平臺)/上限500人
116773358(AgileEAS.NET 平臺)/上限500人
125643764(AgileEAS.NET探討)/上限500人
193486983(AgileEAS.NET 平臺)/上限500人
郵件:james@agilelab.cn,mail.james@qq.com,
電話:18629261335。