By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,009 Members | 1,767 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,009 IT Pros & Developers. It's quick & easy.

UDP send receive with different instances?

Plater
Expert 5K+
P: 7,872
I am a bit mad at myself, as I should know how to do this.
I am almost certain it is because I am using UdpClient and not a Socket object.

What am I looking to do is have one object sit in listen mode, and report anything it gets back.
And use the other object to send out data (which would trigger other devices to send out datagrams that the first object should be able to pick up.)

So I have one thread that does this:
Expand|Select|Wrap|Line Numbers
  1. UdpClient tud = new UdpClient(1900);
  2. //tud.ExclusiveAddressUse = false;//Not allowed if connected or bound
  3. tud.EnableBroadcast = true;
  4. tud.MulticastLoopback = true;
  5.  
  6. UdpState s = new UdpState();
  7. s.e = new IPEndPoint(IPAddress.Any, 1900);
  8. s.u = tud;
  9. Console.WriteLine("Starting to do work"); 
  10. while (_keepgoing)
  11. {
  12.     Thread.Sleep(10);
  13.     if (tud.Available>0)
  14.     {
  15.         IAsyncResult iar = tud.BeginReceive(new AsyncCallback(ReceiveCallback), s);
  16.         bool tbr = iar.IsCompleted;
  17.     }
  18. }
  19.  
And the thread that send out the message
Expand|Select|Wrap|Line Numbers
  1. UdpClient ud = new UdpClient();
  2. //data is my byte[] payload
  3. //myBroadcast is a broadcast IPEndPoint
  4. int bsent = ud.Send(data, data.Length, myBroadcast);
  5.  
Now in the code that sends out the data, ud.Available shows that data is comming in, even though its not bound to any particular local endpoint.
And the thread that listens for data to come in, never sees anything.

I am going to re-work the project using the Socket object I think and see if I get better results.
If anyone has any suggestions, I would like to hear them.


EDIT: I used Sockets and still failed?
Dec 9 '08 #1
Share this Question
Share on Google+
1 Reply


Plater
Expert 5K+
P: 7,872
I am now doing this, and getting even stranger results:
Expand|Select|Wrap|Line Numbers
  1. IPEndPoint Any = new IPEndPoint(IPAddress.Any, 1900);
  2.  
  3. Socket ts1 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  4. ts1.Blocking = true;
  5. ts1.EnableBroadcast = true;
  6. ts1.ExclusiveAddressUse = false;
  7. ts1.MulticastLoopback = true;
  8. ts1.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  9. ts1.Bind(Any);
  10.  
  11. Socket ts2 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  12. ts2.Blocking = true;
  13. ts2.EnableBroadcast = true;
  14. ts2.ExclusiveAddressUse = false;
  15. ts2.MulticastLoopback = true;
  16. ts2.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  17. ts2.Bind(Any);
  18.  
  19. byte[] data = DiscoverString();
  20. ts1.SendTo(data, myBroadcast);
  21.  
  22. Thread.Sleep(10);
  23. _keepgoing = true;
  24. DateTime start = DateTime.Now;
  25. while (_keepgoing)
  26. {
  27.     Thread.Sleep(10);
  28.     if (ts1.Available > 0)
  29.     {
  30.         EndPoint whosentme = new IPEndPoint(IPAddress.Any, 1900);
  31.         byte[] getd= new byte[ts1.Available];
  32.         int realamount= ts1.ReceiveFrom(getd, SocketFlags.None, ref whosentme);
  33.         string msg = Encoding.ASCII.GetString(getd, 0, realamount);
  34.         Console.WriteLine("Socket1 Found Message[" + whosentme.ToString() + "]!\r\n" + msg + "\r\n", null);
  35.     }
  36.     if (ts2.Available > 0)
  37.     {
  38.         EndPoint whosentme = new IPEndPoint(IPAddress.Any, 1900);
  39.         byte[] getd = new byte[ts2.Available];
  40.         int realamount = ts2.ReceiveFrom(getd, SocketFlags.None, ref whosentme);
  41.         string msg = Encoding.ASCII.GetString(getd, 0, realamount);
  42.         Console.WriteLine("Socket2 Found Message[" + whosentme.ToString() + "]!\r\n" + msg + "\r\n", null);
  43.     }
  44.     if (((TimeSpan)(DateTime.Now - start)).TotalSeconds > 5)
  45.     {//exit after 5 seconds
  46.         _keepgoing = false;        
  47.     }
  48. }
  49.  
I send the broadcast out on ts1.
Both ts1 and ts2 see the original broadcast message.
BUT this time, only ts2 (!!!) sees the return messages.
While this was sort of what I wanted, it still doesn't make any sense to me.

I even tried changing which order they called .Bind(), no difference.
If I send with ts2, its the same. Both see the original, only ts2 sees the return messages.

EDIT: Actually it seems to be random who gets the reply messages. All messages go to the same socket though. Race condition? I even tried just PEEK to leave the data there to see if both sockets would get it, not the case.

EDIT: Seems this here has a good explanation as to why: http://bytes.com/groups/net-c/233554...apps-same-time
Dec 9 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.