locked
Multicast with UDP Transport Sample RRS feed

  • Question

  • I'm trying to use the UDP Transport sample to perform a multicast from a single server app to multiple clients. I'd like the server to broadcast the message on a subnet to any clients that just happen to be listening.

    My problem is that I can't find any documentation on how to do this using the UDP transport sample. Any ideas or suggestions?

    Thanks.

    Bruce Bukovics

    Author of .NET 2.0 Interoperability Recipes
    http://www.apress.com/book/bookDisplay.html?bID=10116





    Monday, April 10, 2006 9:21 PM

Answers

  • You need to change the UdpChannelListener.CreateSocket implementation to the following:

            Socket CreateListenSocket(IPAddress ipAddress, int port)
            {
                bool isIPv6 = (ipAddress.AddressFamily == AddressFamily.InterNetworkV6);
                Socket socket = null;
               
                if (multicast)
                {
                    IPAddress anyIPAddr = IPAddress.Any;
                    if (isIPv6)
                        anyIPAddr = IPAddress.IPv6Any;

                    IPEndPoint endPoint = new IPEndPoint(anyIPAddr, port);
                    socket = new Socket(endPoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
                    socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
                    socket.Bind(endPoint);

                    if (isIPv6)
                    {
                        socket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
                            new IPv6MulticastOption(ipAddress));
                    }
                    else
                    {
                        socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership,
                            new MulticastOption(ipAddress));
                    }
                }
                else
                {
                    IPEndPoint endPoint = new IPEndPoint(ipAddress, port);
                    socket = new Socket(endPoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
                    socket.Bind(endPoint);
                }

                return socket;
            }
     

    This implementation will be available in the next release

    Wednesday, April 12, 2006 9:00 PM

