【源码】c#编写的安卓客户端与Windows服务器程序进行网络通信
用c#开发安卓程序 (xamarin.android)系列之三
为了方便您测试,我临时搭建了一个服务器 您可以安装apk文件,直接测试 apk文件下载地址 (测试服务器将会运行至2015年3月1日)
通信框架为来自英国的NetworkComms2.3.1开源通信框架 序列化采用Protobuf.net开源框架
客户端界面如下:

服务器端程序界面:
服务器搭建在winserver2003 上,基于.net4.0.
数据库采用sql2005

输入数据:

数据库建设完成,打开VS2010开始,创建相关的工程

创建服务器端工程


下一步:打开CodeSmith创建“存储过程”,“数据层”代码,“逻辑层(Business层代码)”:
相关CodeSmith模板下载地址:
分享我所使用的数据库框架
使用的CodeSmith为6.5版本:

生成完成后,VS中工程图:

下一步先构建服务器代码
CREATE PROCEDURE [dbo].Users_SelectOneByUserName
@Name nvarchar()
AS
SELECT
ID,Name,PassWord
FROM
[dbo].[Users]
WHERE
[Name] = @Name
数据库中添加存储过程
DBUsers.CS中添加:
//添加 根据UserID获取用户
public static IDataReader GetOneByUserName(
string name)
{
SqlParameterHelper sph = );
sph.DefineSqlParameter(, ParameterDirection.Input, name);
return sph.ExecuteReader();
}
逻辑层DoUsers中添加:
public static string Login(string username, string password)
{
using (IDataReader reader = DBUsers.GetOneByUserName(username))
{
string theResult = "登录不成功";
Users theUser = PopulateFromReader(reader);
if (theUser == null)
{
theResult = "用户不存在";
}
else if (theUser.PassWord == password)
{
theResult = "登录成功";
}
else
{
theResult = "密码不正确";
}
return theResult;
}
}
服务器端代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NetworkCommsDotNet;
using System.Net;
using Mobile.Business;
using Mobile.Entity;
namespace MobileServer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//服务器开始监听客户端的请求
//开始监听某T端口
IPEndPoint thePoint = new IPEndPoint(IPAddress.Parse(txtIP.Text), int.Parse(txtPort.Text));
TCPConnection.StartListening(thePoint, false);
button1.Text = "监听中";
button1.Enabled = false;
//此方法中包含服务器具体的处理方法。
StartListening();
}
private void StartListening()
{
//禁用日志记录 服务器端正式使用时,禁用日志记录
NetworkComms.DisableLogging();
//处理登陆请求
NetworkComms.AppendGlobalIncomingPacketHandler<Users>("UserLogin", IncomingLoginRequest);
}
//处理某个具体的请求
private void IncomingLoginRequest(PacketHeader header, Connection connection, Users currentUser)
{
try
{
//从数据库中获取返回结果
string resMsg = DoUsers.Login(currentUser.Name,currentUser.PassWord);
ResMessage contract = new ResMessage();
contract.Message = resMsg;
//把结果返回给客户端
connection.SendObject("ResLogin", contract);
}
catch (Exception ex)
{
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
NetworkComms.Shutdown();
this.Dispose();
this.Close();
}
}
}
至此,我们已经完成了“建设数据库”,“建表”,“生成数据库存储过程“,”数据层代码“,”逻辑层代码“,”服务器端代码的编写“。只剩下安卓客户端的编写了。
借助xamarin平台,用C#语言开发安卓程序,最大的优势,个人感觉是可以使用.net平台上众多优秀的库类,特别是通过稳定成熟的通信框架与c#服务器端进行交互。


