套接字通信的使用者发布者模型
本文关键字:发布者 模型 使用者 通信 套接字 | 更新日期: 2023-09-27 18:33:49
我正在为 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
}
}
可能有更好的方法可以做到这一点。但是,这是我根据我的理解所做的。这应该可以作为您要求的原型。
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 );
}
}