Using binary data communication based on Networkcomms V3

Source: Internet
Author: User

If both the client and the server are C # languages, it is convenient to use the Networkcomms V3 Framework, the PROTOBUF serialization of the wrapper, or the Binaryformater way to serialize it.

Just talking to a friend. Based on Networkcomms V3 communicates with other languages, a binary array is passed between the client and server side.

This example implements the C # client-server delivery of binary data to mimic communication with other languages

Server-side code:

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingnetworkcommsdotnet;usingnetworkcommsdotnet.dpsbase;usingNetworkcommsdotnet.tools;usingnetworkcommsdotnet.connections;usingNetworkCommsDotNet.Connections.TCP;usingSystem.Net; namespacerawserver{ Public Partial classform1:form {sendreceiveoptions SRO=NewSendreceiveoptions (dpsmanager.getdataserializer<nullserializer> (),NULL,NULL);  PublicForm1 () {InitializeComponent (); }        Private voidButton1_Click (Objectsender, EventArgs e) {            //server starts listening for client requests//start listening on a port//communication with other languages disabled ApplicationlayerprotocolstatusTcpconnectionlistener listener=NewTcpconnectionlistener (sro,applicationlayerprotocolstatus.disabled); Connection.startlistening (Listener,NewIPEndPoint (Ipaddress.parse (Txtip.text),int.                      Parse (Txtport.text)); Button1. Text="Listening in"; Button1. Enabled=false; //This method contains server-specific processing methods. startlistening (); }        Private voidstartlistening () {networkcomms.disablelogging (); //Array received bytesNetworkcomms.appendglobalincomingunmanagedpackethandler (header, connection, array) =            {                //The byte array can be parsed in this method, parsed out and then processed by different typesLoginfo.logmessage ("Create a log file","log. txt");  for(inti =0; I < array. Length; i++) {loginfo.logmessage (i.tostring ()+" - "+ Array[i]. ToString (),"log. txt"); }                //after receiving the server message, if you need to return data to the client, you can use code similar to the following//simulate a data//byte[] datatosend = new byte[] {5,6,7,8}; //connection. Sendunmanagedbytes (Datatosend);             }); }             Private voidForm1_formclosing_1 (Objectsender, FormClosingEventArgs e)            {Networkcomms.shutdown ();        Environment.exit (Environment.exitcode); }    }}
View Code

Client code:

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingnetworkcommsdotnet;usingnetworkcommsdotnet.dpsbase;usingNetworkcommsdotnet.tools;usingnetworkcommsdotnet.connections;usingNetworkCommsDotNet.Connections.TCP;usingSystem.Net;namespacerawclient{ Public Partial classform1:form {sendreceiveoptions SRO=NewSendreceiveoptions (dpsmanager.getdataserializer<nullserializer> (),NULL,NULL); //connection Information Class         PublicConnectionInfo Conninfo =NULL; //Connection ClassConnection newtcpconnection;  PublicForm1 () {InitializeComponent (); }                  Private voidButton1_click_1 (Objectsender, EventArgs e) {Conninfo=NewConnectionInfo (Txtip.text,int.            Parse (Txtport.text), applicationlayerprotocolstatus.disabled); //if not successful, the exception message pops upNewtcpconnection =tcpconnection.getconnection (CONNINFO,SRO); Button1. Enabled=false; Button1. Text="Connection Successful"; }                Private voidButton2_Click (Objectsender, EventArgs e) {            byte[] Datatosend =New byte[] {1,2,3,4 };                     Newtcpconnection.sendunmanagedbytes (Datatosend); }        Private voidForm1_formclosing (Objectsender, FormClosingEventArgs e)            {Networkcomms.shutdown ();        Environment.exit (Environment.exitcode); }        Private voidForm1_Load (Objectsender, EventArgs e) {        }        //If you need to process messages from the server, you can mimic the server-side notation, listen and analyze the data    }}
View Code

Server-Side monitoring:

 //  communication with other languages Disable Applicationlayerprotocolstatus  tcpconnectionlistener listener=new Tcpconnectionlistener (sro,applicationlayerprotocolstatus.disabled); Connection.startlistening (Listener,  int   =   listen   " ; Button1.            Enabled  = false  ;  //  startlistening (); 

Monitoring method:

Private voidstartlistening () {networkcomms.disablelogging (); //Array received bytesNetworkcomms.appendglobalincomingunmanagedpackethandler (header, connection, array) =            {                //The byte array can be parsed in this method, parsed out and then processed by different typesLoginfo.logmessage ("Create a log file","log. txt");  for(inti =0; I < array. Length; i++) {loginfo.logmessage (i.tostring ()+" - "+ Array[i]. ToString (),"log. txt"); }                //after receiving the server message, if you need to return data to the client, you can use code similar to the following//simulate a data//byte[] datatosend = new byte[] {5,6,7,8}; //connection. Sendunmanagedbytes (Datatosend);             }); }

The code for the client's connection server:

New int . Parse (Txtport.text), applicationlayerprotocolstatus.disabled);             // if not successful, the exception message            pops up Newtcpconnection = tcpconnection.getconnection (CONNINFO,SRO);             false ;             " Connection Successful ";

The client sends the code for byte byte data:

Private void button2_click (object  sender, EventArgs e)        {            bytenew  byte1234  };            Newtcpconnection.sendunmanagedbytes (datatosend);                     }

The data received by the server is written in a text file for ease of observation:

Create a log file 0 1 1 2 2 3 3 4

Project file download (without communication framework)

Friends who like Networkcomms.cn can discuss the next

Using binary data communication based on Networkcomms V3

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.