All replies

  • You need to change the UdpChannelListener.CreateSocket implementation to the following:

            Socket CreateListenSocket(IPAddress ipAddress, int port)
            {
                bool isIPv6 = (ipAddress.AddressFamily == AddressFamily.InterNetworkV6);
                Socket socket = null;
               
                if (multicast)
                {
                    IPAddress anyIPAddr = IPAddress.Any;
                    if (isIPv6)
                        anyIPAddr = IPAddress.IPv6Any;

                    IPEndPoint endPoint = new IPEndPoint(anyIPAddr, port);
                    socket = new Socket(endPoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
                    socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
                    socket.Bind(endPoint);

                    if (isIPv6)
                    {
                        socket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
                            new IPv6MulticastOption(ipAddress));
                    }
                    else
                    {
                        socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership,
                            new MulticastOption(ipAddress));
                    }
                }
                else
                {
                    IPEndPoint endPoint = new IPEndPoint(ipAddress, port);
                    socket = new Socket(endPoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
                    socket.Bind(endPoint);
                }

                return socket;
            }
     

    This implementation will be available in the next release

    Wednesday, April 12, 2006 9:00 PM
  • Thanks for the response.  I'll give that a try.

    Bruce Bukovics
    Author of .NET 2.0 Interoperability Recipes
    http://www.apress.com/book/bookDisplay.html?bID=10116
    Thursday, April 13, 2006 1:57 PM
  • Hi, Bruce

    How is your multicast? does it work? im trying the sample according to Ed Pinto 's code but have no clue how to configure the multicast work.

    I assume you need to set the Multicast = "true" in some where, right?

    what i want is one client app send a message and all other client apps(listeners) should receive base on this Udp multicast.

    Very much appreciate if you could share your ideas.

    thanks a lot

    Monday, May 15, 2006 9:58 AM
  • To be honest, I haven't had a chance to give this code a try.   My current projects have been pulling me in other directions.  I plan to revisit this soon.

    Bruce
    Wednesday, August 2, 2006 5:43 PM
  • Thanks. i have tried it with Beta and modified a bit of it so it works for me now.

    Would love to share if anyone interested.

    Thursday, August 3, 2006 12:19 AM
  • Be warned the UDP sample implementation is significantly slower than the nettcp implementation  ( i have seen 3* slower)  and nearly as slow as the http/ws bindings. When queried with some times it was stated it is not optomised ( It would have been nice if the sample showed you how to build a fast transport) . If you had >30 clients on 1 network segment ( subnet)  , id consider multi cast but otherwise id just use multiple tcp conversations.

    Regards,

     

    Ben

    Tuesday, August 8, 2006 12:03 PM
  •  

    I've had a quick look at the UDP sample and like Oaix I cannot instantly see how to configure multicast.  Also I compiled the sample ok but it hangs on the first socket close. I am running .Net 3.0 RC1. Oaix can you post an update of your experience. In particular info on:

    - How to configure multicast + any sample code you want to share.

    - How well udp works in practice. I mean can the sample be considered to be production grade? Another contributor said it was slow and needed optimizing (how?).

    - How far you got in using net.msmq multicasting.

     

    My primary concern is reach rather than performance. I want to multicast to clients across domains in the extended corporate intranet (and possibly remote clients over the Internet) with the minimum fuss. I figured net.msmq using srmp would be the best offering out-the-box?  All comments appreciated.

    Monday, October 30, 2006 5:59 PM
  • Ben is right.  have tested in Tcp with 20 clients within a LAN and the result is really satifying.

    The sample i modified is base on June CTP but i guess it should work with RC1 too.

    I did it without consider the performance. right now we are using MSMQ multicast instaed of UDP for more clients. would love to hear from anyone who has tried it with UDP and its performance.

    leave me a e mail addr so in can send the whole sample to you.

    Tuesday, October 31, 2006 1:57 AM
  • Thanks oaix

     

    - Does your app multicast across multiple domains?

    - How does the UDP version compare vs. TCP and MSMQ in your tests?

    - Are you using msmq or net.msmq?

    - Does your app automatically switch to msmq when the number of active users hits a threshold?

    - Please send sample to webmaster<"at">infosecurityproducts.com

     

    Tuesday, October 31, 2006 7:38 AM
  • - Does your app multicast across multiple domains?

      Yes. thats part of the requirement.

    - How does the UDP version compare vs. TCP and MSMQ in your tests?

      We haven't do the comparaion, as i said the UDP only for POC to know that muticast works with modify the sample.

    - Are you using msmq or net.msmq?

      We are using msmq for multicast, so it has to be msmqintegration

    - Does your app automatically switch to msmq when the number of active users hits a threshold?

      No, we use msmq to distribute data (multicast) that change very freqently for many clients

    - Please send sample to webmaster<"at">infosecurityproducts.com

      give me 1-2 days. need to find teh sample and try to make it work with RC1. which make more sense i guess

    Wednesday, November 1, 2006 2:45 AM
  • It also worth ocnsiderirng WS-eventing type publsih subscribe system

    Ben

    Wednesday, November 1, 2006 5:13 AM
  • Oaix

    Thanks for the info and the offer to package your sample. In actual fact, please don't worry about it. I have pretty much decided to use msmq - basically because it is simpler - and I don't want to waste your time.

    Ben. I am essentially implementing a pub-sub pattern, the issue is which transport to use. I have bookmarked an interesting ws-eventing article I found on CodeProject I intend to read soon. I need to multicast because my app publishes a lot of live data updates. Clients can be in the service domain, another domain within the corporate intranet, or a remote business partner domain. I need an Http transport because there can be firewalls between any client and the service.

    net.msmq with SRMP looks like a good bet, but because it requires both ends of a channel to have IIS I think I have to create a store and forward director to go at the client domain boundary. The director would multicast to clients on the local domain.

     

    Wednesday, November 1, 2006 7:53 AM
  • as a matter of fact, we are using WS-Eventing for the pub/sub right now. so far looking very good. as for our case, the live data we use msmq multicast for the quick deliver but we allow this type of date 'lost' once a while. some 'must' deliver data we use tcp/ip in LAN (so far) . so far so good.

     

     

    Wednesday, November 1, 2006 1:07 PM
  •  

    I am still not totally comfortable with msmq as a multicast delivery mode for a ws-eventing/pub-sub design pattern. A fundamental concept of pub-sub is that the subscription part should be dynamic. Clients should be able to subscribe and unsubcribe to any number of topics on the fly. With msmq, IP multicast is achieved by configuring a queue with a multicast address so each topic has to have a dedicated queue. What's more it is most likely that the subscription manager will have to create create queues on demand. So for example if you had a global stock price system that lets clients subscribe for price updates by individual stocks you would have to create a queue for every stock in the known universe. 

    That seems impractical if not impossible. The workaround is to restrict subscription scope to a higher level e.g. US stocks, European Stocks etc and transfer the burden of filtering to the client. That is all well and good, but it goes against the objective of pub-sub.

    I have read Roman Kiss' ws-eventing article. He talks about the various ws-eventing delivery modes but at first glance the demo only seems to support "push mode". It seems to me that in nearly all cases an intranet based pub-sub implementation would benefit from "trap mode" (i.e. UDP multicast). Doing this in WCF is quite daunting and there is little documentation or sample code. 

    Could you good folks at MS please post a WCF multicast sample somewhere, or direct me to one that is already out there. Roman, if you are listening could you modify your ws-eventing sample to include trap mode?

    Friday, November 3, 2006 11:21 AM
  • Oaix,
    We are in need of similar pub/sub reqt's as you (1000+ msg/second over 20-30 clients).  Did you find the multicast MSMQ implementation to be the only performant option in your case? or did the ws-eventing approach work as well? Would love to see a copy of your sample...david.carrico@gmail.com.donotspam

    Thanks
    dave
    Tuesday, November 7, 2006 12:25 AM
  • MSMQ Multicast way currently work for us but i certainly want to experiment UDP if time allows. the issue with msmq multicast is we have to use integration binding so it is not a two-side WCF approach and hard to switch to other transport by flipping the configure file. 

    plus we have other reason for using MSMQ , we have PDA clients and curently we use MSMQ to communicate with them.

    we also using TCP with WS-Eventing as Sub/pub. the cureent service we build has no problem support 1000 msg/sec (each msg <5k for our case) and we have tested for 30 clients. so far so good.

    I suggested you refer to Roman's WS-Eventing article. its very good one.

    oaix

     

    Tuesday, November 7, 2006 8:25 AM
  • I can't get msmq multicast to work at all, either through WCF or standard system.messaging. I think there must be a networking config problem on my machine. I am running WS2003 R2. IGMP appears to be enabled, but the multicast routing table has no entries ("netsh routing ip show mfe"). I am trawling through technet documentation, but to no avail yet.

    Have any of you guys had similar probs?

    Monday, November 13, 2006 9:07 AM
  • OK scrub that ... it has miraculously started working after I connected a client machine to my server. No idea why. It works fine now even when the client is disconnected. I went rooting through the RRAS config but changed nothing.

    I have mc working with wcf to wcf msmqIntegrationBinding on both ends. It also works with msmq to WCF. I have not tested msmq at the server end but it should work.

    For general info the key factors when working with this are (a) with an msmq client the multicast address is e.g. "FormatName:MULTICAST=234.1.1.1:1234";  with a wcf client the mc address is "msmq.FormatName:MULTICAST=234.1.1.1:1234"; (b) As usual with msmq make sure you have the binding properties the same on both sides - securitymode=none, durable=false, exactlyonce=false; (c)Sending to a queue that has a multicast address is not the same as sending to a multicast address;(d) WS2003 is configured for mc by default. You can find all the config params in RRAS.

    Monday, November 13, 2006 12:36 PM
  • Hello ,

    >Ben. I am essentially implementing a pub-sub pattern, the issue is which transport to use. I >have bookmarked an interesting ws-eventing article I found on CodeProject I intend to read >soon.

    Is this Romans article ? It is the basis of what i have used . Added push and changed it so can use proxies.

     

    >I need to multicast because my app publishes a lot of live data updates. Clients can be in the >service domain, another domain within the corporate intranet, or a remote business partner >domain. I need an Http transport because there can be firewalls between any client and the >service.

     

    I have about 1500 GPRS based Pocket PC clients and use HTTP to a fascade service and tcp internally. The Eventing service will handle about 50,000 events per day which go to 1500 clients ( most events do go to only 1-5  clients ) for push capable clients(pc's)  they go direct for pull the eventing service puts it in a MessageStoreService for pickup . The fun is still to come.

     

    Used MSMQ and asmx web services in the last project and have now removed all the MSMQ code. Too many issues when things go wrong .  

    Regards,

     

    Ben

    Wednesday, November 15, 2006 9:46 AM
  • Hi

    >Is this Romans article ? It is the basis of what i have used . Added push and changed it so can use proxies.

    It's the one that provides a WS-Eventing class library right? I'm not clear on the mods you made to the sample. Have you extended it to support UDP transport? Please expand.

    >Used MSMQ and asmx web services in the last project and have now removed all the MSMQ code. Too many issues when things go wrong .

    Interesting. Are these windows ce issues or msmq issues generally e.g. the time it takes to unscramble all the backed up messages after a blockage? I figured that using msmq multicast would be workable if I put a short TTL on the messages.  I can fire and forget.  Disconnected clients don't need to pick up the messages they missed.

    Wednesday, November 15, 2006 12:28 PM
  • Some more multicast/msmqIntegration gotchas I forgot to mention:

    - Multicast will not work when the LAN interface is down. So if you are trying to multicast to a local host group, it will fail silently.

    - msmqIntegration does not use the datacontractserializer. DataContracts that work fine with other bindings may not work with msmqIntegration. If you override GetHashCode() make sure it does not throw any exceptions e.g. when properties have null values. Make sure all your DataMember properties can get and set.

    Friday, November 17, 2006 8:31 AM
  • - The mods i made was to supply pull functionality , some performance enhancements and to allow the use of a proxy instead of linking to the ws_Eventing dll. I will make more mods as we go.

     - More architecture issues . I used MSMQ for more general background persistant queueing work ( thats a mouthfull) . Basically the client would submit a long running job to a web service which would then add it to a queue and return . The problem was when there was a message later and it failed to process due to an exception. We then added some code to mark jobs as System Exception and have someone look into it , generally the client preffered the call center rep to see the exception . i converted the quick jobs to run directly and the slow jobs were saved in an initial state and processed by a background thread. For the slow jobs persistance was achieved loading initial state jobs at bootup. Note we still had system Exceptions due to BL issues but without MSMQ it is a bit simpler. MSMQ on CE looked ok provided the server is pushing to the device , though we did not do detailed disconnection testing ( there were some issues in the news groups with tuning retransmit times) . Polling server queues from the devices ran into issues when we get >500 devices on a server though this may have been registry related. Regards, Ben

    Sunday, November 19, 2006 12:49 PM
  • thank you, i tried the lastest code, but it did work well as the IP was out of multicasting range? could you please show the .config file as well.

    Wednesday, August 8, 2007 10:10 AM
  • Are you saying that you tried to run the UDP sample from the SDK but it failed because the multicasting address was invalid?

     

    Wednesday, August 8, 2007 10:51 AM
  •  

    hello,

     

    I have written a C code to receive a IPv6 multicast from a Remote-NDIS device usb connected to my PC having Windows XP.

    But the problem I am facing is that ,even though the device multicasts some data to my PC, I am not able to recv. Majority of the times when I restart my PC, it works fine and receives the data.

     

    I use ethereal to see whether is the device actually pumping data. It every time does show data being multicasted.

     

    I tried giving priority to interface,but still works adhocly sometimes...

     

    the steps I foloow are

     

    1. Create socket

    2. bind to in6addr_any and port

    3. setsocketopt to join multicast address, giving interface=0

    4. recv

     

    Please request for help

    Friday, November 30, 2007 3:32 AM