2015-06-22 68 views
0

我想通過將它轉換成字節數組來組播數據結構數據。一切工作正常與下面的代碼,除了字符串變量的結構數據沒有收到客戶端從服務器發送,而是顯示空字符串或其他字符。請建議我解決這個問題。在C#中使用udpmulticasting發送數據結構數據

謝謝inadvance。

這裏是我的代碼:

ServerCode

struct CIFSPacket 
     { 
      public int quantity; 
      public double price; 
      public string Buffer; 
     } 
     static void Main(string[] args) 
     { 
      Socket server=null; 
      try 
      { 
       server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); 
       IPEndPoint iep = new IPEndPoint(IPAddress.Parse("224.100.0.1"), 9050); 

       int i = 0; 
       while (true) 
       { 
        byte[] structData = new byte[4096]; 
        //server.sendto 
        CIFSPacket pkt = new CIFSPacket(); 
        pkt.quantity = i++; 
        pkt.price = i + 0.12; 
        pkt.Buffer = "RELIANCE";      
        structData = StructureToByteArray(pkt); 
        server.SendTo(structData, iep);           
        Console.WriteLine(pkt.Buffer+" - "+pkt.quantity+" - "+pkt.price);      
       } 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine(ex.ToString()); 
      } 
      finally 
      { 
       if (server != null) 
        server.Close(); 
      } 
     } 
private static byte[] StructureToByteArray(CIFSPacket 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; 
     } 

ClientCode

public struct CIFSPacket 
     { 
      public int quantity; 
      public double price; 
      public string Buffer; 
     } 

static void Main(string[] args) 
     { 
      Socket sock = null; 
      CIFSPacket pkt; 
      try 
      { 
       sock = new Socket(AddressFamily.InterNetwork, 
       SocketType.Dgram, ProtocolType.Udp); 
       Console.WriteLine("Ready to receive…"); 
       IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050); 
       EndPoint ep = (EndPoint)iep; 
       sock.Bind(iep); 
       sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, 
        new MulticastOption(IPAddress.Parse("224.100.0.1"))); 

       while (true) 
       { 
        byte[] data = new byte[4096];      
        int recv = sock.ReceiveFrom(data, ref ep);      
        pkt = ByteArrayToStructure(data); 
        Console.WriteLine(pkt.Buffer + " -- " + pkt.quantity + " -- " + pkt.price); 
       } 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine(ex.ToString()); 
      } 
      finally 
      { 
       sock.Close(); 
       Console.ReadLine(); 
      }    
     } 

private static CIFSPacket ByteArrayToStructure(byte[] arr) 
     { 
      CIFSPacket str = new CIFSPacket(); 
      int size = Marshal.SizeOf(str); 
      IntPtr ptr = Marshal.AllocHGlobal(size); 
      Marshal.Copy(arr, 0, ptr, size); 

      str = (CIFSPacket)Marshal.PtrToStructure(ptr, str.GetType()); 
      Marshal.FreeHGlobal(ptr); 

      return str; 
     } 

輸出

Server

Client

回答

0

的問題是,string型在你的結構是一個管理的指針,而不是一個值類型。

嘗試

[MarshalAsAttribute(UnmanagedType.LPArray, ArraySubType=UnmanagedType.U2, SizeConst=Max_Size_Of_Your_String)] 
public char[] Buffer=new char[Max_Size_Of_Your_String]; 

或者更換

public string Buffer; 

,你應該能夠使用

[MarshalAsAttribute(UnmanagedType.XXX)] 

在你public string Buffer聲明,告訴編譯器如何將字符串轉換到/從字節。 XXX是支持的字符串類型之一,例如LPStr。但在這種情況下,您需要在相應的屬性中指定字符串和/或結構的最大大小,以便編譯器知道爲字符串分配多少空間。

您可能需要查看下面的文章以瞭解更多信息:

https://limbioliong.wordpress.com/2011/08/28/passing-managed-structures-with-strings-to-unmanaged-code-part-3/

convert struct handle from managed into unmanaged C++/CLI