本文介绍了没有OnExecute事件的TCPserver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据需要向客户端发送/接收消息,而不是TCPserver的OnExecute事件。



发送/接收消息不是问题;我这样做:

 程序TFormMain.SendMessage(IP,Msg:string); 
var
I:整数;
begin
with TCPServer.Contexts.LockList do
try
for I:= 0 to Count-1 do
if TIdContext(Items [I])。Connection。 Socket.Binding.PeerIP = IP然后
begin
TIdContext(Items [I])。Connection.IOHandler.WriteBuffer(Msg [1],Length(Msg));
//和/或Read
Break;
结束
finally
TCPServer.Contexts.UnlockList;
结束
结束

注意1:如果我不使用OnExecute,程序会在客户端连接时引发异常。

注2:如果我使用OnExecute而不做任何事情,CPU使用率将转到%100

注3:我没有机会更改TCP客户端。 p>

那么该怎么办?

解决方案

使用OnExecute,如果你有无所事事,Sleep()一段时间,比如说10毫秒。每个连接都有自己的OnExecute处理程序,因此这只会影响每个连接。


I want to make a TCPserver and send/receive message to clients as needed, not OnExecute event of the TCPserver.

Send/receive message is not a problem; I do like that:

procedure TFormMain.SendMessage(IP, Msg: string);
var
  I: Integer;
begin
  with TCPServer.Contexts.LockList do
  try
    for I := 0 to Count-1 do
      if TIdContext(Items[I]).Connection.Socket.Binding.PeerIP = IP then
      begin
        TIdContext(Items[I]).Connection.IOHandler.WriteBuffer(Msg[1], Length(Msg));
        //  and/or Read 
        Break;
      end;
  finally
    TCPServer.Contexts.UnlockList;
  end;
end;

Note 1: If I don't use OnExecute, the program raise an exception when a client connects.
Note 2: If I use OnExecute without doing anything, the CPU usage goes to %100
Note 3: I don't have a chance to change the TCP clients.

So what should I do?

解决方案

Use OnExecute and if you have nothing to do, Sleep() for a period of time, say 10 milliseconds. Each connection has its own OnExecute handler so this will only affect each individual connection.

这篇关于没有OnExecute事件的TCPserver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 13:24