1

I am just starting to get into C# UDP networking, and I was wondering if I could send and receive lists. I have code from another question which allows for sending strings, but I would like to send a type List<KeyValuePair<int, String>> or a SortedDictionary. This is the code I have for sending strings:

     SortedDictionary<int, String> m_highScorers = new SortedDictionary<int, String>();
     List<KeyValuePair<int, string>> newList = new List<KeyValuePair<int, string>>();

     m_highScorers.Add(1, "Fish");
     m_highScorers.Add(2, "Fish");
     m_highScorers.Add(3, "Koala");
     m_highScorers.Add(4, "Bear");
     m_highScorers.Add(5, "Lion");

     foreach (KeyValuePair<int, string> p in m_highScorers.OrderBy(key => key.Key))
     {
        newList.Add(p);
     }
     byte[] packetData = System.Text.ASCIIEncoding.ASCII.GetBytes(newList.ToString());

     string ServerIP      = "192.168.0.25";
     string IP            = ReturnIPAddress();
     int port             = 80;

     IPEndPoint ep        = new IPEndPoint(IPAddress.Parse(ServerIP), port);

     Socket client        = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
     client.SendTo(packetData, ep);
     Console.Read();
  }

  static string ReturnIPAddress()
  {
     IPHostEntry host;
     string localIP = "?";
     host           = Dns.GetHostEntry(Dns.GetHostName());

     foreach (IPAddress ip in host.AddressList)
     {
        if (ip.AddressFamily.ToString() == "InterNetwork")
        {
           localIP = ip.ToString();               
        }
     }
     return localIP;
  }

This code works fine for the "ToString()" but when looking at it from another computer, it doesn't show the contents of the list or the SortedDictionary. Please let me know if there is a way to send either of these structures using UDP packets.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Beneagle
  • 93
  • 8
  • What does the receiver receive? What did you try to debug this? – usr Jun 12 '15 at 21:41
  • `newList.ToString()` does not return what you think it returns. All you will get back is `System.Collections.Generic.List'1System.Collections.Generic.KeyValuePair'2[System.Int32,System.String]]` – Scott Chamberlain Jun 12 '15 at 21:51
  • Also Sending a list over UDP is not a very good idea, the max size you can safely send using udp over the internet [is 512 bytes](http://stackoverflow.com/questions/1098897/what-is-the-largest-safe-udp-packet-size-on-the-internet), a list could easily get larger than that. You really should be using TCP. – Scott Chamberlain Jun 12 '15 at 21:54
  • @ScottChamberlain That is exactly the problem I am facing. I want to not receive that long string, but the List which displays all of my data that I sent over UDP. – Beneagle Jun 12 '15 at 22:07
  • 2
    You need to _serialize_ your data before sending over a network. Also, are you trying this just for the experience? Because if you're just trying to get things done, there's this whole thing called WCF that will do all this work for you. – John Saunders Jun 12 '15 at 22:17
  • Please research both UDP datagrams and data serialization, so that you have a useful starting point for your question. As asked, it is far too broad, as there's way too much information that would need to be provided. The short version: transmitting collection data structures using UDP is going to be challenging, to say the least. Unless you are just 100% stuck using UDP, you will be better off using some other mechanism (e.g. TCP, or even better, a full-featured communications library/framework like WCF). – Peter Duniho Jun 13 '15 at 03:21

0 Answers0