在上一节中,还有即将给出的服务器代码,都使用了共同的工具类,包括一个Command类,用来负责表示通讯中的命令控制,一个ServerConfig类,用来表示服务器的配置,这个在客户端中也要使用,还有一个验证用户信息的类,在客户端使用。因为这三个类的通用新比较高,所以单独用一个dll工程来开发的这三个类,这样在客户端工程和服务器端工程中直接可以添加这个新的工程的DLL的引用。
整个项目源码下载地址:http://download.csdn.net/detail/weixingstudio/4301232
Command类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Runtime.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace UtilityClass
{
/// <summary>
/// 通讯系统用的命令
/// </summary>
public class Command
{
public const string Connect = "00"; // 连接
public const string ConnectConfirm = "01"; // 连接确认
public const string DisConnect = "02"; // 请求离开
public const string RequestOnlineUser = "03"; // 请求在线用户列表
public const string VibrateAll = "04"; // 请求对所有人震动
public const string VibrateOne = "05"; // 请求对某人震动
public const string BroadcastAll = "06"; // 对所有人广播消息
public const string SendMessage2One = "07"; // 对指定人传送消息
public const string RedundantUserName = "10"; // 重复的用户名
public const string SomeoneConnected = "11"; // 通知有人上线
public const string SomeoneLeaveUnexpected = "12"; // 某人异常离开
public const string SomeoneLeave = "13"; // 有人离开
public const string ServerShutdown = "14"; // 服务器关闭
public const string ExceedMaxAllowedNumber = "15"; // 超出最大的允许连接数
public const string SendOnlinUserList = "16"; // 发送用户列表
/// <summary>
/// 编码命令,返回命令的字节流
/// </summary>
/// <param name="cmd">需要编码的命令</param>
/// <returns>返回字节流</returns>
public static byte[] EncodeCommand(string cmd)
{
byte[] command_byte = Encoding.Unicode.GetBytes(cmd);
return command_byte;
}
/// <summary>
/// 解码系统命令,返回字符串
/// </summary>
/// <param name="cmd">待解码的密令</param>
/// <returns>返回字符串</returns>
public static string DecodeCommand(byte[] cmd)
{
string cmd_string = Encoding.Unicode.GetString(cmd).TrimEnd('\0');
return cmd_string;
}
/// <summary>
/// 序列化在线用户列表
/// </summary>
/// <param name="onlineuser">序列化数组</param>
/// <returns>返回结果</returns>
public static byte[] SerializeOnlineUser(string[] onlineuser)
{
StringCollection onlineuserCollection = new StringCollection();
foreach (string user in onlineuser)
{
onlineuserCollection.Add(user);
}
IFormatter formatter = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
formatter.Serialize(stream, onlineuserCollection);
byte[] result = stream.ToArray();
stream.Close();
return result;
}
/// <summary>
/// 反序列化在线用户列表
/// </summary>
/// <param name="userbyte">输入的字节流</param>
/// <returns>返回字符串集合</returns>
public static StringCollection DeserializeOnlineUser(byte[] userbyte)
{
IFormatter format = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
stream.Write(userbyte, 0, userbyte.Length);
stream.Position = 0;
StringCollection onlineList = (StringCollection)format.Deserialize(stream);
return onlineList;
}
/// <summary>
/// 检查要传送的消息是否正确
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public static bool VerifyMessage(string message)
{
if (!message.Equals(""))
{
// 检查是否都是空格
char[] temp = message.ToArray();
bool flag = false;
foreach (char c in temp)
{
if (c!=' '&& c!='\n' && c!=13)
{
flag = true;
break;
}
}
if (flag==true)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
}
}
ServerConfig类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
namespace UtilityClass
{
public class ServerConfig
{
// server ip
private string serverIP = "10.108.13.27";
// server port
private int serverPort = 8888;
// max buffer for receive data from client, 56K
private int maxBuffer = 56 * 1024;
// IPAddress
private IPAddress ipAddress;
// max allowed online user
private int maxAllowedOnlineUser = 100;
public ServerConfig()
{
//
}
public ServerConfig(string ip, int port)
{
this.serverIP = ip;
this.serverPort = port;
}
public ServerConfig(string ip)
{
this.serverIP = ip;
this.serverPort = 8888;
}
public ServerConfig(int port)
{
this.serverIP = "127.0.0.1";
this.serverPort = port;
}
/// <summary>
/// 服务器IP地址
/// </summary>
public string ServerIP
{
get { return serverIP; }
set { serverIP = value; }
}
/// <summary>
/// 服务器监听端口
/// </summary>
public int ServerPort
{
get { return serverPort; }
set { serverPort = value; }
}
/// <summary>
/// 最大数据缓冲区
/// </summary>
public int MaxBuffer
{
get { return maxBuffer; }
set { maxBuffer = value; }
}
/// <summary>
/// 最大客户端连接数量
/// </summary>
public int MaxAllowedOnlineUser
{
get { return maxAllowedOnlineUser; }
set { maxAllowedOnlineUser = value; }
}
}
}
VerifyUserInfo类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UtilityClass
{
/// <summary>
/// 验证用户输入数据用的类
/// </summary>
public class VerifyUserInfo
{
/// <summary>
/// 验证用户输入用户名的合法性
/// </summary>
/// <param name="name">输入名</param>
/// <returns>结果</returns>
public bool VerifyUserName(string name)
{
if (name!="")
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 验证用户输入的IP知否正确
/// </summary>
/// <param name="ip">输入字符串</param>
/// <returns>输出结果</returns>
public bool VerifyServerIP(string ip)
{
if (ip=="")
{
return false;
}
else
{
string[] ipA = ip.Split('.');
if (ipA.Length!=4)
{
return false;
}
else
{
int ip1, ip2, ip3, ip4;
try
{
ip1 = int.Parse(ipA[0]);
ip2 = int.Parse(ipA[1]);
ip3 = int.Parse(ipA[2]);
ip4 = int.Parse(ipA[3]);
}
catch (System.Exception ex)
{
return false;
}
if ((ip1>=0&&ip1<=255)&&(ip2>=0&&ip2<=255)&&(ip3>=0&&ip3<=255)&&(ip4>=0&&ip4<=255))
{
return true;
}
else
{
return false;
}
}
}
}
/// <summary>
/// 验证用户输入端口
/// </summary>
/// <param name="port">端口</param>
/// <returns>结果</returns>
public bool VerifyServerPort(string port)
{
if (port=="")
{
return false;
}
else
{
int port_int;
try
{
port_int = int.Parse(port);
}
catch (System.Exception ex)
{
return false;
}
if (port_int>=1024&&port_int<=65535)
{
return true;
}
else
{
return false;
}
}
}
}
}
分享到:
相关推荐
全书共分13章,主要内容包括网络编程原理、Visual C# 2005的套接字以及多线程的开发、基于各种不同协议的网络编程应用模块,并通过几个典型的实例介绍了Visual C# 2005网络编程的实际应用。 本书注重代码的通用性和...
ListenSocket.h 侦听接口类文件 ChatSocket.h 聊天接口类文件 SettingDlg.h 设置对话框类文件 LocalInfoDlg.h 查看本地信息对话框类文件 freeeim_FreeEIM_企业即时通讯软件源代码2010年8月份最新版 如题。 FTP、...
内容简介《Linux高性能服务器编程》是Linux服务器编程领域的经典著作,由资深Linux软件开发工程师撰写,从网络协议、服务器编程核心要素、原理机制、工具框架等多角度全面阐释了编写高性能Linux服务器应用的方法、...
14.11GUI程序设计实例2——小闹钟542 14.11.1程序界面的实现543 14.11.2时间的刷新代码544 14.11.3JRadioButton的事件响应代码545 14.11.4“确定”按钮的事件响应代码546 14.11.5“取消”按钮的事件响应代码548...
2.2.4 Eclipse的工具条 25 2.2.5 Eclipse辅助视图区 25 2.2.6 Eclipse中Package Explorer 26 2.2.7 Eclipse中的源代码编辑器 26 2.2.8 Eclipse的设置窗口 26 2.2.9 Eclipse中的其他视图 27 2.3 如何使用...
例如,容易实现协议的设计。 Java EJB中有、无状态SessionBean的两个例子 两个例子,无状态SessionBean可会话Bean必须实现SessionBean,获取系统属性,初始化JNDI,取得Home对象的引用,创建EJB对象,计算利息等;...
例如,容易实现协议的设计。 Java EJB中有、无状态SessionBean的两个例子 两个例子,无状态SessionBean可会话Bean必须实现SessionBean,获取系统属性,初始化JNDI,取得Home对象的引用,创建EJB对象,计算利息等;...
第1章 窗体与界面设计 1 1.1 菜单应用 2 实例001 在系统菜单中添加菜单项 2 实例002 带历史信息的菜单 3 实例003 菜单动态合并 4 实例004 像“开始”菜单一样漂亮的菜单 5 实例005 多彩的菜单 6 实例...
2.2.4 Eclipse的工具条 25 2.2.5 Eclipse辅助视图区 25 2.2.6 Eclipse中Package Explorer 26 2.2.7 Eclipse中的源代码编辑器 26 2.2.8 Eclipse的设置窗口 26 2.2.9 Eclipse中的其他视图 27 2.3 如何使用...
非常好的一个实例,把网络连接的UDP/TCP都插入到CList控件中显示出来。 VC++视频捕捉系统 win32下 视频操作。 VC++视频会议系统(完整)有开发文档。使用了系统自带的视频。 Windows核心编程(第五版)随书源代码 ...
非常好的一个实例,把网络连接的UDP/TCP都插入到CList控件中显示出来。 VC++视频捕捉系统 win32下 视频操作。 VC++视频会议系统(完整)有开发文档。使用了系统自带的视频。 Windows核心编程(第五版)随书源代码 ...