TO sachow:
不行啊,我必须等到TimeOut到才说超时,但其实是正常发送的。下面是我测试的程序段:
请回复,谢谢。
unit Unit1;
interface
uses
Windows, Messages, SysUtils,winsock, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ScktComp;
type
TForm1 = class(TForm)
Button1: TButton;
ClientSocket1: TClientSocket;
ServerSocket1: TServerSocket;
Label1: TLabel;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
procedure ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
function RecvTimeOut(fd: Cardinal; sec: Word):Integer;
implementation
{$R *.DFM}
function RecvTimeOut(fd: Cardinal; sec: Word):Integer;
var
fs: TFDSet;
timeout: TTimeVal;
n: Integer;
begin
FD_ZERO(fs);
FD_SET(fd, fs);
timeout.tv_sec := sec;
timeout.tv_usec := 0;
n := Select(fd+1, @fs, nil, nil, @timeout);
Result := n;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
//clientsocket1.Host:='liu';
// clientsocket1.Active:=true;
clientsocket1.Socket.SendText('eryteyte');
if (RecvTimeOut(ClientSocket1.Socket.SocketHandle, 10)=0) then
begin
ShowMessage('发送超时!');
ClientSocket1.Close;
end;
end;
procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
label1.Caption:=clientsocket1.Socket.RemoteAddress;
end;
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
label2.Caption:=Socket.ReceiveText;
end;
end.