怎样实现socket编程的超时控制?(100分)

  • 主题发起人 主题发起人 heimukai
  • 开始时间 开始时间
H

heimukai

Unregistered / Unconfirmed
GUEST, unregistred user!
我用winsock单元里面的SOCKET API进行SOCKET通讯。做的是CLIENT端的程序,听说recv和send是阻塞似的,所以不知道怎样实现TIMEOUT的控制。
问过做C的朋友,说是他到了设定的时间如果还没有返回执行结果就用跳转函数,不知道DELPHI是否也有这样的函数?应该不会指的是GOTO吧?
 
When ClientType is ctBlocking, use a TWinSocketStream object for reading and writing.

procedure TMyClientThread.Execute;
var
TheStream: TWinSocketStream;
buffer: string;
begin
{ create a TWinSocketStream for reading and writing }
TheStream := TWinSocketStream.Create(ClientSocket1.Socket, 60000);
try
{ fetch and process commands until the connection or thread is terminated }
while (not Terminated) and (ClientSocket1.Active) do
begin
try
GetNextRequest(buffer); { GetNextRequest must be a thread-safe method }

{ write the request to the server }
TheStream.Write(buffer, Length(buffer) + 1);
{ continue the communication (eg read a response from the server) }
...
except
ifnot(ExceptObject is EAbort) then
Synchronize(HandleThreadException); { you must write HandleThreadException }
end;
end;
finally
TheStream.Free;
end;
end;
 
阻塞编程,用INDY9的 TCPCLIENT就很好 用呀,有超时参数控制的,DELPHI7 DEMOS/INDY下有 DEMO
 
我只想用API,因为我要写在ACTIVE SERVER OBJECT控件里面,不方便用控件,况且编码已经差不多完成了,只剩下超时的控制。
 
那你可以看INDY的源代码,他也是用API,然后一层层封装的
 
后退
顶部