国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

服務(wù)器異步客戶端

這篇具有很好參考價值的文章主要介紹了服務(wù)器異步客戶端。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

服務(wù)器

//泛型單例

internal class Singleton<T>where T:class,new() { ? ? private static T ins; ? ? public static T Ins ? ? { ? ? ? ? get ? ? ? ? { ? ? ? ? ? ? if(ins == null) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ins = new T(); ? ? ? ? ? ? } ? ? ? ? ? ? return ins; ? ? ? ? } ? ? } }

//消息中心

internal class MessageManager<T>:Singleton<MessageManager<T>>
{
? ? Dictionary<int, Action<T>> MsgDic = new Dictionary<int, Action<T>>();
? ? public void OnAddListen(int id,Action<T> action)
? ? {
? ? ? ? if(MsgDic.ContainsKey(id))
? ? ? ? {
? ? ? ? ? ? MsgDic[id] += action;
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? MsgDic.Add(id, action);
? ? ? ? }
? ? }
? ? public void OnBroadCast(int id,T t)
? ? {
? ? ? ? if(MsgDic.ContainsKey(id))
? ? ? ? {
? ? ? ? ? ? MsgDic[id](t);
? ? ? ? }
? ? }
}

internal class Client
{
? ? public Socket socket;
? ? public byte[] data=new byte[1024];
}

internal class MsgData
{
? ? public Client cli;
? ? public byte[] data;
}

//服務(wù)器框架

internal class NetManager:Singleton<NetManager>
{
? ? Socket socket;
? ? //public List<Client> allcli=new List<Client>();
? ? public void InitServer()
? ? {
? ? ? ? socket=new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
? ? ? ? socket.Bind(new IPEndPoint(IPAddress.Any,2201));
? ? ? ? socket.Listen(10);
? ? ? ? Console.WriteLine("2201 服務(wù)器開啟");
? ? ? ? socket.BeginAccept(OnAccept, null);
? ? }

? ? private void OnAccept(IAsyncResult ar)
? ? {
? ? ? ? try
? ? ? ? {
? ? ? ? ? ? Socket socket_cli=socket.EndAccept(ar);
? ? ? ? ? ? IPEndPoint iPEnd=socket_cli.RemoteEndPoint as IPEndPoint;
? ? ? ? ? ? Console.WriteLine("IP"+iPEnd.Address+"port"+iPEnd.Port+"連接上了");
? ? ? ? ? ? socket.BeginAccept(OnAccept, null);
? ? ? ? ? ? Client client=new Client();
? ? ? ? ? ? client.socket= socket_cli;
? ? ? ? ? ? //allcli.Add(client);
? ? ? ? ? ? client.socket.BeginReceive(client.data, 0, client.data.Length, SocketFlags.None, Receive, client);

? ? ? ? }
? ? ? ? catch (Exception ex)
? ? ? ? {
? ? ? ? ? ? Console.WriteLine(ex);
? ? ? ? }
? ? }

? ? private void Receive(IAsyncResult ar)
? ? {
? ? ? ? try
? ? ? ? {
? ? ? ? ? ? Client client=ar.AsyncState as Client;
? ? ? ? ? ? int len=client.socket.EndReceive(ar);
? ? ? ? ? ? if(len>0)
? ? ? ? ? ? {
? ? ? ? ? ? ?
? ? ? ? ? ? ? ? byte[] rdata=new byte[len];
? ? ? ? ? ? ? ? Buffer.BlockCopy(client.data,0,rdata,0,len);
? ? ? ? ? ? ? ? while(rdata.Length>4)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? int bodylen=BitConverter.ToInt32(rdata, 0);
? ? ? ? ? ? ? ? ? ? byte[] bodydata=new byte[bodylen];
? ? ? ? ? ? ? ? ? ? Buffer.BlockCopy(rdata, 4, bodydata, 0, bodylen);

? ? ? ? ? ? ? ? ? ? int msgid=BitConverter.ToInt32(bodydata, 0);
? ? ? ? ? ? ? ? ? ? byte[] info=new byte[bodylen-4];
? ? ? ? ? ? ? ? ? ? Buffer.BlockCopy(bodydata,4,info, 0, info.Length);

? ? ? ? ? ? ? ? ? ? MsgData msgData = new MsgData();
? ? ? ? ? ? ? ? ? ? msgData.cli = client;
? ? ? ? ? ? ? ? ? ? msgData.data = info;
? ? ? ? ? ? ? ? ? ? MessageManager<MsgData>.Ins.OnBroadCast(msgid, msgData);

? ? ? ? ? ? ? ? ? ? int sylen = rdata.Length - 4 - bodylen;
? ? ? ? ? ? ? ? ? ? byte[] sydata=new byte[sylen];
? ? ? ? ? ? ? ? ? ? Buffer.BlockCopy(rdata,4+bodylen, sydata, 0, sylen);
? ? ? ? ? ? ? ? ? ? rdata = sydata;
? ? ? ? ? ? ? ? }

? ? ? ? ? ? }
? ? ? ? ? ? client.socket.BeginReceive(client.data, 0, client.data.Length, SocketFlags.None, Receive, client);
? ? ? ? }
? ? ? ? catch (Exception ex)
? ? ? ? {
? ? ? ? ? ? Console.WriteLine(ex);
? ? ? ? }
? ? }
? ? public void OnSendServer(int id, byte[] data,Client client)
? ? {
? ? ? ? int head = 4 + data.Length;
? ? ? ? byte[] enddata=new byte[0];
? ? ? ? enddata = enddata.Concat(BitConverter.GetBytes(head)).Concat(BitConverter.GetBytes(id)).Concat(data).ToArray();
? ? ? ? client.socket.BeginSend(enddata, 0, enddata.Length, SocketFlags.None, Send, client);
? ? }

