hehexiaoxia 2017-02-16 14:47 采纳率: 100%
浏览 1055
已采纳

C#用socket发送组包给java服务器,调用recieve时ConnectionAbort。

 C#用socket发送组包给java服务器,调用recieve时ConnectionAbort。
java要求 将数据报文通过utf8编码发送。
connection和send都能通过。
调用接收直接返回0。
不知道是组包格式不对还是字节序,查资料说utf8不需要字节序,请大神帮忙分析一下。

  IPAddress ip = IPAddress.Parse("192.168.65.30");
            IPEndPoint endP = new IPEndPoint(ip, 8099);
            Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            client.Connect(endP);

            byte[] face1 = converttobytes("image/1.JPG");
            byte[] face2 = converttobytes("image/1.JPG");

            byte[] magic_word = Encoding.ASCII.GetBytes("HBVE");
            int version = 1;

            byte type = 1;
            int id = 10000;

            byte type1 = 1;
            byte type2 = 1;

            int face1_len = face1.Length;
            int face2_len = face2.Length;

            int totallength = 10 + face1.Length + face2.Length + 5;

            byte[] bytes = null;
            using (MemoryStream ms = new MemoryStream())
            {
                using (BinaryWriter bw = new BinaryWriter(ms, Encoding.UTF8))
                {
                    //bw.Write(bytes);

                    bw.Write(magic_word);
                    bw.Write(version);
                    bw.Write(totallength);
                    bw.Write(type);
                    bw.Write(id);
                    bw.Write(type1);
                    bw.Write(type2);
                    bw.Write(face1_len);
                    bw.Write(face1);
                    bw.Write(face2_len);
                    bw.Write(face2);

                    ms.Position = 0;

                    using (BinaryReader br = new BinaryReader(ms, Encoding.UTF8))
                    {
                        bytes = br.ReadBytes((int)ms.Length);
                    }
                }
            }

            try
            {
                SocketError error = SocketError.AccessDenied;
                int result = client.Send(bytes, 0, bytes.Length, SocketFlags.None, out error);
                byte[] by = new byte[client.ReceiveBufferSize];

                result = client.Receive(by, 0, by.Length, SocketFlags.None, out error);
            }
            catch (Exception ex)
            {
                throw ex;
            }

  • 写回答

2条回答 默认 最新

  • hehexiaoxia 2017-02-17 03:35
    关注

    已经解决了,是字节序的问题。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)