Binary data communication based on NetworkComms V3, networkcomms

Source: Internet
Author: User

Binary data communication based on NetworkComms V3, networkcomms

If both the client and the server are in the C # language, you can use the protobuf serialization method encapsulated by the NetworkComms V3 framework or the BinaryFormater serialization method, which is convenient.

Just now I discussed with my friends how to communicate with other languages based on networkComms V3, and transfer a binary array between the client and the server.

In this example, the C # client and server transmit binary data to simulate communication with other languages.

Server code:

Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. text; using System. windows. forms; using NetworkCommsDotNet. DPSBase; using NetworkCommsDotNet. tools; using NetworkCommsDotNet. connections; using NetworkCommsDotNet. connections. TCP; using System. net; namespace RawServer {public partial class Form1: Form {S EndReceiveOptions sro = new SendReceiveOptions (DPSManager. getDataSerializer <NullSerializer> (), null, null); public Form1 () {InitializeComponent ();} private void button#click (object sender, EventArgs e) {// The server starts listening to client requests // starts listening to a port // communication with other languages disable ApplicationLayerProtocolStatus TCPConnectionListener listener = new TCPConnectionListener (sro, ApplicationLayerProtocolStatus. disabled); Connection. startList Ening (listener, new IPEndPoint (IPAddress. parse (txtIP. text), int. parse (txtPort. text); button1.Text = "listening"; button1.Enabled = false; // This method contains the specific server processing method. StartListening ();} private void StartListening () {NetworkComms. disableLogging (); // array receives the byte NetworkComms. appendGlobalIncomingUnmanagedPacketHandler (header, connection, array) =>{// you can parse the byte array in this method, and then process LogInfo Based on Different Types after parsing. logMessage ("create log file", "log .txt"); for (int I = 0; I <array. length; I ++) {LogInfo. logMessage (I. toString () + "-" + array [I]. toString (), "log. txt");} // After receiving a message from the server, if you need to return data to the client, the following code can be used to simulate a data // byte [] dataToSend = new byte [] {5, 6, 7, 8}; // connection. sendUnmanagedBytes (dataToSend) ;});} private void form=formclosing_1 (object sender, FormClosingEventArgs e) {NetworkComms. shutdown (); Environment. exit (Environment. exitCode );}}}View Code

Client code:

Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. text; using System. windows. forms; using NetworkCommsDotNet. DPSBase; using NetworkCommsDotNet. tools; using NetworkCommsDotNet. connections; using NetworkCommsDotNet. connections. TCP; using System. net; namespace RawClient {public partial class Form1: Form {SendReceiveOptions sro = new SendReceiveOptions (DPSManager. getDataSerializer <NullSerializer> (), null, null); // Connection information class public ConnectionInfo connInfo = null; // Connection class newTcpConnection; public Form1 () {InitializeComponent ();} private void button1_Click_1 (object sender, EventArgs e) {connInfo = new ConnectionInfo (txtIP. text, int. parse (txtPort. text), ApplicationLayerProtocolStatus. disabled); // if it fails, the exception message newTcpConnection = TCPConnection will pop up. getConnection (connInfo, sro); button1.Enabled = false; button1.Text = "connection successful";} private void button2_Click (object sender, EventArgs e) {byte [] dataToSend = new byte [] {1, 2, 3, 4}; newTcpConnection. sendUnmanagedBytes (dataToSend);} private void form=formclosing (object sender, FormClosingEventArgs e) {NetworkComms. shutdown (); Environment. exit (Environment. exitCode);} private void Form1_Load (object sender, EventArgs e) {}// if you want to process messages sent from the server, you can listen to and analyze data on the server }}View Code

Server listening:

 

// Disable ApplicationLayerProtocolStatus TCPConnectionListener listener = new TCPConnectionListener (sro, ApplicationLayerProtocolStatus. disabled); Connection. startListening (listener, new IPEndPoint (IPAddress. parse (txtIP. text), int. parse (txtPort. text); button1.Text = "listening"; button1.Enabled = false; // This method contains the specific server processing method. StartListening ();

Listener method:

Private void StartListening () {NetworkComms. disableLogging (); // array receives the byte NetworkComms. appendGlobalIncomingUnmanagedPacketHandler (header, connection, array) =>{// you can parse the byte array in this method, and then process LogInfo Based on Different Types after parsing. logMessage ("create log file", "log .txt"); for (int I = 0; I <array. length; I ++) {LogInfo. logMessage (I. toString () + "-" + array [I]. toString (), "log. txt");} // After receiving a message from the server, if you need to return data to the client, the following code can be used to simulate a data // byte [] dataToSend = new byte [] {5, 6, 7, 8}; // connection. sendUnmanagedBytes (dataToSend );});}

Client Connection server code:

ConnInfo = new ConnectionInfo (txtIP. text, int. parse (txtPort. text), ApplicationLayerProtocolStatus. disabled); // if it fails, the exception message newTcpConnection = TCPConnection will pop up. getConnection (connInfo, sro); button1.Enabled = false; button1.Text = "connection successful ";

Code used by the client to send byte Bytes:

 private void button2_Click(object sender, EventArgs e)        {            byte[] dataToSend = new byte[] { 1, 2, 3, 4 };            newTcpConnection.SendUnmanagedBytes(dataToSend);                     }

For the convenience of observation, the data received by the server is written into a text file:

Create a log file 0-11-22-33-4

 

Download project files (excluding Communication Framework)

If you like networkComms.cn, you can discuss it.

 

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.