1、创建一个消息体的类型
- public enum MessageType : byte
- {
- Unknown = 0,
- Chat = 100,
- Query = 10,
- Identity = 11,
- Online = 20,
- Offline = 0x25
- }
2、 创建一个消息体(结构包)
- public abstract class MessagePackageBase
- {
- private MessageType a;
-
- protected MessagePackageBase(MessageType type)
- {
-
- this.Type = type;
- }
-
- internal static MessageType Aa(byte[] A_0)
- {
- if ((A_0 != null) && (A_0.Length != 0))
- {
- return (MessageType)A_0[0];
- }
- return MessageType.Unknown;
- }
-
- public virtual byte[] GetPackageData()
- {
- return new byte[] { (Byte)Type };
- }
-
- public MessageType Type
- {
- get
- {
- return this.a;
- }
- set
- {
- this.a = value;
- }
- }
- }
3、创建一个处理的包
- public class ChatMessagePackage : MessagePackageBase
- {
- public static readonly Encoding Charset;
- private string message;
- private DateTime time;
-
- static ChatMessagePackage()
- {
-
- Charset = Encoding.Default;
- }
-
- public ChatMessagePackage(DateTime time, string message) : base(MessageType.Chat)
- {
-
- this.Time = time;
- this.Message = message;
- }
-
- public static ChatMessagePackage FromPackage(byte[] data)
- {
- try
- {
- byte[] destinationArray = new byte[8];
- Array.Copy(data, 1, destinationArray, 0, 8);
- DateTime time = DateTime.FromBinary(BitConverter.ToInt64(destinationArray, 0));
- return new ChatMessagePackage(time, Charset.GetString(data, destinationArray.Length + 1, (data.Length - destinationArray.Length) - 1));
- }
- catch
- {
- return null;
- }
- }
-
- public override byte[] GetPackageData()
- {
- List<byte> list = new List<byte>(100);
- list.AddRange(base.GetPackageData());
- list.AddRange(BitConverter.GetBytes(this.Time.ToBinary()));
- list.AddRange(Charset.GetBytes(this.Message));
- return list.ToArray();
- }
-
- public string Message
- {
- get
- {
- return message;
- }
- set
- {
- message = value;
- }
- }
-
- public DateTime Time
- {
- get
- {
- return time;
- }
- set
- {
- time = value;
- }
- }
4、创建一个包的事件
- public class ChatServerDataReceiveEventArgs : EventArgs
- {
- private IPEndPoint iP;
- private byte[] bytes;
- private MessageType type;
-
- public ChatServerDataReceiveEventArgs(IPEndPoint remoteEP, byte[] data)
- {
-
- this.RemoteEP = remoteEP;
- this.Data = data;
- this.MessageType = MessagePackageBase.Aa(data);
- }
-
- public IPEndPoint RemoteEP
- {
- get
- {
- return iP;
- }
- set
- {
- iP = value;
- }
- }
-
- public byte[] Data
- {
- get
- {
- return bytes;
- }
- set
- {
- bytes = value;
- }
- }
-
- public MessageType MessageType
- {
- get
- {
- return type;
- }
- set
- {
- type = value;
- }
- }
- }
5、身份包
- public class IdentityMessagePackage : MessagePackageBase
- {
- public IdentityMessagePackage() : base(MessageType.Identity)
- {
-
- }
- }
6、离线包
- public class OfflineMessagePackage : MessagePackageBase
- {
- public OfflineMessagePackage() : base(MessageType.Offline)
- {
-
- }
- }
7、在线包
- public class OnlineMessagePackage : MessagePackageBase
- {
- public OnlineMessagePackage() : base(MessageType.Online)
- {
-
- }
- }
8、查询包
- public class QueryMessagePackage : MessagePackageBase
- {
- public QueryMessagePackage() : base(MessageType.Query)
- {
-
- }
- }
9、服务端
- public class ChatServer : IDisposable
- {
- public static int Port;
- public static readonly IPAddress BroadcastAddress;
- private UdpClient udp;
- private bool Run;
- private AsyncCallback back;
- public event EventHandler
DataReceive; -
- static ChatServer()
- {
- Port = 2368;
- BroadcastAddress = IPAddress.Parse("255.255.255.255");
- }
- public ChatServer(int port)
- {
-
- Port = port;
- Run = false;
- udp = new UdpClient(Port, AddressFamily.InterNetwork);
- back = new AsyncCallback(Result);
- }
-
- private void Result(IAsyncResult A_0)
- {
- if (Run)
- {
- UdpClient sender = null;
- try
- {
- sender = (UdpClient)A_0.AsyncState;
- IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
- byte[] data = sender.EndReceive(A_0, ref remoteEP);
- EventHandler
d = this.DataReceive; - if (d != null)
- {
- d(sender, new ChatServerDataReceiveEventArgs(remoteEP, data));
- }
- }
- catch (ObjectDisposedException)
- {
- sender = null;
- }
- catch
- {
- }
- finally
- {
- if (Run && (sender != null))
- {
- sender.BeginReceive(back, sender);
- }
- }
- }
- }
-
- public void Dispose()
- {
- this.Stop();
- udp = null;
- }
-
- public void Start()
- {
- if (!Run)
- {
- udp.BeginReceive(back, udp);
- Run = true;
- }
- }
-
- public void Stop()
- {
- if (Run)
- {
- Run = false;
- udp.Close();
- }
- }
- public void SendMeesage(byte[] datas ,IPEndPoint iPEnd)
- {
- udp.Send(datas, datas.Length, iPEnd);
- }
- public void SendPackMesasage(string message, IPEndPoint iPEndPoint)
- {
- PackMessage(new ChatMessagePackage(DateTime.Now, message), iPEndPoint);
- }
- private void PackMessage(MessagePackageBase A_0, IPEndPoint point)
- {
- byte[] packageData = A_0.GetPackageData();
- udp.Send(packageData, packageData.Length, point);
- }
-
- }
10 、客户端:
- public class ChatClient : IDisposable
- {
- private UdpClient udp;
- public IPEndPoint ServerAddress;
- private AsyncCallback back;
- static ChatClient()
- {
-
-
- }
-
- public ChatClient()
- {
- ServerAddress = new IPEndPoint(ChatServer.BroadcastAddress, ChatServer.Port);
- udp = new UdpClient();
-
- }
-
-
- private void PackMessage(MessagePackageBase A_0)
- {
- byte[] packageData = A_0.GetPackageData();
- udp.Send(packageData, packageData.Length, ServerAddress);
- }
-
- public void Dispose()
- {
- udp.Close();
- udp = null;
- }
-
- public void Offline()
- {
- PackMessage(new OfflineMessagePackage());
- }
-
- public void Online()
- {
- PackMessage(new OnlineMessagePackage());
- }
-
- public void Query()
- {
- PackMessage(new QueryMessagePackage());
- }
-
- public void SendIdentity()
- {
- PackMessage(new IdentityMessagePackage());
- }
-
- public void SendPackMessage(string message)
- {
- PackMessage(new ChatMessagePackage(DateTime.Now, message));
- }
- public void SendMessage(byte[] datas)
- {
- udp.Send(datas, datas.Length, ServerAddress);
- }
- }
11、调用
- private ChatServer ChatServer;
-
-
- void StartInit()
- {
- ChatServer = new ChatServer(ContentConfig.Load().NetWorkControl.Port);
- ChatServer.DataReceive += new EventHandler
(Chatserver_DataReceive); - ChatServer.Start();
- }
- //JIESHOU
- private void Chatserver_DataReceive(object sender, ChatServerDataReceiveEventArgs e)
- {
-
- if (e.Data != null)
- {
- byte[] data = e.Data;
- //TODO......
-
-
- }
- }