修改 Main.axml文件,增加几个文本框给用户输入用户名和密码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/ConnectButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="连接服务器" />
<TextView
android:id="@+id/tvUseName"
android:layout_width="195px"
android:layout_height="35px"
android:text="用户名:" />
<EditText
android:id="@+id/etUserName"
android:layout_width="195px"
android:layout_height="wrap_content"
android:text=""
android:textSize="18sp" />
<TextView
android:id="@+id/tvPassWord"
android:layout_width="195px"
android:layout_height="35px"
android:text="密码:" />
<EditText
android:id="@+id/etPassWord"
android:layout_width="195px"
android:layout_height="wrap_content"
android:text=""
android:textSize="18sp" />
<Button
android:id="@+id/MyButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="登陆" />
<ScrollView
android:id="@+id/mainTextScroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".1"
android:layout_above="@+id/messageTextInput">
<TextView
android:id="@+id/mainText"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:inputType="none" />
</ScrollView>
</LinearLayout>
修改Main.axml文件
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using NetworkCommsDotNet;
using DPSBase;
using System.Net;
using Mobile.Entity;
namespace Mobile.Client
{
[Activity(Label = "Mobile.Client", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
;
//连接信息对象
public ConnectionInfo connInfo = null;
//连接对象
Connection newTcpConnection;
//连接服务器
Button connButton;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
//映射登陆按钮
Button button = FindViewById<Button>(Resource.Id.MyButton);
//登陆按钮相关的方法
button.Click += loginButton_Click;
//映射连接服务器按钮
connButton = FindViewById<Button>(Resource.Id.ConnectButton);
//连接服务器按钮相关方法
connButton.Click += connButton_Click;
//TextView 显示信息
tvShowMessage = FindViewById<TextView>(Resource.Id.mainText);
//映射输入用户名控件
etUserName = FindViewById<EditText>(Resource.Id.etUserName);
//映射输入密码控件
etPassWord = FindViewById<EditText>(Resource.Id.etPassWord);
if (NetConnected())
{
AppendMessage("网络状态:可用");
}
else
{
AppendMessage("无网络,请先设置网络");
}
}
//连接服务器相关方法
void connButton_Click(object sender, EventArgs e)
{
//给连接信息对象赋值
connInfo = new ConnectionInfo(ServerIP, ServerPort);
//如果不成功,会弹出异常信息
newTcpConnection = TCPConnection.GetConnection(connInfo);
connButton.Text = "连接成功";
connButton.Enabled = false;
}
string ServerIP = "115.28.141.108";
;
//客户端登陆相关方法
void loginButton_Click(object sender, EventArgs e)
{
Users theUser = new Users();
theUser.Name = etUserName.Text;
theUser.PassWord = etPassWord.Text;
ResMessage resMessage = newTcpConnection.SendReceiveObject<ResMessage>(, theUser);
if (resMessage.Message == "登陆成功")
AppendMessage("登陆成功");
else
//显示错误信息
AppendMessage(resMessage.Message);
}
//用于显示信息的TextView
TextView tvShowMessage;
//用户名
EditText etUserName;
//密码
EditText etPassWord;
/// <summary>
/// 数据序列化器
/// </summary>
public DataSerializer Serializer { get; set; }
//显示信息
public void AppendMessage(string message)
{
tvShowMessage.Text += System.Environment.NewLine + message;
}
//判断当前网络是否可用
private bool NetConnected()
{
var cm = Context.ConnectivityService;
var cmMgr = (Android.Net.ConnectivityManager)GetSystemService(cm);
if (cmMgr.GetNetworkInfo(Android.Net.ConnectivityType.Mobile).IsConnected || cmMgr.GetNetworkInfo(Android.Net.ConnectivityType.Wifi).IsConnected)
{
return true;
}
else
{
return false;
}
}
}
}
MainActivity代码
文章先写到这儿,希望您喜欢
www.networkcomms.cn
【源码】c#编写的安卓客户端与Windows服务器程序进行网络通信的更多相关文章
- 项目源码--Android即时通讯IM客户端
下载源码 技术要点: 1.完整精美客户端UI设计 2.自定义控件的灵活使用 3.UI控件的详细使用 4.即时通讯IM协议的实现 5.完整即时通讯IM客户端实现 6.源码详细的中文注释 ……. ...
- Netty源码分析第3章(客户端接入流程)---->第1节: 初始化NioSockectChannelConfig
Netty源码分析第三章: 客户端接入流程 概述: 之前的章节学习了server启动以及eventLoop相关的逻辑, eventLoop轮询到客户端接入事件之后是如何处理的?这一章我们循序渐进, 带 ...
- Netty源码分析第3章(客户端接入流程)---->第2节: 处理接入事件之handle的创建
Netty源码分析第三章: 客户端接入流程 第二节: 处理接入事件之handle的创建 上一小节我们剖析完成了与channel绑定的ChannelConfig初始化相关的流程, 这一小节继续剖析客户端 ...
- Netty源码分析第3章(客户端接入流程)---->第3节: NioSocketChannel的创建
Netty源码分析第三章: 客户端接入流程 第三节: NioSocketChannel的创建 回到上一小节的read()方法: public void read() { //必须是NioEventLo ...
- Netty源码分析第3章(客户端接入流程)---->第4节: NioSocketChannel注册到selector
Netty源码分析第三章: 客户端接入流程 第四节: NioSocketChannel注册到selector 我们回到最初的NioMessageUnsafe的read()方法: public void ...
- Netty源码分析第3章(客户端接入流程)---->第5节: 监听读事件
Netty源码分析第三章: 客户端接入流程 第五节: 监听读事件 我们回到AbstractUnsafe的register0()方法: private void register0(ChannelPro ...
- Zookeeper 源码(三)Zookeeper 客户端源码
Zookeeper 源码(三)Zookeeper 客户端源码 Zookeeper 客户端主要有以下几个重要的组件.客户端会话创建可以分为三个阶段:一是初始化阶段.二是会话创建阶段.三是响应处理阶段. ...
- CentOS 7上源码编译安装和配置LNMP Web+phpMyAdmin服务器环境
CentOS 7上源码编译安装和配置LNMP Web+phpMyAdmin服务器环境 什么是LNMP? LNMP(别名LEMP)是指由Linux, Nginx, MySQL/MariaDB, PHP/ ...
- [源码解析] NVIDIA HugeCTR,GPU 版本参数服务器 --(1)
[源码解析] NVIDIA HugeCTR,GPU版本参数服务器 --(1) 目录 [源码解析] NVIDIA HugeCTR,GPU版本参数服务器 --(1) 0x00 摘要 0x01 背景 1.1 ...
随机推荐
- CentOS-6 yum安装nginx php53 mysql55 搭建LNMP环境
1.导入外部软件库 01.rpm -Uvh http://dl.iuscommunity.org/pub/ius/stable/Redhat/6/x86_64/epel-release-6-5.noa ...
- windows系统调用 临界区机制
#include "iostream" #include "windows.h" #include "cstring" using name ...
- MySql5.7.12设置log-bin
什么是binlog日志 binlog日志记录了MySql数据库的增加.删除.修改操作.用来实现MySql主从复制. 设置binlog日志 在my.cnf中配置binlog日志 [mysqld] log ...
- Spring.Net实现IOC和DI
一:在Asp.net MVC中应该怎样使用Spring.Net?1:先导入dll文件. 2:将案例中的Config文件夹拷贝到项目中. 3:修改Config文件夹中的相关的配置信息. type里面填写 ...
- SVN使用教程总结
SVN简介: 为什么要使用SVN? 程序员在编写程序的过程中,每个程序员都会生成很多不同的版本,这就需要程序员有效的管理代码,在需要的时候可以迅速,准确取出相应的版本. Subversion是什么? ...
- 161213、Maven资源替换和Freemarker模板
先介绍一下本文的两位主角: Apache Maven - 正当红的项目管理工具 FreeMarker - 老牌的模板引擎 两者貌似互不相干,何来冲突呢? 原来Maven有一个内置的资源替换机制, 可以 ...
- Why does pthread_cond_signal not work?【转】
转自:http://stackoverflow.com/questions/16819169/why-does-pthread-cond-signal-not-work# 0 down vote fa ...
- 今日随笔:scrollTop与overflow
今天想写一个页面一加载滚动条就自动滚到底部的效果,结果在IE上实现成功了,chrome上完全没反应,最后测试了一下,居然是因为css文件中,html,body都写了overflow:auto这一语句, ...
- AOJ 0121: Seven Puzzle (BFS DP STL 逆向推理)(转载)
转载自: http://m.blog.csdn.net/blog/Enjoying_Science/42008801 题目链接:http://acm.hust.edu.cn/vjudge/probl ...
- HDU 4048 Zhuge Liang's Stone Sentinel Maze
Zhuge Liang's Stone Sentinel Maze Time Limit: 10000/4000 MS (Java/Others) Memory Limit: 32768/327 ...