如何在控制台应用程序中使用 TIdUDPServer?
Indy UDP 服务器组件似乎依赖于 Windows 消息,因为我看到只有当鼠标光标移动到包含服务器组件的表单上时才会出现新消息。
我使用以下代码在运行时创建了该组件:
private
{ Private declarations }
Srv: TIdUDPServer;
...
procedure TForm1.FormCreate(Sender: TObject);
begin
Srv := TIdUDPServer.Create(Self);
Srv.DefaultPort := 9009;
Srv.OnUDPRead := Self.udpServerUDPRead;
Srv.Active := True;
end;
我更喜欢控制台或服务应用程序。我是否需要一个(不可见的)窗口句柄+一些窗口消息队列代码来使该组件正常工作?
更新:一些(非官方)文档说:
TIdUDPServer 当活动时创建一个监听线程来监听 入站 UDP 数据包。 ...当 ThreadedEvent 为 false 时,OnUDPRead 事件将在主程序线程的上下文中触发。什么时候 ThreadedEvent 为 true,则在以下上下文中触发 OnUDPRead 事件 侦听器线程。
The Indy UDP server component seems to depend on Windows messages, as I see new messages appear only when the mouse cursor moves over the form which contains the server component.
I created the component at run time using this code:
private
{ Private declarations }
Srv: TIdUDPServer;
...
procedure TForm1.FormCreate(Sender: TObject);
begin
Srv := TIdUDPServer.Create(Self);
Srv.DefaultPort := 9009;
Srv.OnUDPRead := Self.udpServerUDPRead;
Srv.Active := True;
end;
I would prefer a console or service application. Do I need an (invisible) window handle + some windows message queue code which to get this component working?
Update: some (inofficial) documentation says:
TIdUDPServer when active creates a listening thread to listen for
inbound UDP packets. ... When ThreadedEvent is false, the OnUDPRead
event will be fired in the context of the main program thread. When
ThreadedEvent is true, the OnUDPRead event is fired in the context of
the listener thread.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用线程,则不需要 Windows 消息或表单。只需将 ThreadedEvent 设置为 true 并在线程中处理,请记住该事件是在不同的线程上传入的,因此您必须处理线程“同步”。我通过仅使用处理来自其自己的内部队列(我的消息和我的队列,而不是 Windows 消息或队列)的消息的线程来执行此操作,因此事件处理程序将带有接收到的数据的消息放入线程的队列中,以便在“目标”线程的上下文,而不是 Indy 侦听器线程。
If you use threading then you don't need Windows messages nor forms. Just set ThreadedEvent to true and handle in a thread, remembering that the event is coming in on a different thread so you will have to handle thread "synchronisation". I do this by only using threads that process messages off their own internal queue (my messages and my queue, not Windows messages or queues), so the event handler puts a message with the received data onto the thread's queue, to be processed in the context of the "destination" thread, not the Indy listener thread.