一、AgileEAS.NET SOA中介軟體Socket/Tcp框架介紹
在文章AgileEAS.NET SOA 中介軟體平臺Socket/Tcp通訊框架介紹一文之中我們對AgileEAS.NET SOA中間Socket/Tcp框架進行了總體的介紹,我們知道
AgileEAS.NET SOA中介軟體Socket/Tcp框架是一套Socket通訊的訊息中介軟體:
二、簡單例子-實現簡單的伺服器客戶段訊息應答
我們接下來實現一個簡單的例子,例子的場景非常的簡單,客戶端向服務端傳送一個請求訊息,服務段收到訊息之後顯示訊息內容,並對客戶端傳送一個應答訊息,客戶端收到應答訊息之後顯示這個應答訊息,對於本例子,我們使用WinForm應用來實現。
三、一步一步實現
首先我們下載AgileEAS.NET SOA 中介軟體最新版本5.0/5.1,下載地址:http://www.smarteas.net/page/EAS_NET_Downloads.aspx。
首先我們開啟VS2010或者VS2010建立一個類為專案Socket.Demo.Messages:
刪除預設的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:
1: [Serializable]
2: [Message("D94C51C4-8423-4BF6-A011-184265BBA62C", "應答訊息")]
3: public class ResponseMessage : RequestMessage, IMessage
4: {
5: }
在本例子之中應答訊息與請求訊息其內容沒有本質的區別,都是文字通訊,所以就直接繼承了RequestMessage,變更了MessageID和訊息說明。
到此為止,我們已經完成了請求訊息與應答訊息的定義Socket.Demo.Messages專案結構下:
接下來我們來實現服務端程式,向解決方案新增一個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推向市場,讓更多的人使用。
我的技術團隊成員都是合作多年的老朋友,因為這個平臺是免費的,所以也沒有什麼收入,都是由程式設計師的那種理想與信念堅持,在此我感謝一起奮鬥的朋友。
團隊網站:http://www.agilelab.cn,
AgileEAS.NET網站:http://www.smarteas.net
官方部落格:http://eastjade.cnblogs.com
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。