![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
var
ListenSocket: TSocket;
Addr: TSockAddr;
Len: Integer;
WsaData: TWSADATA;
Arg: DWORD;
Timeval: TTimeVal;
FdRead: TFDSet;
begin
if WSAStartup(MAKEWORD( 2 , 2 ), WsaData) <> 0 then
begin
WSACleanup;
Exit;
end;
ListenSocket : = WSASocket(AF_INET, SOCK_STREAM, 0 , nil, 0 , WSA_FLAG_OVERLAPPED);
if ListenSocket = INVALID_SOCKET then
begin
ShowMessage(SysErrorMessage(WSAGetLastError));
Exit;
end;
addr.sin_family : = AF_INET;
addr.sin_addr.s_addr : = inet_addr( ' 127.1.0.1 ' );
addr.sin_port : = htons( 1002 );
Len : = SizeOf(TSockAddr);
Arg : = 1 ;
if Socket_error = ioctlsocket(ListenSocket, FIONBIO, Arg) then
begin
closesocket(ListenSocket);
WSACleanup;
Exit;
end;
FD_ZERO(FdRead);
FD_SET(ListenSocket, FdRead);
Timeval.tv_sec : = 0 ;
Timeval.tv_usec : = 60 ;
connect(ListenSocket, PSockAddr(@addr), len);
if (select( 0 , 0 , @FdRead, 0 , @Timeval) <= 0 ) then
begin
closesocket(ListenSocket);
WSACleanup;
ShowMessage( ' 超时 ' );
Exit;
end;
Arg : = 0 ;
ioctlsocket(ListenSocket, FIONBIO, Arg);
WSACleanup;
end;
ListenSocket: TSocket;
Addr: TSockAddr;
Len: Integer;
WsaData: TWSADATA;
Arg: DWORD;
Timeval: TTimeVal;
FdRead: TFDSet;
begin
if WSAStartup(MAKEWORD( 2 , 2 ), WsaData) <> 0 then
begin
WSACleanup;
Exit;
end;
ListenSocket : = WSASocket(AF_INET, SOCK_STREAM, 0 , nil, 0 , WSA_FLAG_OVERLAPPED);
if ListenSocket = INVALID_SOCKET then
begin
ShowMessage(SysErrorMessage(WSAGetLastError));
Exit;
end;
addr.sin_family : = AF_INET;
addr.sin_addr.s_addr : = inet_addr( ' 127.1.0.1 ' );
addr.sin_port : = htons( 1002 );
Len : = SizeOf(TSockAddr);
Arg : = 1 ;
if Socket_error = ioctlsocket(ListenSocket, FIONBIO, Arg) then
begin
closesocket(ListenSocket);
WSACleanup;
Exit;
end;
FD_ZERO(FdRead);
FD_SET(ListenSocket, FdRead);
Timeval.tv_sec : = 0 ;
Timeval.tv_usec : = 60 ;
connect(ListenSocket, PSockAddr(@addr), len);
if (select( 0 , 0 , @FdRead, 0 , @Timeval) <= 0 ) then
begin
closesocket(ListenSocket);
WSACleanup;
ShowMessage( ' 超时 ' );
Exit;
end;
Arg : = 0 ;
ioctlsocket(ListenSocket, FIONBIO, Arg);
WSACleanup;
end;