none
Multicasting Applications using the UDP Transport RRS feed

  • Question

  • Hi all,

    I follow the link below to create Multicasting Applications using the UDP Transport, but my receiver failed to get message from the sender. Please help me have a look.

    https://msdn.microsoft.com/en-us/library/hh556229(v=vs.110).aspx

    Here is my code:

    Sender:

    namespace GettingStartedUDPTransportSender
    {
        class Program
        {
            [ServiceContract]
            interface IStockTicker
            {
                [OperationContract(IsOneWay = true)]
                void SendStockInfo(StockInfo[] stockInfo);
            }
            [DataContract]
            class StockInfo
            {
                [DataMember]
                public string Symbol;
                [DataMember]
                public float Price;
                public StockInfo(string symbol, float price)
                {
                    this.Symbol = symbol;
                    this.Price = price;
                }
            }
            static void Main(string[] args)
            {
                string baseAddress = "soap.udp://224.0.0.1:40000";
                UdpBinding myBinding = new UdpBinding();
                ChannelFactory<IStockTicker> factory  = new ChannelFactory<IStockTicker>(myBinding, new EndpointAddress(baseAddress));
                IStockTicker proxy = factory.CreateChannel();
                while (true)
                {
                    // This will continue to mulicast stock information 
                    proxy.SendStockInfo(GetStockInfo());
                    Console.WriteLine(String.Format("sent stock info at {0}", DateTime.Now));
                    // Wait for one second before sending another update
                    System.Threading.Thread.Sleep(new TimeSpan(0, 0, 1));
    
                }
                Console.ReadLine();
            }
             static StockInfo[] GetStockInfo()
            { 
                StockInfo[] stockInfo=new StockInfo[2];
                stockInfo[0] = new StockInfo("S1", 1);
                stockInfo[1] = new StockInfo("S2", 2);
                return stockInfo;
            }
        }
    }

    Receiver:

    namespace GettingStartedUDPTransport
    {
        [ServiceContract]
        public interface IStockTicker
        {
            [OperationContract(IsOneWay = true)]
            void SendStockInfo(StockInfo[] stockInfo);
        }
        public class StockTickerService : IStockTicker
        {
            public void SendStockInfo(StockInfo[] stockInfo)
            {
                foreach (StockInfo s in stockInfo)
                {
                    Console.WriteLine(string.Format("Stock Name: {0}, Price: ${1}", s.Symbol, s.Price));
                }
            }
        }
        [DataContract]
        public class StockInfo
        {
            [DataMember]
            public string Symbol;
            [DataMember]
            public float Price;
            public StockInfo(string symbol, float price)
            {
                this.Symbol = symbol;
                this.Price = price;
            }
        }
        class Program
        {
    
            static void Main(string[] args)
            {
                string serviceAddress = "soap.udp://224.0.0.1:40000";
                UdpBinding myBinding = new UdpBinding();
                ServiceHost host = new ServiceHost(typeof(StockTickerService), new Uri(serviceAddress));
                host.AddServiceEndpoint(typeof(IStockTicker), myBinding, "");
                host.Open();
                Console.WriteLine("Start receiving stock information");
                Console.WriteLine("{0} is up and running with following endpoint(s)-", host.Description.ServiceType);
                Console.ReadLine();
            }
        }
    }

    Any help would be appreciated.


    Help each other

    Wednesday, December 16, 2015 7:50 AM

Answers

  • Hi Tony,
    According to this case, I create a solution to test, it works fine.

    Perhaps you haven't noticed the comment statement in document,

    the document said that we need to shared contracts between the client and the service.

    So, we need to generate a class project. Then add the reference to client and service.

    The result as shown below:

    I hope that will be helpful to you.

    Best Regards,

    Wanjun Dong


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    • Marked as answer by Tony---- Thursday, December 17, 2015 9:39 AM
    Thursday, December 17, 2015 8:38 AM
    Moderator