设置网络延时大小
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.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;