TIdTCPServer的OnExecute事件不写代码,我只需要他的onConnect事件,为什么会报错呢?

yczyk 2005-07-16 10:45:04
请问,TIdTCPServer的OnExecute事件不写代码,我只需要他的onConnect事件,为什么会报错呢?
而且,如果我写个空事件在OnExecute中,为什么会出现资源占用100%呢?
...全文
312 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
wizardqi 2005-11-14
  • 打赏
  • 举报
回复
我建议楼主的信息读去放在OnExecute中,哪怕只有一次信息读取。
wizardqi 2005-11-14
  • 打赏
  • 举报
回复
这个简单,如果IdTCPServer组件不为OnExecute指定处理过程,就会得到EIdNoExecuteSpecified
异常,你可以查看以下两个代码片段:
function TIdTCPServer.DoExecute(AThread: TIdPeerThread): boolean;
var
i: integer;
LLine: string;
begin
if CommandHandlersEnabled and (CommandHandlers.Count > 0) then begin
Result := True;
if AThread.Connection.Connected then begin //APR: was While, but user can disable handlers
LLine := AThread.Connection.ReadLn;
// OLX sends blank lines during reset groups and expects no response. Not sure
// what the RFCs say about blank lines.
// I telnetted to some newsservers, and they dont respond to blank lines.
// This unit is core and not NNTP, but we should be consistent.
if Length(LLine) > 0 then begin
DoBeforeCommandHandler(AThread, LLine); try
for i := 0 to CommandHandlers.Count - 1 do begin
with CommandHandlers.Items[i] do begin
if Enabled then begin
if Check(LLine, AThread) then begin
Break;
end;
end;
end;
if i = CommandHandlers.Count - 1 then begin
DoOnNoCommandHandler(LLine, AThread);
end;
end;//for
finally DoAfterCommandHandler(AThread); end;
end;//if >''
end;
end else begin
Result := Assigned(OnExecute);//注意这里,当你没有为OnExecute指定处理是该函数就会返回False;
if Result then begin
OnExecute(AThread);
end;
end;
end;

procedure TIdPeerThread.Run;
begin
try
try
if not Connection.Server.DoExecute(Self) then begin//如果DoExecute返回False抛出异常
raise EIdNoExecuteSpecified.Create(RSNoExecuteSpecified);
end;
。。。。。。。
end;
将计就计123 2005-11-14
  • 打赏
  • 举报
回复
要只是想通知的话,不用维护一个连接
用UDP不是更方便吗
日总是我哥 2005-07-17
  • 打赏
  • 举报
回复
procedure TIdThread.Execute;
begin
try
try
BeforeExecute;
while not Terminated do begin
if Stopped then begin
DoStopped;
// It is possible that either in the DoStopped or from another thread,
// the thread is restarted, in which case we dont want to restop it.
if Stopped then begin // DONE: if terminated?
if Terminated then begin
Break;
end;
Suspended := True; // Thread manager will revive us
if Terminated then begin
Break;
end;
end;
end;

try
BeforeRun;
try
////////////////////////注意这里////////////////////
//一有连接线程创建时执行的事件 
while not Stopped do begin
Run;
end;
finally
AfterRun;
end;//tryf
finally
Cleanup;
end;

end;//while NOT Terminated
finally
AfterExecute;
end;
except
on E: Exception do begin
FTerminatingExceptionClass := E.ClassType;
FTerminatingException := E.Message;
DoException(E);
Terminate;
end;
end;//trye
end;


//这是被调用的方法
procedure TIdPeerThread.Run;
begin
try
try
//如果没有写DoExecute事件,则异常
if not Connection.Server.DoExecute(Self) then begin
raise EIdNoExecuteSpecified.Create(RSNoExecuteSpecified);
end;
except
// We handle these seperate as after these we expect .Connected to be false
// and caught below. Other exceptions are caught by the outer except.
on E: EIdSocketError do begin
Connection.Server.DoException(Self, E);
case E.LastError of
Id_WSAECONNABORTED // WSAECONNABORTED - Other side disconnected
, Id_WSAECONNRESET:
Connection.Disconnect;
end;
end;
on E: EIdClosedSocket do begin
// No need to disconnect - this error means we are already disconnected or never connected
Connection.Server.DoException(Self, E);
end;
on E: EIdConnClosedGracefully do begin
// No need to Disconnect, .Connected will detect a graceful close
Connection.Server.DoException(Self, E);
end;
end;
// If connection lost, stop thread
//如果没有断开连接,则一直执行...........所以占用资源100%
if not Connection.Connected then begin
Stop;
end;
// Master catch. Catch errors not known about above, or errors in Stop, etc.
// Must be a master catch to prevent thread from doing nothing.
except
on E: Exception do begin
Connection.Server.DoException(Self, E);
raise;
end;
end;
end;
cjf1009 2005-07-16
  • 打赏
  • 举报
回复
关注先
日总是我哥 2005-07-16
  • 打赏
  • 举报
回复
星哥,小的这厢有礼了:)
yczyk 2005-07-16
  • 打赏
  • 举报
回复
下面是部分代码:
for iCount := 0 to SendList.Count - 1 do
begin
try
try
tcpSendOut.Host := SendList[iCount];
tcpSendOut.Connect(DrinkTransOutTime);
except
MemoDaily.Lines.Add(format(sSendOutErr,[SendList[iCount]]));
end;
finally
tcpSendOut.Disconnect;
end;
end;
我有一个SendList列表,这里有待发的服务器列表,他们端口都相同
因为他们执行的是同一个事件,只是在不同的机器上执行
yczyk 2005-07-16
  • 打赏
  • 举报
回复
我的客户端一Connect上Server后,就马上断掉。
我这样做其实就是实现这样一个过程:
一个程序通知另一个程序做事,TIdTCPClient向TIdTCPServer发连接信息,发完后就断开,TIdTCPServer收到信息就做某事,我直接在Connection事件里做的,无须在OnExecute做任何事。可是接收到连接后就报错,你报错就报错呗,但结果造成我在Connection里的打印失败。真是晕了!
loveyzz 2005-07-16
  • 打赏
  • 举报
回复
说说清楚,哪一端报错?
你贴的那端代码,没有write那Server端哪里收得到数据?
lovend 2005-07-16
  • 打赏
  • 举报
回复
帮你顶
hqhhh 2005-07-16
  • 打赏
  • 举报
回复
关注:
UP

1,593

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 网络通信/分布式开发
社区管理员
  • 网络通信/分布式开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