.Net网络通讯编程[利用Socket实现字串、文件、序列化对象传输]--类设计1
本案例使用.Net Socket的Tcp、Udp实现字串、文件、各种序列化对象的网络传输,同时封装了Tcp的粘包、半包处理细节,定义了网络封包格式,在发送端和接收端无需考虑内部传输细节。以下是类设计:
序列化相关类类图:
序列化相关类代码:
示范代码using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TcpLabCommon { /// <summary> /// 序列化工具 /// </summary> public interface ISerializeHelper { /// <summary> /// 序列化 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="stream"></param> /// <param name="item"></param> void Serialize<T>(System.IO.Stream stream, T item); /// <summary> /// 反序列化 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="stream"></param> /// <returns></returns> T DeSerialize<T>(System.IO.Stream stream); } }
示范代码using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Serialization; using System.Xml; using System.Runtime.Serialization.Formatters.Binary; using System.Runtime.Serialization.Formatters.Soap; using System.Runtime.Serialization.Json; namespace TcpLabCommon { /// <summary> /// Xml序列化 /// </summary> public class XmlSerializeHelper:ISerializeHelper { public void Serialize<T>(System.IO.Stream stream, T item) { XmlSerializer serializer = new XmlSerializer(item.GetType()); stream.Seek(0, System.IO.SeekOrigin.Begin); serializer.Serialize(stream, item); } public T DeSerialize<T>(System.IO.Stream stream) { XmlSerializer serializer = new XmlSerializer(typeof(T)); stream.Seek(0, System.IO.SeekOrigin.Begin); return (T)serializer.Deserialize(stream); } } /// <summary> /// 二进制序列化 /// </summary> public class BinarySerializeHelper : ISerializeHelper { public void Serialize<T>(System.IO.Stream stream, T item) { BinaryFormatter formatter = new BinaryFormatter(); stream.Seek(0, System.IO.SeekOrigin.Begin); formatter.Serialize(stream, item); } public T DeSerialize<T>(System.IO.Stream stream) { BinaryFormatter formatter = new BinaryFormatter(); stream.Seek(0, System.IO.SeekOrigin.Begin); return (T)formatter.Deserialize(stream); } } /// <summary> /// Soap序列化 /// </summary> public class SoapSerializeHelper : ISerializeHelper { public void Serialize<T>(System.IO.Stream stream, T item) { SoapFormatter formatter = new SoapFormatter(); stream.Seek(0, System.IO.SeekOrigin.Begin); formatter.Serialize(stream, item); } public T DeSerialize<T>(System.IO.Stream stream) { SoapFormatter formatter = new SoapFormatter(); return (T)formatter.Deserialize(stream); } } /// <summary> /// Json序列化 /// </summary> public class JsonSerializeHelper : ISerializeHelper { public void Serialize<T>(System.IO.Stream stream, T item) { DataContractJsonSerializer formatter = new DataContractJsonSerializer(typeof(T)); stream.Seek(0, System.IO.SeekOrigin.Begin); formatter.WriteObject(stream, item); } public T DeSerialize<T>(System.IO.Stream stream) { DataContractJsonSerializer formatter = new DataContractJsonSerializer(typeof(T)); stream.Seek(0, System.IO.SeekOrigin.Begin); return (T)formatter.ReadObject(stream); } } }
网络封包相关类类图:
网络封包相关类代码:
示范代码using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TcpLabCommon { /// <summary> /// 包类型 /// </summary> public enum PacketType { /// <summary> /// 简单字符串传输 /// </summary> STRING=0, /// <summary> /// 二进制[文件传输] /// </summary> BINARY=1, /// <summary> /// 复杂对象传输[使用序列化和反序列化] /// </summary> COMPLEX=2 } /// <summary> /// 包头 /// </summary> public class NetPacketHead { /// <summary> /// 包头大小 /// </summary> public const Int32 HEAD_SIZE = 4 * 3; private Int32 _version = 1; /// <summary> /// 版本 /// </summary> public Int32 Version { get { return _version; } set { _version = value; } } private PacketType _pType = PacketType.STRING; /// <summary> /// 包类型[决定如何解包] /// </summary> public PacketType PType { get { return _pType; } set { _pType = value; } } private Int32 _len = 0; /// <summary> /// 包体长度[决定后面data数据的长度] /// </summary> public Int32 Len { get { return _len; } set { _len = value; } } } /// <summary> /// 网络数据包【Udp建议大小不超过548个字节,Internet标准MTU为576,减去IP数据报头(20字节)和UDP数据包头(8字节)】 /// </summary> public class NetPacket { NetPacketHead _packetHead; /// <summary> /// 包头 /// </summary> public NetPacketHead PacketHead { get { return _packetHead; } set { _packetHead = value; } } private object _data=null; /// <summary> /// 包体,根据封包类型决定包体数据类型 /// STRING:包体为String /// BINARY:包体为Byte[] /// COMPLEX:包体为可序列化的对象 /// </summary> public object Data { get { return _data; } set { _data = value; } } } /// <summary> /// 传输的文件 /// </summary> public class NetFile { private string _fileName = string.Empty; public string FileName { get { return _fileName; } set { _fileName = value; } } private Byte[] _content; public Byte[] Content { get { if (_content==null) _content = new Byte[]{}; return _content; } set { _content = value; } } } }
提供网络封包传输服务的核心类:
posted on 2012-03-28 09:00 jiangtongcn 阅读(966) 评论(0) 编辑 收藏 举报