Client Server Communication demo

Source: Internet
Author: User

Some time ago, a few friends who studied esframework told me that esframework is a little huge, and it means a little "cool" for their current projects, because their projects do not require file transfer, P2P, friend relationship, group broadcast, server balance, cross-server communication, or even userid, as long as the client can communicate with the server in a simple, stable, and efficient manner. So they suggested that I use a lightweight communication component to meet the needs of projects similar to them. I think this suggestion makes sense, so it took me a few days to pull out the esframework kernel. After modification and encapsulation, The striveengine was formed, its biggest feature is its stability, efficiency, and ease of use. Through the simple demo below, we should be able to get started. At the end of this article, we will download the demo source code. Let's first go to the demo:

1. Demo Introduction

The demo consists of three projects:

1. striveengine. simpledemoserver: server developed based on striveengine.

2. striveengine. simpledemoclient: a client developed based on striveengine.

3. striveengine. simpledemo: a client developed directly Based on. NET socket. The purpose is to demonstrate how to communicate with the server based on striveengine without using striveengine.

Striveengine has built-in support for TCP/UDP, text protocol, and binary Protocol. In this demo, we use the Message Protocol in TCP and text format, and the message Terminator is "\ 0 ".

2. Demo Server

 

Private itcpserverengine tcpserverengine; private void button1_click (Object sender, eventargs e) {try {// initialize and start the server engine (TCP and text protocols) This. tcpserverengine = networkenginefactory. createtexttcpserverengine (Int. parse (this. textbox_port.text), new defaulttextcontracthelper ("\ 0"); this. tcpserverengine. clientcountchanged + = new cbdelegate <int> (tcpserverengine_clientcountchanged); this. tcpserverengine. clientco Nnected + = new cbdelegate <system. net. ipendpoint> (tcpserverengine_clientconnected); this. tcpserverengine. clientdisconnected + = new cbdelegate <system. net. ipendpoint> (tcpserverengine_clientdisconnected); this. tcpserverengine. messagereceived + = new cbdelegate <ipendpoint, byte []> (tcpserverengine_messagereceived); this. tcpserverengine. initialize (); this. button1.enabled = false; this. textbox_port.readonl Y = true; this. button2.enabled = true;} catch (exception ee) {MessageBox. show (EE. message) ;}} void tcpserverengine_messagereceived (ipendpoint client, byte [] BMSG) {string MSG = system. text. encoding. utf8.getstring (BMSG); // The message uses UTF-8-encoded MSG = MSG. substring (0, MSG. length-1); // remove the end mark "\ 0" from this. showclientmsg (client, MSG);} void tcpserverengine_clientdisconnected (system. net. ipendpoint IPE) {stri Ng MSG = string. format ("{0} deprecate", IPE); this. showevent (MSG);} void tcpserverengine_clientconnected (system. net. ipendpoint IPE) {string MSG = string. format ("{0} online", IPE); this. showevent (MSG);} void tcpserverengine_clientcountchanged (INT count) {This. showconnectioncount (count);} private void showevent (string MSG) {If (this. invokerequired) {This. begininvoke (New cbdelegate <string> (this. showevent ), MSG);} else {This. toolstriplabel_event.text = MSG;} private void showclientmsg (ipendpoint client, string MSG) {If (this. invokerequired) {This. begininvoke (New cbdelegate <ipendpoint, string> (this. showclientmsg), client, MSG);} else {listviewitem item = new listviewitem (New String [] {datetime. now. tostring (), client. tostring (), MSG}); this. listview1.items. insert (0, item) ;}} private void Showconnectioncount (INT clientcount) {If (this. invokerequired) {This. begininvoke (New cbdelegate <int> (this. showconnectioncount), clientcount);} else {This. toolstriplabel_clientcount.text = "online quantity:" + clientcount. tostring () ;}} private void comboboxincludropdown (Object sender, eventargs e) {list <ipendpoint> List = This. tcpserverengine. getclientlist (); this. combobox1.datasource = List;} private v Oid button2_click (Object sender, eventargs e) {try {ipendpoint client = (ipendpoint) This. combobox1.selecteditem; If (client = NULL) {MessageBox. show ("no online client is selected! "); Return;} If (! This. tcpserverengine. isclientonline (client) {MessageBox. Show ("the target client is not online! "); Return;} string MSG = This. textbox_msg.text + "\ 0"; // "\ 0" indicates the end of a message byte [] bmsg = system. text. encoding. utf8.getbytes (MSG); // The message uses UTF-8 encoding this. tcpserverengine. sendmessagetoclient (client, BMSG);} catch (exception ee) {MessageBox. show (EE. message );}}

 

The following describes how to use the server engine:

(1) first, call the createtexttcpserverengine method of networkenginefactory to create an engine (server, TCP, and text protocols ).

(2) reserve certain events (such as messagereceived events) of the engine instance as needed ).

(3) Call the initialize method of the engine instance to start the communication engine.

(4) Call the sendmessagetoclient method of the server engine to send a message to the client.

3. Demo Client

Private itcppassiveengine tcppassiveengine; private void button3_click (Object sender, eventargs e) {try {// initialize and start the Client Engine (TCP and text protocols) This. tcppassiveengine = networkenginefactory. createtexttcppassiveengine (this. textbox_ip.text, Int. parse (this. textbox_port.text), new defaulttextcontracthelper ("\ 0"); this. tcppassiveengine. messagereceived + = new cbdelegate <system. net. ipendpoint, byte []> (tcppassiveengine _ Messagereceived); this. tcppassiveengine. autoreconnect = true; // automatically reconnect to this when a disconnection occurs. tcppassiveengine. connectioninterrupted + = new cbdelegate (tcppassiveengine_connectioninterrupted); this. tcppassiveengine. connectionrebuildsucceed + = new cbdelegate (tcppassiveengine_connectionrebuildsucceed); this. tcppassiveengine. initialize (); this. button2.enabled = true; this. button3.enabled = false; MessageBox. show ("connect Success! ");} Catch (exception ee) {MessageBox. show (EE. message) ;}} void tcppassiveengine_connectionrebuildsucceed () {If (this. invokerequired) {This. begininvoke (New cbdelegate (this. tcppassiveengine_connectioninterrupted);} else {This. button2.enabled = true; MessageBox. show ("reconnect successful. ") ;}} Void tcppassiveengine_connectioninterrupted () {If (this. invokerequired) {This. begininvoke (New cbdelegate (this. tcppassiveengine_connectioninterrupted);} else {This. button2.enabled = false; MessageBox. show ("You have dropped. ") ;}} Void tcppassiveengine_messagereceived (system. net. ipendpoint serveripe, byte [] BMSG) {string MSG = system. text. encoding. utf8.getstring (BMSG); // The message uses UTF-8-encoded MSG = MSG. substring (0, MSG. length-1); // remove the end mark "\ 0" from this. showmessage (MSG);} private void showmessage (string MSG) {If (this. invokerequired) {This. begininvoke (New cbdelegate <string> (this. showmessage), MSG);} else {listviewitem item = new listviewitem (New String [] {datetime. now. tostring (), MSG}); this. listview1.items. insert (0, item) ;}} private void button2_click (Object sender, eventargs e) {string MSG = This. textbox_msg.text + "\ 0"; // "\ 0" indicates the end of a message byte [] bmsg = system. text. encoding. utf8.getbytes (MSG); // The message uses UTF-8 encoding this. tcppassiveengine. sendmessagetoserver (BMSG );}

The use of the Client Engine is similar to that of the server:

(1) first, call the createtexttcppassiveengine method of networkenginefactory to create an engine (client, TCP, and text protocols ).

(2) reserve certain events (such as messagereceived and connectioninterrupted) of the engine instance as needed ).

(3) set certain attributes of the engine instance (such as the autoreconnect attribute) as needed ).

(4) Call the initialize method of the engine instance to start the communication engine.

(5) Call the sendmessagetoserver method of the Client Engine to send a message to the server.

4. socket-based clients

This client is directly based on. net socket development, which demonstrates how to communicate with the striveengine-based server without using striveengine on the client (for example, the client is a heterogeneous system. This client is just a rough implementation of the basic purpose, and many details are ignored, such as the packet sticking problem, message restructuring, and disconnection detection. These problems must be solved in practical applications. (Both clients and server engines in striveengine solve these problems ).
TheCodeYou can see it in the source code.

5. Download source code

Download the demo source code.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.