Delphi中如何发送广播消息

发布于 2024-09-14 15:13:21 字数 110 浏览 6 评论 0原文

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

我希望更新用户界面,这样任何计算机都会发送消息来更新其他计算机。 我可以使用UDPServer indy吗,如何使用? 谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三生路 2024-09-21 15:13:21

创建两个应用程序,一个代表发送者,另一个代表接收者。

发送者

在表单上放置一个 TIdUDPClient 和一个 TButton 组件。在按钮的 OnClick 处理程序上写入:

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

Receiver

在表单上放置一个 TIdUDPServer,为其定义相同的端口 (8090) 并将其添加到 OnUDPRead< /code> 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;

或者,在 Indy 的更高版本中:

procedure TfrmUDPServer.UDPServerUDPRead(AThread: TIdUDPListenerThread;
  const AData: TIdBytes; ABinding: TIdSocketHandle);
var
  Msg: String;
begin
  try
    {if you actually sent a string encoded in utf-8}
    Msg := TEncoding.UTF8.GetString(AData);
  except
  end;

  ShowMessage(Msg);
end;

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

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;

Or, in later versions of Indy:

procedure TfrmUDPServer.UDPServerUDPRead(AThread: TIdUDPListenerThread;
  const AData: TIdBytes; ABinding: TIdSocketHandle);
var
  Msg: String;
begin
  try
    {if you actually sent a string encoded in utf-8}
    Msg := TEncoding.UTF8.GetString(AData);
  except
  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.

夜还是长夜 2024-09-21 15:13:21

创建一个 TIdUDPServerTIdUDPClient 组件。两者都有 Broadcast 方法,可以完全满足您的需要。

Create a TIdUDPServer or TIdUDPClient component. Both have Broadcast methods that should do exactly what you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文