Developing the Android Program (xamarin.android) series three in C #
Source (contains client and server-side all project files) database file
In order to facilitate your testing, I temporarily set up a server you can install the APK file, directly test the APK file (the test server will run until March 1, 2015)
Communication framework for NetworkComms2.3.1 Open source communication framework serialized from UK using protobuf.net open source framework
The client interface is as follows:
Server-Side Program interface:
The server is built on winserver2003, based on. net4.0.
Database with sql2005
Input data:
Database construction complete, open VS2010 Start, create related projects
Create a server-side project
Next: Open codesmith create "stored procedure", "Data Layer" code, "logical layer (business layer code)":
Related Codesmith templates:
share the database framework I'm using
The Codesmith used is version 6.5:
When the build is complete, vs in the drawing:
Next, build the server code first
Adding stored procedures to the database
Added in DBUsers.CS:
//add a user based on UserID Public StaticIDataReader Getonebyusername (stringname) {Sqlparameterhelper SPH=NewSqlparameterhelper (Getreadconnectionstring (),"Users_selectonebyusername",1); Sph. Definesqlparameter ("@Name", SqlDbType.NVarChar, $, ParameterDirection.Input, name); returnSPH. ExecuteReader (); }
Add the logical layer Dousers:
Public Static stringLogin (stringUsernamestringpassword) { using(IDataReader reader =Dbusers.getonebyusername (username)) { stringTheresult ="Login not successful"; Users Theuser=Populatefromreader (reader); if(Theuser = =NULL) {Theresult="user does not exist"; } Else if(Theuser.password = =password) {Theresult="Landing Success"; } Else{Theresult="The password is not correct"; } returnTheresult; } }
Server-side code:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingnetworkcommsdotnet;usingSystem.Net;usingmobile.business;usingmobile.entity;namespacemobileserver{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } Private voidButton1_Click (Objectsender, EventArgs e) { //server starts listening for client requests//start listening on a T portIPEndPoint Thepoint =NewIPEndPoint (Ipaddress.parse (Txtip.text),int. Parse (Txtport.text)); Tcpconnection.startlistening (Thepoint,false); Button1. Text="Listening in"; Button1. Enabled=false; //This method contains server-specific processing methods. startlistening (); } Private voidstartlistening () {//Disable logging when the server side of logging is disabled for official usenetworkcomms.disablelogging (); //Handling Login RequestsNetworkcomms.appendglobalincomingpackethandler<users> ("Userlogin", incomingloginrequest); } //to process a specific request Private voidincomingloginrequest (packetheader header, Connection Connection, Users currentUser) {Try { //get the returned results from the database stringResmsg =Dousers.login (Currentuser.name,currentuser.password); Resmessage Contract=NewResmessage (); Contract. Message=resmsg; //return the results to the clientConnection. SendObject ("Reslogin", contract); } Catch(Exception ex) {}}Private voidForm1_formclosing (Objectsender, FormClosingEventArgs e) {Networkcomms.shutdown (); This. Dispose (); This. Close (); } }}
At this point, we have completed the "Construction database", "Build Table", "Generate database stored procedure", "Data layer Code", "Logical Layer Code", "Writing server-side code". Only the Android client was written.
With the Xamarin platform, the biggest advantage of developing Android programs in the C # language is that you can use the many excellent library classes on the. NET platform, especially through a stable and mature communication framework to interact with the C # server side.
To modify the Main.axml file, add a few text boxes to the user to enter the user name and password:
Modify the Main.axml filemainactivity Code
The article first wrote here, I hope you like
www.networkcomms.cn Edit
"Source" C # written by the Android client and Windows Server program for network communication