这是我修改的TCustomWinSocket.DoOpen过程,基本原理乃是以前自网上获得,
我针对Delphi的控件特性作了修改,如有问题,概不负责 ^_^
procedure TCustomWinSocket.DoOpen;
var
val:integer;
rc:integer;
ulB:U_LONG;
Time:timeval;
FdSet:TFDset;
TimeOut:integer;
begin
do
SetASyncStyles;
Event(Self, seConnecting);
if FAsyncStyles=[] then
begin
Timeout:=8000;
rc:=0;
ulB := 1;
ioctlsocket(FSocket, FIONBIO, ulB);
if WinSock.connect(FSocket, FAddr, SizeOf(FAddr))=SOCKET_ERROR then
begin
if (WSAGetLastError() = WSAEWOULDBLOCK) then
begin
FD_ZERO(FdSet);
FD_SET(FSocket, FdSet);
Time.tv_sec := timeout div 1000;
Time.tv_usec := (timeout mod 1000) * 1000;
rc := select(0, nil, @FdSet, nil, @Time);
end;
end;
ulB := 0;
ioctlsocket(FSocket, FIONBIO, ulB);
if rc=0 then
begin
WSASetLastError(10065);
CheckSocketResult(SOCKET_ERROR,'connect');
end;
end
else
CheckSocketResult(WinSock.connect(FSocket, FAddr, SizeOf(FAddr)),'connect');
FLookupState := lsIdle;
if not (asConnect in FAsyncStyles) then
begin
FConnected := FSocket <> INVALID_SOCKET;
Event(Self, seConnect);
end;
end;