Esempio n. 1
0
        static void Main(string[] args)
        {
            client              = new SocketClient();
            client.BinaryInput  = new ClientBinaryInputHandler(ClientBinaryInputHandler);   //设置数据包处理回调方法
            client.MessageInput = new ClientMessageInputHandler(ClientMessageInputHandler); //断开处理
            if (client.Connect(Config.Default.IP, Config.Default.Port))                     //连接到服务器
            {
                client.StartRead();                                                         //开始监听读取
                while (true)
                {
                    string mess = Console.ReadLine();
                    if (mess.Length == 0)
                    {
                        continue;
                    }

                    Console.WriteLine("Send:" + mess);
                    byte[] messdata = Buffers.GetSocketBytes(mess);
                    messdata = Buffers.MergeBytes(Buffers.GetSocketBytes(messdata.Length), messdata);
                    client.SendData(messdata);
                }
            }
            else
            {
                Console.WriteLine("无法连接服务器");
            }
        }
Esempio n. 2
0
        public int GetDataLen(byte[] data, int index)
        {
            var len = BitConverter.ToInt32(data, index);
            var bys = BitConverter.GetBytes(len);

            bys = Buffers.ReverseBytes(bys);
            len = BitConverter.ToInt32(bys, 0); //获取到包头多少个字节
            return(len);
        }
Esempio n. 3
0
        void DataMM(byte[] data)
        {
            var index = 0;

            if (iLen > 0)
            {
                if (iLen <= data.Length)
                {
                    var tmp = new byte[iLen];
                    Array.Copy(data, 0, tmp, 0, tmp.Length);
                    buf = Buffers.MergeBytes(buf, tmp);
                    this.BinaryInput(buf);
                    buf   = null;
                    index = iLen;
                }
                else
                {
                    buf   = Buffers.MergeBytes(buf, data);
                    iLen -= data.Length;
                    return;
                }
            }

            if (bu != null)
            {
                data = Buffers.MergeBytes(bu, data);
                bu   = null;
            }

            while (true)
            {
                if (data.Length - index < 4)
                {
                    bu = new byte[data.Length - index];
                    Array.Copy(data, index, bu, 0, bu.Length);
                    iLen = 0;
                    break;
                }

                iLen = GetDataLen(data, index);
                if (index + iLen + 4 > data.Length)
                {
                    //包不完整
                    buf = new byte[data.Length - index - 4];
                    Array.Copy(data, index + 4, buf, 0, buf.Length);
                    iLen -= buf.Length;
                    break;
                }
                else if (index + iLen + 4 == data.Length)
                {
                    buf = new byte[iLen];
                    Array.Copy(data, index + 4, buf, 0, buf.Length);
                    //回调函数
                    this.BinaryInput(buf);
                    buf  = null;
                    iLen = 0;
                    break;
                }
                else
                {
                    buf = new byte[iLen];
                    Array.Copy(data, index + 4, buf, 0, buf.Length);
                    //回调函数
                    this.BinaryInput(buf);
                    buf    = null;
                    index += iLen + 4;
                }
            }
        }