Exemplo n.º 1
0
        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);
                  // }
                  



                }            

            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            

            client.DataOn += new DataOn(client_DataOn); //数据包进入事件

            client.Disconnection += new ExceptionDisconnection(client_Disconnection); //数据包断开事件

            if (client.ConnectionTo("127.0.0.1", 9982)) //使用同步连接到服务器,一步就用Begin开头的那个
            {
                while (true)
                {
                    Console.ReadLine();

                    
                    
                    testClass.PPo temp = new testClass.PPo(); 
                    temp.Id = 1;
                    temp.Message = "通过对象通讯";
                    temp.guid = new List<Guid>();

                    for (int i = 0; i < 100; i++)
                    {
                        temp.guid.Add(Guid.NewGuid());
                    }
                    client.SendTo(BufferFormat.FormatFCA(temp));  //讲一个PPO对象发送出去


                   // Console.ReadLine();

                    BufferFormat buffmat = new BufferFormat(1001);
                    buffmat.AddItem(2);
                    buffmat.AddItem("通过组合数据包通讯,GUID is object");
                    buffmat.AddItem(Guid.NewGuid());

                    client.SendTo(buffmat.Finish()); //用组合数据包模拟PPO对象

                   // Console.ReadLine();

                    BufferFormat buffmat2 = new BufferFormat(1002);
                    buffmat2.AddItem(3);
                    buffmat2.AddItem("通过组合数据包通讯 all buff");
                    buffmat2.AddItem(Guid.NewGuid().ToString());
                    client.SendTo(buffmat2.Finish()); //用组合数据包模拟PPO对象 但GUID 是字符串类型

                }

            }
            else
            {
                Console.WriteLine("无法连接服务器");
            }

            Console.ReadLine();
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            #region DES

            BufferFormat fan = new BufferFormat(1000, new FDataExtraHandle((o) =>
            {
                return DES.EncryptDES(o, DESkeys, "hello word");
            }));

            fan.AddItem(true);
            fan.AddItem("abc");
            fan.AddItem(123);

            byte[] data = fan.Finish();


            ReadBytes read = new ReadBytes(data, 4, -1, new RDataExtraHandle((o) =>
                {
                    return DES.DecryptDES(o, DESkeys, "hello word");
                }));

            int lengt;
            int cmd;
            bool var1;
            string var2;
            int var3;

            if (read.IsDataExtraSuccess &&
                read.ReadInt32(out lengt) &&
                lengt == read.Length &&
                read.ReadInt32(out cmd) &&
                read.ReadBoolean(out var1) &&
                read.ReadString(out var2) &&
                read.ReadInt32(out var3))
            {
                Console.WriteLine("This DES-> Length:{0} Cmd:{1} var1:{2} var2:{3} var3:{4}", lengt, cmd, var1, var2, var3);

            }
            #endregion

            //AES测试
            AEStest();

            //数据压缩
            Deflatetest();



            Console.ReadLine();
        }
Exemplo n.º 4
0
        static void Deflatetest()
        {
            BufferFormat fan = new BufferFormat(1000, new FDataExtraHandle((o) =>
            {
                return Deflate.Compress(o);
            }));

            fan.AddItem(true);
            fan.AddItem("abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc");
            fan.AddItem(123);

            byte[] data = fan.Finish();


            ReadBytes read = new ReadBytes(data, 4, -1, new RDataExtraHandle((o) =>
            {
                 return Deflate.Decompress(o);
            }));

            int lengt;
            int cmd;
            bool var1;
            string var2;
            int var3;

            if (read.IsDataExtraSuccess &&
                read.ReadInt32(out lengt) &&
                lengt == read.Length &&
                read.ReadInt32(out cmd) &&
                read.ReadBoolean(out var1) &&
                read.ReadString(out var2) &&
                read.ReadInt32(out var3))
            {
                Console.WriteLine("压缩前长度:{0}", read.Data.Length);
                Console.WriteLine("压缩后长度:{0}", read.Length);
                Console.WriteLine("This Deflate-> Length:{0} Cmd:{1} var1:{2} var2:{3} var3:{4}", lengt, cmd, var1, var2, var3);

            }
        }
Exemplo n.º 5
0
        static void DataOn(byte[] data, UserInfo userinfo)
        {

            //建立一个读取数据包的类 参数是数据包
            //这个类的功能很强大,可以读取数据包的数据,并可以把你发送过来的对象数据,转换对象引用

            ReadBytes read = new ReadBytes(data);

            int lengt; //数据包长度,用于验证数据包的完整性
            int cmd; //数据包命令类型

            //注意这里一定要这样子写,这样子可以保证所有你要度的数据是完整的,如果读不出来 Raed方法会返回FALSE,从而避免了错误的数据导致崩溃
            if (read.ReadInt32(out lengt) && read.Length == lengt && read.ReadInt32(out cmd))
            {  //read.Read系列函数是不会产生异常的

                //根据命令读取数据包
                switch (cmd)
                {
                    case 1000:
                        string msg = read.ReadString();
                        while (true)
                        {
                           
                            BufferFormat buffer = new BufferFormat(1000);
                            buffer.AddItem(msg.ToString());
                            buffer.AddItem(new byte[8096]);
                            byte[] pdata = buffer.Finish();
                            server.Send(userinfo, pdata);
                        }

                    break;
                        

                }

            }


        }
Exemplo n.º 6
0
        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);
            }
        }
Exemplo n.º 7
0
        /// <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;

                    }
                }
            }


        }
Exemplo n.º 8
0
        static void AEStest()
        {
            BufferFormat fan = new BufferFormat(1000, new FDataExtraHandle((o) =>
            {
                return AES.AESEncrypt(o, AESkeys, "hello wordhello wordhello wordhello wordhello wordhello wordhello wordhello wordhello wordhello wordhello wordhello wordhello wordhello word");
            }));

            fan.AddItem(true);
            fan.AddItem("abc");
            fan.AddItem(123);

            byte[] data = fan.Finish();


            ReadBytes read = new ReadBytes(data, 4, -1, new RDataExtraHandle((o) =>
            {
                return AES.AESDecrypt(o, AESkeys, "hello wordhello wordhello wordhello wordhello wordhello wordhello wordhello wordhello wordhello wordhello wordhello wordhello wordhello word");
            }));

            int lengt;
            int cmd;
            bool var1;
            string var2;
            int var3;

            if (read.IsDataExtraSuccess&&
                read.ReadInt32(out lengt) &&
                lengt == read.Length &&
                read.ReadInt32(out cmd) &&
                read.ReadBoolean(out var1) &&
                read.ReadString(out var2) &&
                read.ReadInt32(out var3))
            {
                Console.WriteLine("This AES-> Length:{0} Cmd:{1} var1:{2} var2:{3} var3:{4}", lengt, cmd, var1, var2, var3);

            }
        }