? ? private void Send(IAsyncResult ar)
? ? {
? ? ? ? Client client=ar.AsyncState as Client;
? ? ? ? int len = client.socket.EndSend(ar);
? ? ? ? Console.WriteLine("發(fā)給客戶端消息"+len);
? ? }
}

客戶端

public class NetManager : Singleton<NetManager>
{
? ? Socket socket;
? ? byte[] data=new byte[1024];

? ? Queue<byte[]> queue=new Queue<byte[]>();
? ? public void InitClient()
? ? {
? ? ? ? socket=new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
? ? ? ? socket.BeginConnect("127.0.0.1", 2201, OnConnect, null);
? ? }

? ? private void OnConnect(IAsyncResult ar)
? ? {
? ? ? ? try
? ? ? ? {
? ? ? ? ? ? socket.EndConnect(ar);
? ? ? ? ? ? Debug.Log("連接成功");
? ? ? ? ? ? socket.BeginReceive(data, 0, data.Length, SocketFlags.None, OnReceive, null);
? ? ? ? }
? ? ? ? catch (Exception ex)
? ? ? ? {
? ? ? ? ? ? Debug.Log(ex);
? ? ? ? }
? ? }

? ? private void OnReceive(IAsyncResult ar)
? ? {
? ? ? ? try
? ? ? ? {
? ? ? ? ? ? int len =socket.EndReceive(ar);
? ? ? ? ? ? if (len > 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? byte[] rdata = new byte[len];
? ? ? ? ? ? ? ? Buffer.BlockCopy(data, 0, rdata, 0, len);
? ? ? ? ? ? ? ? while (rdata.Length > 4)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? int bodylen = BitConverter.ToInt32(rdata, 0);
? ? ? ? ? ? ? ? ? ? byte[] bodydata = new byte[bodylen];
? ? ? ? ? ? ? ? ? ? Buffer.BlockCopy(rdata, 4, bodydata, 0, bodylen);

? ? ? ? ? ? ? ? ? ? queue.Enqueue(bodydata);

? ? ? ? ? ? ? ? ? ? int sylen = rdata.Length - 4 - bodylen;
? ? ? ? ? ? ? ? ? ? byte[] sydata = new byte[sylen];
? ? ? ? ? ? ? ? ? ? Buffer.BlockCopy(rdata, 4 + bodylen, sydata, 0, sylen);
? ? ? ? ? ? ? ? ? ? rdata = sydata;
? ? ? ? ? ? ? ? }

? ? ? ? ? ? }
? ? ? ? ? ? socket.BeginReceive(data, 0, data.Length, SocketFlags.None, OnReceive, null);
? ? ? ? }
? ? ? ? catch (Exception ex)
? ? ? ? {
? ? ? ? ? ? Debug.Log(ex);
? ? ? ? }
? ? }
? ? public void NetUpdata()
? ? {
? ? ? ? if(queue.Count > 0)
? ? ? ? {
? ? ? ? ? ? byte[] bodydata=queue.Dequeue();
? ? ? ? ? ? int msgid=BitConverter.ToInt32(bodydata, 0);
? ? ? ? ? ? byte[] info=new byte[bodydata.Length-4];
? ? ? ? ? ? Buffer.BlockCopy(bodydata, 4, info, 0, info.Length);
? ? ? ? ? ? MessageManager<byte[]>.Ins.OnBroadCast(msgid, info);

? ? ? ? }
? ? }

? ? public void OnSendClient(int id, byte[] data)
? ? {
? ? ? ? int head = 4 + data.Length;
? ? ? ? byte[] enddata=new byte[0];
? ? ? ? enddata = enddata.Concat(BitConverter.GetBytes(head)).Concat(BitConverter.GetBytes(id)).Concat(data).ToArray();
? ? ? ? socket.BeginSend(enddata, 0, enddata.Length, SocketFlags.None, OnSend, null);
? ? }

? ? private void OnSend(IAsyncResult ar)
? ? {
? ? ? ? int len = socket.EndSend(ar);
? ? ? ? Debug.Log("發(fā)給服務(wù)器消息"+len);
? ? }
}文章來源地址http://www.zghlxwxcb.cn/news/detail-845522.html

到了這里,關(guān)于服務(wù)器異步客戶端的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包