【源码】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 ...
随机推荐
- drop delete truncate 区别
http://jingyan.baidu.com/article/8275fc8693e11846a03cf696.html
- Normalize.css 与 reset.css
Normalize.css 与 reset.css都是初始化页面样式 不同点在于 reset.css更加粗暴,直接把所有的样式全部初始化了: Normalize.css还剩点良心,还保留了一些浏览器默 ...
- MySql5.7.12设置log-bin
什么是binlog日志 binlog日志记录了MySql数据库的增加.删除.修改操作.用来实现MySql主从复制. 设置binlog日志 在my.cnf中配置binlog日志 [mysqld] log ...
- 编译libjpeg库
最近在写车牌识别软件,需要用到BMP转成JPG的功能,自然就想到借助libjpeg来实现 OS: win7 64位 编译器: VS2008 1. 下载源代码下载地址:http://www.ijg.or ...
- Windows Azure 如何学习Azure
通过上一篇博文可以得知,Azure其实是个平台,上面跑的服务五花八门,可以相互分开使用,同时也可以相互结合. 那我们应该如何来学习Azure呢? 其实有很多种选择,正所谓条条大路通罗马, 官方的tra ...
- Oracle数据库DOC命令导入导出(2014-3-10记)
导出:exp lwj/lwj123456@orcl file=d:/db.dmp full=y 导入:imp lwj/lwj123456@orcl file=d:/db.dmp full=y 注:用户 ...
- Dynamics AX 2012 R2 AIF 错误 '/MicrosoftDynamicsAXAif60' 应用程序中的服务器错误
Reinhard在使用AIF的时候,服务端收到如下错误提示之一,并触发InsufficientMemoryException 和ServiceActivationException异常,那么代表你服务 ...
- json学习小记
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- caffe问题集锦
不断更新中... 1.问题:check failure stack trace:*** 解决方法:路径错误,重新去看自己的路径是否有错误 2.Check failed: error == cudaSu ...
- python(八)内置模块logging/os/time/sys/json/pickle
模块 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护.为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代码就相对较少 ...