创建服务器
class BasicDDE:DdeServer { public BasicDDE(string ServiceName):base(ServiceName) { } }
在主类中
private void MakeServer() { BasicDDE server = new BasicDDE("BasicDDE"); server.Register(); }
服务器的创建关键就是服务器名称。
创建客户端
private void MakeClient(string ServiceName,string topic) {try { client = new DdeClient(ServiceName, topic,this); client.Connect(); } catch (Exception) {
client = null; MessageBox.Show("DDE Client Creation failed"); } }
关键是对应的服务器名称和topic名称。
HotLink模式
类似于长连接,服务器可以主动推送相关信息给客户端。
这里的例子服务器开始定时器,每个一秒调用Advise函数
Advise("*", "*"); //向所有topic和item发送消息
发送的时候,会调用
protected virtual byte[] OnAdvise(string topic, string item, int format);
发送具体信息。
服务器全部代码
class BasicDDE:DdeServer { public System.Timers.Timer timer = new System.Timers.Timer(); public BasicDDE(string ServiceName):base(ServiceName) { timer.Elapsed += timer_Elapsed; timer.Interval = 1000; timer.SynchronizingObject = this.Context; } void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { Advise("*", "*"); } protected override byte[] OnAdvise(string topic, string item, int format) { return System.Text.Encoding.UTF8.GetBytes("Server Advise:" + DateTime.Now.ToString()); } }