Exemple #1
0
        /// <summary>
        /// 以同步方式接收消息,并转换为UTF8编码格式的字符串,使用5000字节的默认缓冲区接收。
        /// </summary>
        /// <param name="socket">socket对象</param>
        public static string ReceiveMsg(Socket socket)
        {
            //定义接收缓冲区
            byte[] buffer = new byte[5000];
            //接收数据,获取接收到的字节数
            int receiveCount = socket.Receive(buffer);

            //定义临时缓冲区
            byte[] tempBuffer = new byte[receiveCount];
            //将接收到的数据写入临时缓冲区
            Buffer.BlockCopy(buffer, 0, tempBuffer, 0, receiveCount);
            //转换成字符串,并将其返回
            return(ConvertHelper.BytesToString(tempBuffer, Encoding.Default));
        }
Exemple #2
0
        public void ListenPort()
        {
            Socket     server  = NetHelper.CreateTcpSocket();
            IPEndPoint ipPoint = new IPEndPoint(IPAddress.Any, PORT_SELF_VEDIO);

            server.Bind(ipPoint);
            server.Listen(0);
            while (true)
            {
                Socket reader = server.Accept();
                byte[] buff   = new byte[BUFFER_SIZE];

                int byteBuff = reader.Receive(buff);

                string strMsg = ConvertHelper.BytesToString(buff, System.Text.Encoding.UTF8);
                Console.WriteLine(strMsg);
            }
        }
Exemple #3
0
        public static Login1Struct DecodeLogin1(byte[] data)
        {
            Login1Struct login1 = new Login1Struct();

            login1.Length = data[0];
            int len = (login1.Length + 7) / 8 * 8;

            byte[] codeInfo = new byte[data.Length - 1];
            Array.Copy(data, 1, codeInfo, 0, codeInfo.Length);
            //Console.WriteLine(ConvertHelper.BytesToString(codeInfo, Encoding.ASCII));
            if (!fish.Decode(codeInfo, len, FISH_KEY))
            {
                Console.WriteLine("解码失败");
            }
            //Console.WriteLine(codeInfo);
            login1.Plain = ConvertHelper.BytesToString(codeInfo, Encoding.ASCII);
            login1.Plain = login1.Plain.Substring(0, login1.Length - 1);
            //Console.WriteLine(login1.Plain);
            return(login1);
        }
Exemple #4
0
        public static WifiRespInfo DecodeWifiSearch(byte[] buff)
        {
            WifiRespInfo info = new WifiRespInfo();

            int locIdx = 4;

            info.Magic = ConvertHelper.BytesToInt32(buff, 0, true);
            info.Code  = (enumCommandType)buff[locIdx++];

            int wifiIDLen = buff[locIdx++];

            byte[] wifiIDByte = new byte[wifiIDLen];
            Array.Copy(buff, locIdx, wifiIDByte, 0, wifiIDLen);
            locIdx += wifiIDLen;
            info.ID = ConvertHelper.BytesToString(wifiIDByte, Encoding.ASCII);

            int wifiVersionLen = buff[locIdx++];

            byte[] wifiVersionByte = new byte[wifiVersionLen];
            Array.Copy(buff, locIdx, wifiVersionByte, 0, wifiVersionLen);
            locIdx      += wifiVersionLen;
            info.Version = ConvertHelper.BytesToString(wifiVersionByte, Encoding.ASCII);

            int wifiWebLen = buff[locIdx++];

            byte[] wifiWebByte = new byte[wifiWebLen];
            Array.Copy(buff, locIdx, wifiWebByte, 0, wifiWebLen);
            locIdx  += wifiWebLen;
            info.Web = ConvertHelper.BytesToString(wifiWebByte, Encoding.ASCII);

            int wifiNameLen = buff[locIdx++];

            byte[] wifiNameByte = new byte[wifiNameLen];
            Array.Copy(buff, locIdx, wifiNameByte, 0, wifiNameLen);
            locIdx   += wifiNameLen;
            info.Name = ConvertHelper.BytesToString(wifiNameByte, Encoding.ASCII);

            info.IP            = string.Format("{0}.{1}.{2}.{3}", buff[locIdx++], buff[locIdx++], buff[locIdx++], buff[locIdx++]);
            info.NetMask       = string.Format("{0}.{1}.{2}.{3}", buff[locIdx++], buff[locIdx++], buff[locIdx++], buff[locIdx++]);
            info.IsDhcp        = buff[locIdx++] == 1;
            info.IPStatic      = string.Format("{0}.{1}.{2}.{3}", buff[locIdx++], buff[locIdx++], buff[locIdx++], buff[locIdx++]);
            info.NetMaskStatic = string.Format("{0}.{1}.{2}.{3}", buff[locIdx++], buff[locIdx++], buff[locIdx++], buff[locIdx++]);
            info.NetGateStatic = string.Format("{0}.{1}.{2}.{3}", buff[locIdx++], buff[locIdx++], buff[locIdx++], buff[locIdx++]);
            info.DNS1          = string.Format("{0}.{1}.{2}.{3}", buff[locIdx++], buff[locIdx++], buff[locIdx++], buff[locIdx++]);
            info.DNS2          = string.Format("{0}.{1}.{2}.{3}", buff[locIdx++], buff[locIdx++], buff[locIdx++], buff[locIdx++]);
            byte[] wifiPort = new byte[2];
            info.Port = ConvertHelper.BytesToInt16(buff, locIdx, true);
            locIdx   += 2;

            info.IsHttps = buff[locIdx++] == 1;

            int wifiNOLen = buff[locIdx++];

            byte[] wifiNOByte = new byte[wifiNameLen];
            Array.Copy(buff, locIdx, wifiNOByte, 0, wifiNOLen);
            Array.Reverse(wifiNOByte);
            locIdx     += wifiNOLen;
            info.Number = ConvertHelper.BytesToString(wifiNOByte, Encoding.ASCII);

            info.Type = buff[locIdx++];

            return(info);
        }