Beispiel #1
0
        public void send(byte[] bytes, object client)
        {
            //写入4字节的包头长度
            var package = new byte[bytes.Length + packageHeadLen];

            Buffer.BlockCopy(BitConverter.GetBytes(NetUtils.ConvertToEndian(bytes.Length, Endian.BigEndian)), 0, package, 0, packageHeadLen);
            Buffer.BlockCopy(bytes, 0, package, packageHeadLen, bytes.Length);
            ((Socket)client).Send(package);
        }
Beispiel #2
0
        private void OnHeadReceived(object sender, SocketAsyncEventArgs context_head)
        {
            var client = (ClientObject)context_head.UserToken;
            int len    = BitConverter.ToInt32(context_head.Buffer, 0);

            len = NetUtils.ConvertToEndian(len, Endian.BigEndian);
            if (len > 0)
            {
                if (!checkSocketConnect(client.socket))
                {
                    return;
                }
                client.context_body.SetBuffer(new byte[len], 0, len);
                client.socket.ReceiveAsync(client.context_body);
            }
            else
            {
                receive((Socket)sender);
            }
        }