Original URL: Http://www.cnblogs.com/csdev
Networkcomms is a C # language written by TCP/UDP communication framework The author is the British used to charge the current author has open source address is: Https://github.com/MarcFletcher/NetworkComms.Net
When using the NETWORKCOMMS framework to communicate, the client sends a message and the server-side replies to the message.
In the article introducing the open source. NET Communication Framework Networkcomms, we describe how to send a string from the client to the server side, and how to receive the returned string from the server side.
This article describes how to send custom class data to the server side, and how to get the data returned from the service side.
Create a new class library
Add a reference to the Protobuf.dll, which is in the Marcf-networkcomms.net-8e01e19f827f\packages\protobuf-net.2.0.0.668\lib related folder
This reference is added because the protobuf is used for serialization when communicating
Add 2 classes that can be serialized
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingProtobuf;namespacedemo1.business{[Protocontract] Public classResmessage { Publicresmessage () {} [Protomember (1)] Public stringMessage {Get;Set; } }}
View Code
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingProtobuf;namespacedemo1.business{[Protocontract] Public classUser { PublicUser () {} [Protomember (1)] Public stringUserID {Get;Set; } [Protomember (2)] Public stringName {Get;Set; } }}
View Code
Adding a reference to the Demo1.business class in Demo1.client and Demo1.server
Client:
Client code:
User theuser =NewUser (); Theuser.userid=TxtName.Text.Trim (); Theuser.name=TxtPsw.Text.Trim (); Resmessage Res= Newtcpconnection.sendreceiveobject<user, resmessage> ("Userlong","Reslogin", the, theuser); if(Res. Message = ="Validation Successful") {MessageBox.Show ("User authentication Successful"); } Else{MessageBox.Show (res). Message); }
Server-side code:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingnetworkcommsdotnet.connections;usingnetworkcommsdotnet;usingnetworkcommsdotnet.dpsbase;usingNetworkcommsdotnet.tools;usingNetworkCommsDotNet.Connections.TCP;usingSystem.Net;usingdemo1.business;namespacedemo1.server{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } Private voidButton1_Click (Objectsender, EventArgs e) { //IP address and portIPEndPoint Thepoint =NewIPEndPoint (Ipaddress.parse (Txtip.text),int. Parse (Txtport.text)); //start listening for this IP and port using the TCP protocolconnection.startlistening (connectiontype.tcp, thepoint); Networkcomms.appendglobalincomingpackethandler<string> ("GetName", Incomingmsghandle); Networkcomms.appendglobalincomingpackethandler<User> ("Userlong", Incoinghandlelogin); Button1. Text="already started listening ."; } Private voidIncomingmsghandle (packetheader header, Connection Connection,stringmsg) { Try { stringResmsg =""; if(msg = ="Monday") resmsg="Monday"; Else if(msg = ="Tuesday") resmsg="Tuesday"; Else if(msg = ="Wednesday") resmsg="Wednesday"; Else if(msg = ="Thursday") resmsg="Thursday"; Else if(msg = ="Friday") resmsg="Friday"; Else if(msg = ="Saturday") resmsg="Saturday"; Else if(msg = ="Sunday") resmsg="Sunday"; Connection. SendObject ("Resname", resmsg); } Catch(Exception ex) {}}Private voidIncoinghandlelogin (packetheader header, Connection Connection, User theuser) {resmessage msg=NewResmessage (); if(Theuser.userid = =" +"&& Theuser.name = ="Zhang San") Msg. Message="Login Successful"; ElseMsg. Message="user does not exist"; Connection. SendObject ("Reslogin", MSG); } }}
This means that the communication using PROTOBUF for serialization is complete. Because Neworkcomms uses PROTOBUF for communication by default, additional settings are not required. If you use a different serializer, you can also set it up conveniently.
Original Address Www.cnblogs.com/csdev
Source: Http://pan.baidu.com/s/1dFNrMtN
Introducing the two-pass class of the open source. NET Communication Framework NETWORKCOMMS framework