473,396 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

UDP send receive with different instances?

Plater
7,872 Expert 4TB
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
1 5297
Plater
7,872 Expert 4TB
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

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

Similar topics

6
by: Tom | last post by:
I try to write a simple tcp based job queue server. It's purpose is to get commands from a client and store them in a queue (STL). The server has a thread which checks periodically the queue,...
1
by: Kitchen Bin | last post by:
Hi. I am trying to use Sockets to do multiple Send and Receives via HTTP (not simultaneously). A first pair of Send/Receives works fine and sure enough I receive HTML back, but the next...
13
by: Manfred Braun | last post by:
Hi All, I am trying to understand the blocking method socket.Send(). The call blocks as expected, but does this mean, it returnes after the underlying TCP layer got a confirmation, that the send...
0
by: Steve | last post by:
I am looking into UdpClient sample code from MSDN where one UdpClient object used for both send and receive operation. Please take a look at the link below...
6
by: Cameron Eckman | last post by:
I get various errors when I try to use email on the machine I have. XP professional. It works fine on another machine with NT 2000 server. Below is the code: 'BEGIN CODE Dim oMail As New...
4
by: Joseph Geretz | last post by:
We use a Soap Header to pass a token class (m_Token) back and forth with authenticated session information. Given the following implementation for our Logout method, I vastly prefer to simply code...
5
by: SungHyun Nam | last post by:
Hello, If there are two ethernet device, how I can select a device to send/receive? Actually I want to do loopback test between twe ethernet device. Send a UDP packet through eth0 and receives...
4
by: =?Utf-8?B?Y2hyaXNiZW4=?= | last post by:
Hi, My understanding is that for the same socket, assuming send and receive run at two different threads, I have to use BeginReceive to avoid the blocking on send part. However, I can jsut use...
2
by: SvenV | last post by:
I based my program on the asynchronous client/server example on msdn. There weren't too many modifications to the original code. I just created some extra code to response to different messages. F.e....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.