如何在Delphi中发送广播消息 [英] How can I send a broadcast message in Delphi

查看:163
本文介绍了如何在Delphi中发送广播消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的局域网中发送广播UDP消息,应用程序是客户端/服务器。



我希望更新用户界面,这样任何一台计算机都可以发送一个消息来更新他人。
我可以使用UDPServer indy,怎么用?
谢谢

解决方案

创建两个应用程序,一个表示发件人,另一个表示接收者。



发件人



删除一个 TIdUDPClient TButton 组件在您的表单上。在 OnClick 处理程序的按钮上写:

  procedure TfrmUDPClient.BroadcastClick (发件人:TObject); 
begin
UDPClient.Broadcast('Test',8090);
结束



接收者



删除一个 TIdUDPServer 在您的表单上定义相同的端口(8090),并将其添加到 OnUDPRead 处理程序:

 程序TfrmUDPServer.UDPServerUDPRead(发件人:TObject; AData:TStream; ABinding:TIdSocketHandle); 
var
DataStringStream:TStringStream;
Msg:String;
开始
DataStringStream:= TStringStream.Create('');
try
DataStringStream.CopyFrom(AData,AData.Size);
Msg:= DataStringStream.DataString;
finally
DataStringStream.Free;
结束
ShowMessage(Msg);
结束

要测试运行这两个应用程序并单击按钮。要使用两个或多个监听器进行测试,您必须使用另一台机器,也就是说,您不能在同一IP上运行多个监听器。


I want to send a broadcast UDP message in my LAN, the application is client/server.

I desire to update the user interface, this way any computer send a message to update the others. Can I use UDPServer indy, how to use ? Thanks

解决方案

Create two applications, one represents the sender and the other the receiver.

Sender

Drop a TIdUDPClient and a TButton component on your form. On the OnClick handler of the button write:

procedure TfrmUDPClient.BroadcastClick(Sender: TObject);
begin
  UDPClient.Broadcast('Test', 8090);
end;

Receiver

Drop a TIdUDPServer on your form, define the same port (8090) for it and add this to the OnUDPRead handler:

procedure TfrmUDPServer.UDPServerUDPRead(Sender: TObject; AData: TStream; ABinding: TIdSocketHandle);
var
  DataStringStream: TStringStream;
  Msg: String;
begin
  DataStringStream := TStringStream.Create('');
  try
    DataStringStream.CopyFrom(AData, AData.Size);
    Msg := DataStringStream.DataString;
  finally
    DataStringStream.Free;
  end;
  ShowMessage(Msg);
end;

To test run both applications and click on the button. To test with two or more "listeners" you have to use another machine, that is, you can't run multiple listeners on the same IP.

这篇关于如何在Delphi中发送广播消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