Exemplo n.º 1
0
        /// <summary>
        /// 使用UTF8编码格式以同步方式向指定的Socket对象发送消息
        /// </summary>
        /// <param name="socket">socket对象</param>
        /// <param name="msg">发送的消息</param>
        public static void SendMsg(Socket socket, string msg)
        {
            //将字符串消息转换成字符数组
            byte[] buffer = ConvertHelper.StringToBytes(msg, Encoding.Default);

            //发送消息
            socket.Send(buffer, buffer.Length, SocketFlags.None);
        }
Exemplo n.º 2
0
        public static void EncodeLogin2(out byte[] buff)
        {
            string name = "admin";
            string pwd  = string.Empty;

            int locIdx = 0;
            int sNameLen = 0, sPwdLen = 0;
            int nameLen = 0, pwdLen = 0, conLen = 0;

            sNameLen = name.Length + 1;
            sPwdLen  = pwd.Length + 1;
            nameLen  = (sNameLen + 7) / 8 * 8;
            pwdLen   = (sPwdLen + 7) / 8 * 8;
            conLen   = 1 + nameLen + 1 + pwdLen;

            byte[] byteName = new byte[nameLen];
            for (int i = 0; i < name.Length; i++)
            {
                byteName[i] = (byte)name[i];
            }
            byteName[name.Length] = (byte)0;
            byte[] bytePwd = new byte[pwdLen];
            for (int i = 0; i < pwd.Length; i++)
            {
                bytePwd[i] = (byte)pwd[i];
            }
            bytePwd[pwd.Length] = (byte)0;
            ConvertHelper.StringToBytes(pwd, Encoding.ASCII);
            Random rand = new Random();

            for (int i = sNameLen; i < nameLen; i++)
            {
                byteName[i] = (byte)rand.Next(48, 125);
            }
            for (int i = sPwdLen; i < pwdLen; i++)
            {
                bytePwd[i] = (byte)rand.Next(48, 125);
            }

            byte[] content = new byte[conLen];
            content[locIdx++] = (byte)(sNameLen);
            fish.Encode(byteName, nameLen, FISH_KEY);
            Array.Copy(byteName, 0, content, locIdx, nameLen);
            locIdx           += nameLen;
            content[locIdx++] = (byte)(sPwdLen);
            fish.Encode(bytePwd, pwdLen, FISH_KEY);
            Array.Copy(bytePwd, 0, content, locIdx, pwdLen);

            NormalDataStruct data = new NormalDataStruct()
            {
                Code       = enumCommandType.LOGIN2_REQ,
                contentLen = conLen,
                Content    = content
            };

            EncodeData(data, out buff);

            //byte[] bf1 = new byte[] { 0xd0, 0xab, 0x22, 0x8a, 0x50, 0x35, 0x33, 0x11 };
            //byte[] bf2 = new byte[] { 0x92, 0x5c, 0x81, 0xf6, 0x8d, 0xdf, 0xfb, 0x60 };
            //byte[] bf3 = new byte[] { 0xed, 0xdf, 0xd8, 0x9e, 0x11, 0xce, 0x7b, 0xd5 };
            //fish.Decode(bf1, nameLen, FISH_KEY);
            //fish.Decode(bf2, nameLen, FISH_KEY);
            //fish.Decode(bf3, nameLen, FISH_KEY);
        }