Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过套接字发送包含位图对象的结构_C#_Sockets_Udp_Structure - Fatal编程技术网

C# 通过套接字发送包含位图对象的结构

C# 通过套接字发送包含位图对象的结构,c#,sockets,udp,structure,C#,Sockets,Udp,Structure,我正试图发送以下结构: public struct imageBlock { public uint blockId; public Bitmap block; } 使用udp套接字 首先,我使用以下函数将结构转换为字节数组: public byte[] getBytes(MyStructure str) { int size = Marshal.SizeOf(str); byte[] arr = n

我正试图发送以下结构:

 public struct imageBlock

    {
        public uint blockId;
        public Bitmap block;
    }
使用udp套接字 首先,我使用以下函数将结构转换为字节数组:

 public byte[] getBytes(MyStructure str)
    {
        int size = Marshal.SizeOf(str);
        byte[] arr = new byte[size];
        IntPtr ptr = Marshal.AllocHGlobal(size);
        Marshal.StructureToPtr(str, ptr, true);
        Marshal.Copy(ptr, arr, 0, size);
        Marshal.FreeHGlobal(ptr);

        return arr;
    }
我把它寄给你:

            buffer = crop.getBytes((imageBlock)ImageToBeSent[i]);

            crop.sender(buffer);
当我尝试接收结构时,出现了一个异常:

                data = client.Receive(ref localEp);

                int size = Marshal.SizeOf(myStructure);
                IntPtr ptr = Marshal.AllocHGlobal(size);

                Marshal.Copy(data, 0, ptr, size);

                myStructure= (MyStructure)Marshal.PtrToStructure(ptr, myStructure.GetType());
                Marshal.FreeHGlobal(ptr);
在线:

  myStructure= (MyStructure)Marshal.PtrToStructure(ptr, myStructure.GetType());
我得到了一个例外:

运行时遇到致命错误。错误地址位于线程0x1860上的0x792a02fe。错误代码为0xc0000005。此错误可能是CLR或用户代码的不安全或不可验证部分中的错误。此错误的常见来源包括COM互操作或PInvoke的用户封送错误,这可能会损坏堆栈


谢谢你的帮助

出于好奇,您使用非托管代码和POINER来执行此操作有什么原因吗?除非我遗漏了什么,否则可以不用InteropServicesNop,没有特别的原因,你有更好的主意吗?你能具体告诉我你想做什么吗?你使用套接字而不是web服务有什么原因吗?我解决了这个问题,我只是序列化了套接字,它工作了,谢谢大家的帮助