"C # source" Client disconnection based on TCP communication

Source: Internet
Author: User

SOURCE download

In CS programs, the disconnection should be a common feature.

Here the disconnection is mainly refers to the server side because of some kind of failure, the server-side program or the system has been restarted, the client can automatically detect the server-side drop, and try to reconnect

This program is based on the Networkcomms (2.3.1 version) of the Open source C # Communication framework from the UK

First look at the effect

Initial state:

650) this.width=650; "src=" http://images.cnitblog.com/blog/586310/201502/272023554394790.jpg "style=" border:0px; " />

When the server-side program shuts down, the client automatically detects and displays the relevant information on the client

650) this.width=650; "src=" http://images.cnitblog.com/blog/586310/201502/272029351741566.jpg "style=" border:0px; " />

Then, we set to re-connect every 5 seconds, you can customize the number of times to re-connect, such as to re-connect 50 times, if not re-connected successfully, then discard the re-connect

650) this.width=650; "src=" http://images.cnitblog.com/blog/586310/201502/272030053778871.jpg "style=" border:0px; " />

Then we restart the server side and the client will show that the reconnect was successful.

650) this.width=650; "src=" http://images.cnitblog.com/blog/586310/201502/272030414247659.jpg "style=" border:0px; " />

The steps are as follows:

You need to modify the code in several NetworkComms2.3.1 communication frameworks

First step: Modify the Noteconnectionshutdown method of the ConnectionInfo class

The method turns out to be:

650) this.width=650; "id=" Code_img_closed_80803bce-5e4c-4ffa-9a1e-68fcf55873e5 "class=" code_img_closed "src=" http ://images.cnblogs.com/outliningindicators/contractedblock.gif "style=" border:0px;vertical-align:middle; padding-right:5px; "/> View Code

Modified to:

650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" Border:none; "/>

   private bool reconnectFlag = false;         /// <summary>        ///  whether to reconnect mode          /// </summary>         public bool ReconnectFlag        {             get { return reconnectFlag; }             set { reconnectFlag =  value; }        }          <summary>        /// Note this  Connection as shutdown        /// </summary>      &nbsP;  internal void noteconnectionshutdown ()          {            lock  (InternalLocker)                  connectionstate  = ConnectionState.Shutdown;             Add the following code    initial status to false   trigger connection state Change Event              if  (Reconnectflag == false)              {                 statechanged.raise (This, new stringeventargs ("Connection exception"));             }        }         //Add state Change Event          public event EventHandler< stringeventargs> statechanged;

650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" Border:none; "/>

Step two: Add the relevant code to the Networkcomms library class as follows:

650) this.width=650; "id=" code_img_closed_bd73f2bc-fb97-458d-aff6-0234b0ea4624 "class=" code_img_closed "src=" http ://images.cnblogs.com/outliningindicators/contractedblock.gif "style=" border:0px;vertical-align:middle; padding-right:5px; "/> related code

Step Three: Add the following method to the Networkcomms static class:

650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" Border:none; "/>

 public  static void cleardic ()         {             lock  (Globaldictanddelegatelocker)              {                 allconnectionsbyid.clear ();                 allconnectionsbyendpoint.clear ();                  Oldnetworkidentifiertoconnectioninfo.clear ();             }        } 

650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" Border:none; "/>

If you are using the V3 version, the code changes slightly:

650) this.width=650; "id=" code_img_closed_4dab5cb6-ec12-4ce9-ab68-667845cef98c "class=" code_img_closed "src=" http ://images.cnblogs.com/outliningindicators/contractedblock.gif "style=" border:0px;vertical-align:middle; padding-right:5px; "/> V3

Client code:

650) this.width=650; "id=" Code_img_closed_fc644620-2ce9-465d-8dde-843c82a498b8 "class=" code_img_closed "src=" http ://images.cnblogs.com/outliningindicators/contractedblock.gif "style=" border:0px;vertical-align:middle; padding-right:5px; "/> client code

No additional settings are required on the server side.

At this point, our work has been completed.

I wish you all a smooth work

www.networkcomms.cn

"C # source" Client disconnection based on TCP communication

Related Article

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.