用于Socket通信的Consumer Publisher模型

时间:2013-10-04 10:16:20

标签: c# sockets events delegates

我正在为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
         }
    }

1 个答案:

答案 0 :(得分: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 );
    }
}