static void Main(string[] args) { SocketClient client = new SocketClient(); if (client.Connect("127.0.0.1", 5566)) { client.BinaryInput += client_BinaryInput; client.StartRead(); while (true) { Console.ReadLine(); //for (int i = 0; i < 100000; i++) //{ BufferFormat buffer = new BufferFormat(1000); buffer.AddItem(1.ToString()); buffer.AddItem(new byte[64]); byte[] data = buffer.Finish(); client.Send(data); System.Threading.Thread.Sleep(1); // } } } }
internal ConClient(string host, int port) { this.Host = host; this.Port = port; Sock = new SocketClient(); Sock.ConnInput += new ZYSocket.ClientB.ConnectionHandler(Connection); Sock.BinaryInput += new ZYSocket.ClientB.ClientBinaryInputHandler(DataIn); Sock.MessageInput += new ZYSocket.ClientB.ClientMessageInputHandler(ExpInput); }
/// <summary> /// 数据包处理 /// </summary> /// <param name="data"></param> private void BufferIn(byte[] data) { ReadBytesV2 read = new ReadBytesV2(data); int length; if (read.ReadInt32(out length) && length == read.Length) { int cmd; if (read.ReadInt32(out cmd)) { PCMD pcmd = (PCMD)cmd; switch (pcmd) { case PCMD.SET: //准备就绪 BufferFormatV2 tmp = new BufferFormatV2((int)PCMD.GETALLMASK); Mainclient.Send(tmp.Finish()); break; case PCMD.ALLUSER: //获取全部用户列表 try { int count; if (read.ReadInt32(out count)) { for (int i = 0; i < count; i++) { string usermask; if (read.ReadString(out usermask)) { UserMaskList.Enqueue(usermask); } } RunQueueList(); } } catch (ArgumentOutOfRangeException) { } break; case PCMD.NOWCONN: //立刻连接到指定IP端口 string host; string key; if (read.ReadString(out host) && read.ReadString(out key)) { host = host + ":" + key; SocketClient client = new SocketClient(); Tp: IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, BindPort++); //绑定端口 if (BindPort >= 60000) BindPort = 1000; try { client.Sock.Bind(endpoint); //如果无法绑定那么重新选个端口 } catch { goto Tp; } if (client.Connect(this.Host, RegIpPort)) //连接到注册端口 { BufferFormat tmpX = new BufferFormat(100); tmpX.AddItem(Key); tmpX.AddItem(BindPort); client.Send(tmpX.Finish()); System.Threading.Thread.Sleep(50); BufferFormatV2 tmpX2 = new BufferFormatV2((int)PCMD.LEFTCONN); tmpX2.AddItem(key); Mainclient.Send(tmpX2.Finish()); client.Close(); System.Threading.Thread.Sleep(50); System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(RunConnToMe), host); } } break; case PCMD.LEFTCONN: string host2; string key2; if (read.ReadString(out host2) && read.ReadString(out key2)) { host2 = host2 + ":" + key2; System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(RunConnToMe), host2); } break; case PCMD.GETALLUSER: { int count; if (read.ReadInt32(out count)) { AllUser = new List<string>(); for (int i = 0; i < count; i++) { string var; if (read.ReadString(out var)) { AllUser.Add(var); } else break; } if (GetAllUserList != null) GetAllUserList(AllUser); } } break; case PCMD.ProxyData: { string keys; byte[] buff; if (read.ReadString(out keys) && read.ReadByteArray(out buff)) { if (ProxyList.ContainsKey(keys)) { client_DataOutPut(keys, ProxyList[keys], buff); } else { ConClient client = new ConClient(keys); if (ProxyList.TryAdd(client.Key, client)) { client_DataOutPut(keys, client, buff); } } } } break; } } } }
void RunQueueList() { try { if (UserMaskList.Count > 0) //如果列队数量大于0 { Re: string userkey; if (UserMaskList.TryDequeue(out userkey)) //挤出一个用户ID { if (userkey == Key) goto Re; SocketClient client = new SocketClient(); //建立一个 SOCKET客户端 Pt: IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, BindPort++); //绑定当前端口 if (BindPort >= 60000) BindPort = 1000; try { client.Sock.Bind(endpoint); //如果无法绑定此端口 那么换个端口 } catch { goto Pt; } if (client.Connect(Host, RegIpPort)) //连接注册服务器端口 { BufferFormat tmp = new BufferFormat(100); tmp.AddItem(Key); tmp.AddItem(BindPort); client.Send(tmp.Finish()); System.Threading.Thread.Sleep(50); //等待 50毫秒 BufferFormatV2 tmp2 = new BufferFormatV2((int)PCMD.CONN); tmp2.AddItem(userkey); Mainclient.Send(tmp2.Finish()); client.Close();//关闭客户端 } } } } catch (Exception e) { LogOut.LogIn(e.ToString(),ActionType.Error); } }
public void ConToServer() { Mainclient = new SocketClient(); Mainclient.BinaryInput += new ClientBinaryInputHandler(DataIn); Mainclient.MessageInput += new ClientMessageInputHandler(Exption); if (Mainclient.Connect(Host, Port)) { Mainclient.StartRead(); LogOut.LogIn("成功连接服务器", ActionType.ServerConn); string localip = ((IPEndPoint)(Mainclient.Sock.LocalEndPoint)).Address.ToString(); //获取本地局域网IP地址 BufferFormatV2 tmp = new BufferFormatV2((int)PCMD.REGION); tmp.AddItem(Key); tmp.AddItem(localip); tmp.AddItem(Mac); Mainclient.Send(tmp.Finish()); } else { LogOut.LogIn("不能连接到服务器", ActionType.ServerNotConn); if (ServerDiscon != null) ServerDiscon("不能连接到服务器"); } }