【问题标题】:Error sending data through Socket TCP / IP通过 Socket TCP/IP 发送数据时出错
【发布时间】:2018-08-27 20:35:49
【问题描述】:

下午好。 我正在尝试通过套接字(在 c# 中详细说明)将流发送到 javascript 中的 websocket 发送的数据较少。我收到一个我不知道以下内容的错误:

“无法将数据写入传输连接:连接被对等方重置。”

代码

JObject obj = JObject.Parse(VarTransform.reciveMASKXOR(bytes));
string docs = SearhLucene((int)obj.SelectToken("point"),Obj.SelectToken("data").ToString());                                            bytes = VarTransform.sendMaskXOR(docs);
//ERROR############
stream.Write(bytes, 0, bytes.Length);
//#################

异或编码

byte[] bytes = Encoding.UTF8.GetBytes(text);
            List<byte> Lbytes = new List<byte>();
            Lbytes.Add((byte)129);
            Lbytes.Add((byte)129);
            int Length = bytes.Length + 1;
            if (Length <= 125)
            {
                Lbytes.Add((byte)(Length));
                Lbytes.Add((byte)(Lbytes.Count));
            }
            else
            {
                if (Length >= 125 && Length <= 65535)
                {
                    Lbytes.Add((byte)126);
                    Lbytes.Add((byte)(Length >> 8));
                    Lbytes.Add((byte)Length);
                    Lbytes.Add((byte)Lbytes.Count);
                }
                else
                {
                    Lbytes.Add((byte)127);
                    Lbytes.Add((byte)(Length >> 56));
                    Lbytes.Add((byte)(Length >> 48));
                    Lbytes.Add((byte)(Length >> 40));
                    Lbytes.Add((byte)(Length >> 32));
                    Lbytes.Add((byte)(Length >> 24));
                    Lbytes.Add((byte)(Length >> 16));
                    Lbytes.Add((byte)(Length >> 8));
                    Lbytes.Add((byte)Length);
                    Lbytes.Add((byte)Lbytes.Count);
                }
            }
            Lbytes.RemoveAt(0);
            Lbytes.AddRange(bytes);
            return Lbytes.ToArray();

异或解码

byte b = buffer[1];
            int dataLength = 0;
            int totalLength = 0;
            int keyIndex = 0;
            if (b - 128 <= 125)
            {
                dataLength = b - 128;
                keyIndex = 2;
                totalLength = dataLength + 6;
            }
            if (b - 128 == 126)
            {
                dataLength = BitConverter.ToInt16(new byte[] { buffer[3], buffer[2] }, 0);
                keyIndex = 4;
                totalLength = dataLength + 8;
            }
            if (b - 128 == 127)
            {
                dataLength = (int)BitConverter.ToInt64(new byte[] { buffer[9], buffer[8], buffer[7], buffer[6], buffer[5], buffer[4], buffer[3], buffer[2] }, 0);
                keyIndex = 10;
                totalLength = dataLength + 14;
            }
            if (totalLength > buffer.Length)
                throw new Exception("The buffer length is small than the data length");
            var key = new byte[] { buffer[keyIndex], buffer[keyIndex + 1], buffer[keyIndex + 2], buffer[keyIndex + 3] };
            int dataIndex = keyIndex + 4;
            int count = 0;
            for (int i = dataIndex; i < totalLength; i++)
            {
                buffer[i] = (byte)(buffer[i] ^ key[count % 4]);
                count++;
            }
            string res = "";
            try
            {
                res=Encoding.UTF8.GetString(buffer, dataIndex, dataLength);
            }catch(Exception ex)
            {
                res = "all:a";
            }
            return res;        

找到

如果 docs 值为 "hello"(干净的字符串,没有 \n 或 \r 或空格)

"{'name':'newname'}"(干净的字符串没有 \n 或 \r 或空格)

崩溃

如果文档值 " " (spacesx4)

错误

如果文档值"{'name':'newname'},{'name':'newname'},{'name':'newname'},..."

有很多值失败,可能是异或算法(在C#中)

【问题讨论】:

标签: c# sockets tcp websocket


【解决方案1】:

经过多次、多次、多次...尝试后,我发现了以下内容,因此不会发生此错误:

步骤 1 清理字符串,如 \n \r \t 并分隔(标准化,使其不超过 4)。

第 2 步不超过发送的 3 MB 数据。

感谢您的帮助,我希望这对您有所帮助(我会发送图片但我仍然不能)

【讨论】:

    猜你喜欢
    • 2022-06-14
    • 1970-01-01
    • 1970-01-01
    • 2011-01-07
    • 2015-11-01
    • 2015-08-25
    • 2020-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多