[原创]C# UDP穿越NAT,UDP打洞,UDP Hole Punching源代码(2)
[原创]C# UDP穿越NAT,UDP打洞,UDP Hole Punching源代码(2)
续: [原创]C# UDP穿越NAT,UDP打洞,UDP Hole Punching源代码(1) 本帖包括公共代码及用于转发命令代码 5.公共代码 using System; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System.Net; using System.Collections; namespace vjsdn.net.library { /// <summary> /// 显示跟踪消息的事件委托 /// </summary> public delegate void WriteLogHandle(string msg); /// <summary> /// 刷新在线用户的事件委托 /// </summary> public delegate void UserChangedHandle(UserCollection users); public class Globals { /// <summary> /// 服务器侦听端口 /// </summary> public const int SERVER_PORT = 21134; /// <summary> /// 本地侦听端口 /// </summary> public const int LOCAL_PORT = 19786; } /// <summary> /// User 的摘要说明。 /// </summary> [Serializable] public class User { protected string _userName; protected IPEndPoint _netPoint; protected bool _conntected; public User(string UserName, IPEndPoint NetPoint) { this._userName = UserName; this._netPoint = NetPoint; } public string UserName { get { return _userName; } } public IPEndPoint NetPoint { get { return _netPoint; } set { _netPoint = value; } } public bool IsConnected //打洞标记 { get { return _conntected; } set { _conntected = value; } } public string FullName { get { return this.ToString(); } } public override string ToString() { return _userName + "- [" + _netPoint.Address.ToString() + ":" + _netPoint.Port.ToString() + "] " + (_conntected ? "Y" : "N"); } } /// <summary> /// 在线用户列表 /// </summary> [Serializable] public class UserCollection : CollectionBase { public void Add(User user) { InnerList.Add(user); } public void Remove(User user) { InnerList.Remove(user); } public User this[int index] { get { return (User)InnerList[index]; } } public User Find(string userName) { foreach (User user in this) { if (string.Compare(userName, user.UserName, true) == 0) { return user; } } return null; } } /// <summary> /// 序列化反序列化对象 /// </summary> public class ObjectSerializer { public static byte[] Serialize(object obj) { BinaryFormatter binaryF = new BinaryFormatter(); MemoryStream ms = new MemoryStream(1024 * 10); binaryF.Serialize(ms, obj); ms.Seek(0, SeekOrigin.Begin); byte[] buffer = new byte[(int)ms.Length]; ms.Read(buffer, 0, buffer.Length); ms.Close(); return buffer; } public static object Deserialize(byte[] buffer) { BinaryFormatter binaryF = new BinaryFormatter(); MemoryStream ms = new MemoryStream(buffer, 0, buffer.Length, false); object obj = binaryF.Deserialize(ms); ms.Close(); return obj; } } } 来源:C/S框架网(www.csframework.com) QQ:1980854898 6.消息类(转发命令) 6.1 消息基类.所有命令的基类 /// <summary> /// 消息基类,抽象类 /// </summary> [Serializable] public abstract class MessageBase { //消息基类 } 6.2 Client to Server命令 #region 客户端发送到服务器的消息 /// <summary> /// 客户端发送到服务器的消息基类 /// </summary> [Serializable] public abstract class C2S_MessageBase : MessageBase { private string _fromUserName; protected C2S_MessageBase(string fromUserName) { _fromUserName = fromUserName; } public string FromUserName { get { return _fromUserName; } } } /// <summary> /// 用户登录消息 /// </summary> [Serializable] public class C2S_LoginMessage : C2S_MessageBase { private string _password; public C2S_LoginMessage(string userName, string password) : base(userName) { this._password = password; } public string Password { get { return _password; } } } /// <summary> /// 用户登出消息 /// </summary> [Serializable] public class C2S_LogoutMessage : C2S_MessageBase { public C2S_LogoutMessage(string userName) : base(userName) { } } /// <summary> /// 请求用户列表消息 /// </summary> [Serializable] public class C2S_GetUsersMessage : C2S_MessageBase { public C2S_GetUsersMessage(string userName) : base(userName) { } } /// <summary> /// 请求Purch Hole消息 /// </summary> [Serializable] public class C2S_HolePunchingRequestMessage : C2S_MessageBase { protected string toUserName; public C2S_HolePunchingRequestMessage(string fromUserName, string toUserName) : base(fromUserName) { this.toUserName = toUserName; } public string ToUserName { get { return this.toUserName; } } } #endregion 来源:C/S框架网(www.csframework.com) QQ:1980854898 6.3 Server to Client命令 #region 服务器发送到客户端消息 /// <summary> /// 服务器发送到客户端消息基类 /// </summary> [Serializable] public abstract class S2C_MessageBase : MessageBase { } /// <summary> /// 请求用户列表应答消息 /// </summary> [Serializable] public class S2C_UserListMessage : S2C_MessageBase { private UserCollection userList; public S2C_UserListMessage(UserCollection users) { this.userList = users; } public UserCollection UserList { get { return userList; } } } /// <summary> /// 转发请求Purch Hole消息 /// </summary> [Serializable] public class S2C_HolePunchingMessage : S2C_MessageBase { protected System.Net.IPEndPoint _remotePoint; public S2C_HolePunchingMessage(System.Net.IPEndPoint point) { this._remotePoint = point; } public System.Net.IPEndPoint RemotePoint { get { return _remotePoint; } } } /// <summary> /// 服务器通知所有在线用户, /// </summary> [Serializable] public class S2C_UserAction : S2C_MessageBase { protected User _User; protected UserAction _Action; public S2C_UserAction(User user, UserAction action) { _User = user; _Action = action; } public User User { get { return _User; } } public UserAction Action { get { return _Action; } } } #endregion 来源:C/S框架网(www.csframework.com) QQ:1980854898 6.4 Client to Client(Peer to Peer P2P)命令 #region 点对点消息 /// <summary> /// 点对点消息基类 /// </summary> [Serializable] public abstract class P2P_MessageBase : MessageBase { // } /// <summary> /// 聊天消息 /// </summary> [Serializable] public class P2P_TalkMessage : P2P_MessageBase { private string message; public P2P_TalkMessage(string msg) { message = msg; } public string Message { get { return message; } } } /// <summary> /// UDP打洞测试消息 /// </summary> [Serializable] public class P2P_HolePunchingTestMessage : P2P_MessageBase { private string _UserName; public P2P_HolePunchingTestMessage(string userName) { _UserName = userName; } public string UserName { get { return _UserName; } } } /// <summary> /// 收到消息的回复确认 /// 如A与B想建立通话通道,些命令由B发出确认打洞成功 /// </summary> [Serializable] public class P2P_HolePunchingResponse : P2P_MessageBase { private string _UserName; public P2P_HolePunchingResponse(string userName) { _UserName = userName; } public string UserName { get { return _UserName; } } } #endregion 来源:C/S框架网(www.csframework.com) QQ:1980854898 6.5 定义用户的动作 /// <summary> /// 用户动作 /// </summary> public enum UserAction { Login, Logout .... more action } 扫一扫加作者微信
参考文档:
C#源代码高亮着色类(C/S框架网开源) C#网络版中国象棋游戏源代码(VS2005) C++实现的NAT打洞技术(C++ NAT Hole Puching) [原创]C# UDP穿越NAT,UDP打洞,UDP Hole Punching源代码(1) UDP打洞(UDP Hole Punching)原理 C#贪吃蛇小游戏的源代码 C# ImageListView控件下载(源代码) [原创]C#键盘勾子(Hook),屏蔽键盘活动.(源代码下载) C#实现UDP穿透NAT(UDP打洞)完整版(原) 原创:CodeHighlighter源代码格式化,代码缩进,关键词高亮着色(C#源码) CSFramework C#代码生成器生成窗体界面UI,BLL,DAL,Model,WCF接口层源代码 C#源代码安全缺陷与提高源代码质量解决方案 C#源代码安全缺陷与提高源代码质量解决方案-WCF服务配置安全 C#源码-微信营销系统(第三方微信平台)C#完整源代码-源码下载 C#源码-[博客空间]X3BLOG(ASP.NET开源多用户博客系统) 1.1.0 beta1 源代码_x3blog-源码下载
其它资料:
什么是C/S结构? | C/S框架核心组成部分 | C/S框架-WebService部署图 | C/S框架-权限管理 | C/S结构系统框架 - 5.1旗舰版介绍 | C/S结构系统框架 - 功能介绍 | C/S结构系统框架 - 产品列表 | C/S结构系统框架 - 应用展示(图) | 三层体系架构详解 | C/S架构轻量级快速开发框架 | C/S框架网客户案例 | WebApi快速开发框架 | C/S框架代码生成器 | 用户授权注册软件系统 | 版本自动升级软件 | 数据库底层应用框架 | CSFramework.CMS内容管理系统 | |