0

我正在为 UDP 套接字通信实现发布者消费者模型。在该应用程序中,消费者将在订阅事件时创建 UDP 套接字。因此每个消费者将有不同的 IP 地址和端口用于数据发送。订阅事件的过程是动态的,这意味着我们可以添加订阅者运行时间。在我的应用程序中,发布者在一段时间后调用事件。假设在我的应用程序中目前我有 5 个订阅者,所以在发布者调用该方法后,应该使用他们的 IP 和端口将这些数据发送给 5 个不同的订阅者。由于我有在订阅者中发送数据的单一方法,我不知道如何同时将数据发送到不同的 IP 和端口?如何维护事件处理程序的状态?请帮我。

我在这里添加示例代码。

public class Program
    {        
        public static EventHandler<ListEventArgs> DataSending;

        static void Main(string[] args)
        {

            Class1.CreateNewEvent();          
            Class1.CreateNewEvent();
            Class1.CreateNewEvent();
            Class1.CreateNewEvent();
            Class1.CreateNewEvent();
            OnDataSending("Hello2");
            Console.ReadLine();          

        }

        static void OnDataSending(object data)
        {
            if (DataSending != null)
            {
                List<string> ss = new List<string>();
                ss.Add(IPADDRESS);
                ss.Add(PORT);               
                DataSending(data,new ListEventArgs(ss));
            }
        }
    }




public static class Class1
    {      

         public static void CreateNewEvent()
         {
             Program.DataSending += new EventHandler<ListEventArgs>(newEvent);
         }

         static void newEvent(object o, ListEventArgs e)
         {
            //Code for socket creation and sending data
         }
    }
4

1 回答 1

0

可能有更好的方法来做到这一点。但是,这是我根据自己的理解所做的。这应该可以作为您要求的原型。

 public class Program
 {
    static List<Subscriber> GetSubscribers()
    {
        return
            new List<Subscriber>
            {
                new Subscriber{IP ="1.2.3.4",Port="1521"},
                new Subscriber{IP="2.2.2.2",Port="8080"},
                new Subscriber{IP="4.4.4.4",Port="1250"},
                new Subscriber{IP="6.6.6.6",Port="4123"}
            };

    }

    static void Main(string[] args)
    {

        Publisher p = new Publisher();
        List<Subscriber> subs = GetSubscribers();
        p.PublishEvent += new Publisher.PublishData(subs[0].PrintMessage);
        p.PublishEvent += new Publisher.PublishData(subs[1].PrintMessage);
        p.PublishEvent += new Publisher.PublishData(subs[2].PrintMessage);
        p.PublishEvent += new Publisher.PublishData(subs[3].PrintMessage);
        p.OnPublishData();
        Console.ReadKey();
    }

}

class Publisher
{

    public delegate void PublishData();
    public event PublishData PublishEvent;
    public void OnPublishData()
    {           
        if (PublishEvent != null)
            PublishEvent();
    }
}


class Subscriber
{
    public string IP { get; set; }
    public string Port { get; set; }

    public void PrintMessage()
    {
        Console.WriteLine("Data has arrived for IP " + IP + " Port " + Port);
    }
}

我认为更好的方法。

public class Program
{
    static List<Subscriber> GetSubscribers()
    {
        return
            new List<Subscriber>
            {
                new Subscriber{IP ="1.2.3.4",Port="1521"},
                new Subscriber{IP="2.2.2.2",Port="8080"},
                new Subscriber{IP="4.4.4.4",Port="1250"},
                new Subscriber{IP="6.6.6.6",Port="4123"}
            };

    }

    static void Main(string[] args)
    {

        PublishDataEventArgs e = new PublishDataEventArgs();
        e.Message = "Hello from Subscriber";

        Publisher p = new Publisher();
        List<Subscriber> subs = GetSubscribers();
        p.PublishData += new EventHandler<PublishDataEventArgs>(subs[0].PrintMessage);
        p.PublishData += new EventHandler<PublishDataEventArgs>(subs[1].PrintMessage);
        p.PublishData += new EventHandler<PublishDataEventArgs>(subs[2].PrintMessage);
        p.PublishData += new EventHandler<PublishDataEventArgs>(subs[3].PrintMessage);
        p.OnPublishData(e);
        Console.ReadKey();
    }

}

class Publisher
{  
    public void OnPublishData(PublishDataEventArgs p)
    {
        EventHandler<PublishDataEventArgs> _PublishData = PublishData;
        if (_PublishData != null)
            _PublishData(this, p);
    }


    public event EventHandler<PublishDataEventArgs> PublishData;
}

class PublishDataEventArgs:EventArgs
{
    public string Message{get;set;}
}


class Subscriber
{
    public string IP { get; set; }
    public string Port { get; set; }

    public void PrintMessage(object sender,PublishDataEventArgs e)
    {
        Console.WriteLine("Data has arrived for IP " + IP + " Port " + Port + " message " + e.Message );
    }
}
于 2013-11-14T21:11:35.580 回答