Udp打洞简单描述
2009-03-18 08:21:12 来源:WEB开发网类库,传递序列化的消息类。
lib
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace lib
{
[Serializable]
public class GetUserList
{
}
public class SerializeHelper
{
/**////*****************************************
/// <summary>
/// 序列化一个对象
/// </summary>
/// <param name="o">将要序列化的对象</param>
/// <returns>返回byte[]</returns>
///*****************************************
public static byte[] Serialize(object o)
{
if (o == null) return null;
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
formatter.Serialize(ms, o);
ms.Position = 0;
byte[] b = new byte[ms.Length];
ms.Read(b, 0, b.Length);
ms.Close();
return b;
}
/**////*****************************************
/// <summary>
/// 反序列化
/// </summary>
/// <param name="b">返回一个对象</param>
///*****************************************
public static object Deserialize(byte[] b)
{
if (b.Length == 0) return null;
try
{
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
ms.Write(b, 0, b.Length);
ms.Position = 0;
object n = (object)bf.Deserialize(ms);
ms.Close();
return n;
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.ToString());
return null;
}
}
}
[Serializable]
public class UserInfo
{
/**//// <summary>
/// 用户名
/// </summary>
public string UserName
{
set;
get;
}
/**//// <summary>
/// 地址(IP+Port)
/// </summary>
public IPEndPoint Address
{
set;
get;
}
}
[Serializable]
public class UserList
{
/**//// <summary>
/// 用户列表
/// </summary>
public List<UserInfo> userInfoList
{
set;
get;
}
}
}
更多精彩
赞助商链接