【源码】c#编写的安卓客户端与Windows服务器程序进行网络通信

Sunny5816 2015-02-22 06:27:17
用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(200)

AS


SELECT
ID,Name,PassWord

FROM
[dbo].[Users]

WHERE[code=csharp]

[Name] = @Name

[/code]

DBUsers.CS中添加:
//添加 根据UserID获取用户
public static IDataReader GetOneByUserName(
string name)
{
SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "Users_SelectOneByUserName", 1);
sph.DefineSqlParameter("@Name", SqlDbType.NVarChar, 200, 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文件


以及对MainActivity.cs文件进行相应的修改

www.networkcomms.cn 编辑
...全文
375 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
程序员鼓励师 2015-02-24
  • 打赏
  • 举报
回复
是不错的教程
wyx100 2015-02-23
  • 打赏
  • 举报
回复
Sunny5816 2015-02-22
  • 打赏
  • 举报
回复
本拉灯 2015-02-22
  • 打赏
  • 举报
回复
E。。不是原生安卓。。

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