首页 > 代码库 > IOCP扩展方法AcceptEx, DisconnectEx, GetAcceptExSockaddr用法示例
IOCP扩展方法AcceptEx, DisconnectEx, GetAcceptExSockaddr用法示例
这篇文章记录了我刚接触IOCP模型时的理解,对于初学者,应该算不错的调试程序,仅有一个400多行代码的dpr文件,可以直接用WriteLn输出信息,前提是会用delphi建立Console Application,当然你也可以很容易的就改成了C控制台程序或是其它语言。附加代码中有详细的注释,如果你已有IOCP经验,那么请不用在此浪费时间,这个示例不适合你。示例仅展示了IOCP中AcceptEx, DisconnectEx, GetAcceptExSockaddr等的用法。在文章最后有推荐的两个连接,对于想深入研究IOCP与TCP的可以去看看,可人感觉值得去看。文中错误或是不合理的地方敬请原谅,如果方便还请指出,深表感谢!
刚接触IOCP时,因为要弄清里面的复杂关系,最直接的方法当然就是断点调试与信息输出了,所以我选用了控制台程序。具体的原理在文章末尾的第一个连接中讲得很详细,扩展的方法可以到MSDN上查。示例中仅用了一个工作线程和10个连接,个人觉得这样方便调试与查看输出信息,省去了多线程与数据池的干扰,当然在实际应用中会涉及线程池、消息队列、内存管理等等,不在这个示例展示范围。下面我只记录了我碰到的问题:
问题一:AcceptEx成功,但有客户端连接时,GetQueuedCompletionStatus却不返回,后来把ListenSocket用CreateIoCompletionPort绑定到完在端口之后问题不再出现,因为之前用WSAAccept,就没有把ListenSocket绑定。
问题二:10014错误,在一开始时,gOverls用了array[0..ACCEPT_COUNT - 1] of TIOCPRecord,用这样的类型时,AcceptEx是成功的,也能接收几个连接,但是测试的连接不完全能连上,后来改用array[0..ACCEPT_COUNT - 1] of PIOCPRecord,用New分配内存时,测试连接能全部连上,分析原因,可能是栈内存与堆内存的区别。另外一次出现10014是把TWSABUF中的u_long类型改为int64类型后出现这个错。
问题三:首次GetQueuedCompletionStatus的完键不是预期的那个,即opAccept时的那个完成键并不是想要的那个,连接后正常。我一开始AcceptEx时就已经把每一个待接收的Socket绑定到了完成端口,因为连接非法时不想在DisconnectEx那里判断。
问题四:要说一下TAcceptEx与TGetAcceptExSockaddrs中的dwReceiveDataLength参数,这个初始值如果不是0,那么当IOCP接到一个连接时并不立即返回,而是收到客户端的数据时才返回。这会导致DOS攻击的大量连接耗尽预设等待的Socket连接,即AcceptEx投递的连接资源,如果设置为0,那么有连接时GetQueuedCompletionStatus就回立即返回,至于是否非法那就仁者见人智者见智了,比如心跳包的检测。使用AcceptEx时,一定要保证IOCP中有空闲的Socket接收客户端。
问题五:ERROR_IO_PENDING(997),当WSAGetLastError返回这个值时表示已经进入处理中,但并没有处理完。
下图是我写完这个示例后对IOCP中那几个API关系的理解,注意图中的箭头方向,PostQueuedCompletionStatus,AcceptEx,WSARecv, WSASend, DisconnectEx都是向IOCP内部投递数据,投递完后立即返回,这并不意味着投递的数据就会立即被处理,可以理解为进入一个等待处理队列,这几个方法可以投递多个数据不阻塞。PostQueuedCompletionStatus一般用于结束工作线程时使用,也就是GetQueuedCompletionStatus会得到PostQueuedCompletionStatus的状态信息。所有IOCP内部处理完的数据都会通过GetQueuedCompletionStatus这个方法反回,比如收到客户端请求后,发送数据到客户端后。这是一个阻塞的方法,当然可以设置超时。每一个工作线程也就通过GetQueuedCompletionStatus这个方法来处理消息,具体可以看后面的附加代码。
附加IOCPsever.dpr与WinSock2.pas:
1 program IOCPServer; 2 3 {$APPTYPE CONSOLE} 4 5 uses 6 SysUtils, windows, 7 WinSock2 in ‘winsock2.pas‘; 8 9 const 10 WINSOCK_VERSION = $0202; 11 LISTEN_PORT = 6553; //监听端口 12 MAX_BUFFSIZE = 4096; //数据buffer 13 ACCEPT_COUNT = 10; //接收连接数 14 SHUTDOWN_FLAG = $FFFFFFFF; //退出标记 15 16 SO_UPDATE_ACCEPT_CONTEXT = $700B; 17 TF_REUSE_SOCKET = $02; 18 IOC_IN =$80000000; 19 IOC_VENDOR =$18000000; 20 IOC_OUT =$40000000; 21 SIO_KEEPALIVE_VALS =IOC_IN or IOC_VENDOR or 4; 22 23 WSAID_GETACCEPTEXSOCKADDRS: System.TGuid = (D1:$b5367df2;D2:$cbac;D3:$11cf;D4:($95,$ca,$00,$80,$5f,$48,$a1,$92)); 24 WSAID_ACCEPTEX: System.TGuid = (D1:$b5367df1;D2:$cbac;D3:$11cf;D4:($95,$ca,$00,$80,$5f,$48,$a1,$92)); 25 WSAID_CONNECTEX: System.TGuid = (D1:$25a207b9;D2:$ddf3;D3:$4660;D4:($8e,$e9,$76,$e5,$8c,$74,$06,$3e)); 26 WSAID_DISCONNECTEX: System.TGuid = (D1:$7fda2e11;D2:$8630;D3:$436f;D4:($a0,$31,$f5,$36,$a6,$ee,$c1,$57)); 27 28 type 29 TAcceptEx = function(sListenSocket, sAcceptSocket: TSocket; 30 lpOutputBuffer: Pointer; dwReceiveDataLength, dwLocalAddressLength, 31 dwRemoteAddressLength: LongWord; var lpdwBytesReceived: LongWord; 32 lpOverlapped: POverlapped): BOOL; stdcall; 33 34 TGetAcceptExSockaddrs = procedure(lpOutputBuffer: Pointer; 35 dwReceiveDataLength, dwLocalAddressLength, dwRemoteAddressLength: DWORD; 36 var LocalSockaddr: PSockAddr; var LocalSockaddrLength: Integer; 37 var RemoteSockaddr: PSockAddr; var RemoteSockaddrLength: Integer);stdcall; 38 39 TConnectEx = function(const s : TSocket; const name: PSOCKADDR; const 40 namelen: Integer; lpSendBuffer : Pointer; dwSendDataLength : DWORD; var 41 lpdwBytesSent : LongWord; lpOverlapped : POverlapped): BOOL; stdcall; 42 43 TDisconnectEx = function(const hSocket : TSocket; lpOverlapped: POverlapped; 44 const dwFlags : LongWord; const dwReserved : LongWord) : BOOL; stdcall; 45 46 PTCPKeepAlive = ^TTCPKeepAlive; 47 TTCPKeepAlive = packed record 48 KeepOn: integer; 49 KeepTime: integer; 50 KeepInterval: integer; 51 end; 52 53 TSockOperate = (soAccept, soDisconnect, soRead, soWrite); 54 PIOCPRecord = ^TIOCPRecord; 55 TIOCPRecord = packed record 56 Ovelpd: TOverlapped; 57 WsaBuf: TWsaBuf; 58 Buffer: array [0..MAX_BUFFSIZE - 1] of AnsiChar; 59 operate: TSockOperate; 60 sock: TSocket; 61 end; 62 63 var 64 gListen: TSocket; 65 gOverls: array[0..ACCEPT_COUNT - 1] of PIOCPRecord; 66 gIOCP: THandle = 0; 67 gWorker: THandle; 68 gFunAcceptEx: TAcceptEx = nil; 69 gFunGetAcceptEx: TGetAcceptExSockaddrs = nil; 70 gFunConnectEx: TConnectEx = nil; 71 gFunDisconnectEx: TDisconnectEx = nil; 72 73 //加载扩展函数 74 function LoadFunEx(ASock: TSocket): Boolean; 75 var 76 tag: LongWord; 77 begin 78 Result := False; 79 if ASock = INVALID_SOCKET then Exit; 80 try 81 tag := 0; 82 if SOCKET_ERROR = WSAIoctl(ASock, SIO_GET_EXTENSION_FUNCTION_POINTER, 83 @WSAID_ACCEPTEX, SizeOf(WSAID_ACCEPTEX), 84 @@gFunAcceptEx, SizeOf(Pointer), @tag, nil, nil)then 85 begin 86 gFunAcceptEx := nil; 87 Exit; 88 end; 89 90 tag := 0; 91 if SOCKET_ERROR = WSAIoctl(ASock, SIO_GET_EXTENSION_FUNCTION_POINTER, 92 @WSAID_GETACCEPTEXSOCKADDRS, SizeOf(WSAID_GETACCEPTEXSOCKADDRS), 93 @@gFunGetAcceptEx, SizeOf(Pointer), @tag, nil, nil)then 94 begin 95 gFunGetAcceptEx := nil; 96 Exit; 97 end; 98 99 if SOCKET_ERROR = WSAIoctl(ASock, SIO_GET_EXTENSION_FUNCTION_POINTER,100 @WSAID_CONNECTEX, SizeOf(WSAID_CONNECTEX),101 @@gFunConnectEx, SizeOf(Pointer), @tag, nil, nil)then102 begin103 gFunConnectEx := nil;104 Exit;105 end;106 107 if SOCKET_ERROR = WSAIoctl(ASock, SIO_GET_EXTENSION_FUNCTION_POINTER,108 @WSAID_DISCONNECTEX, SizeOf(WSAID_DISCONNECTEX),109 @@gFunDisconnectEx, SizeOf(Pointer), @tag, nil, nil)then110 begin111 gFunDisconnectEx := nil;112 Exit;113 end; 114 Result := True;115 except116 end;117 end;118 119 //判断socket是否有效120 function IsSocketAlive(ASock: TSocket): Boolean;121 var122 tmp: LongWord;123 begin124 Result := WinSock2.send(ASock, tmp, 0, 0) <> -1;125 end;126 127 //设置心跳128 procedure SetKeepLive(ASock: TSocket);129 var130 opt: Integer;131 dwFlags: DWORD;132 inKeepAlive, OutKeepAlive: TTCPKeepAlive;133 begin134 if ASock <> INVALID_SOCKET then135 begin136 opt := 1;137 inKeepAlive.KeepOn := 1;138 inKeepAlive.KeepTime := 5000;139 inKeepAlive.KeepInterval := 1;140 141 if (SetSockOpt(ASock, SOL_SOCKET, SO_KEEPALIVE, @opt, SizeOf(opt)) = SOCKET_ERROR)142 or (WSAIoctl(ASock, SIO_KEEPALIVE_VALS, @inKeepAlive,SizeOf(TTCPKeepAlive),143 @outKeepAlive, SizeOf(TTCPKeepAlive), @dwFlags, nil, nil) = SOCKET_ERROR) then144 WriteLn(‘设置心跳错误 socket: ‘ + Inttostr(ASock) + ‘Error: ‘ + Inttostr(WSAGetLastError) );145 end;146 end;147 148 //关闭socket149 procedure FunCloseSocket(ASock: TSocket);150 var151 Linger: TLinger;152 begin153 if ASock <> INVALID_SOCKET then154 begin155 FillChar(Linger, SizeOf(TLinger), 0);156 setsockopt(ASock, SOL_SOCKET, SO_LINGER, @Linger, Sizeof(Linger));157 closesocket(ASock);158 end;159 end;160 161 procedure ResetData(PData: PIOCPRecord);162 var163 sock: TSocket;164 begin165 if Assigned(PData) then166 begin167 sock := PData.sock;168 FillChar(PData^, SizeOf(TIOCPRecord), 0);169 PData^.WsaBuf.len := MAX_BUFFSIZE;170 PData^.WsaBuf.buf := @(PData^.Buffer[0]);171 PData.sock := sock;172 end;173 end;174 175 //投递一个接收socket176 function PostAccept(PAccept: PIOCPRecord): Boolean;177 var178 dwBytes: LongWord;179 begin180 Result := False;181 if Assigned(PAccept) and (PAccept^.sock <> INVALID_SOCKET) then182 begin183 ResetData(PAccept);184 PAccept^.operate := soAccept;185 dwBytes := 0;186 Result := gFunAcceptEx(gListen, PAccept^.sock, PAccept^.Wsabuf.buf, 0,187 SizeOf(TSockAddrIn) + 16, SizeOf(TSockAddrIn) + 16,188 dwBytes, @(PAccept^.Ovelpd))189 or (ERROR_IO_PENDING = WSAGetLastError);190 end;191 end;192 193 //投递一个断开socket194 function PostDisconnect(PData: PIOCPRecord): Boolean;195 begin196 Result := False;197 if Assigned(PData) and (PData^.sock <> INVALID_SOCKET) then198 begin199 ResetData(PData);200 PData^.operate := soDisconnect;201 WinSock2.shutdown(PData^.sock, SD_BOTH);202 Result := gFunDisconnectEx(PData^.sock, @(PData^.Ovelpd), TF_REUSE_SOCKET, 0)203 or (ERROR_IO_PENDING = WSAGetLastError);204 if Result then205 WriteLn(‘客户端断开:socket ‘ + Inttostr(PData^.sock))206 else207 WriteLn(‘客户端断开错误:socket ‘ + Inttostr(PData^.sock) + ‘ WSAError: ‘ + Inttostr(WSAGetLastError));208 end;209 end;210 211 //投递接收数据缓冲212 function PostRecv(PData: PIOCPRecord): Boolean;213 var214 dwBuff, dwRecv: LongWord;215 begin216 Result := False;217 if Assigned(PData) and (PData^.sock <> INVALID_SOCKET) then218 begin219 ResetData(PData);220 PData^.operate := soRead;221 dwBuff := 0;222 dwRecv := 0;223 Result := (WSARecv(PData^.sock, @(PData^.WsaBuf), 1, @dwBuff, @dwRecv, @(PData^.Ovelpd), nil) <> SOCKET_ERROR)224 or (ERROR_IO_PENDING = WSAGetLastError);225 end;226 end;227 228 procedure WriteError(AInfo: String);229 begin230 WriteLn(AInfo + ‘ WSAError: ‘ + Inttostr(WSAGetLastError));231 end;232 233 //工作线程234 function WorkProc(PValue: Pointer): Integer;stdcall;235 var236 data: PIOCPRecord;237 workCount, dwSend, dwFlags: Cardinal;238 sock: TSocket;239 str: AnsiString;240 remoteAddr, localAddr: PSockAddrin;241 remoteLen, localLen, opt: Integer;242 begin243 Result := 0;244 while true do245 begin246 data := nil;247 workCount := 0;248 sock := 0;249 if not GetQueuedCompletionStatus(gIOCP, workCount, Cardinal(sock), POVerlapped(data), INFINITE) then250 begin //有可能是客服端断开251 if Assigned(data) then252 PostDisconnect(data);253 Continue;254 end;255 WriteLn(‘Worker GetQueued socket:‘ + inttostr(sock));256 if Cardinal(data) = SHUTDOWN_FLAG then Break; // 有退出257 if not Assigned(data) then continue;258 259 case data^.operate of260 soAccept:261 begin262 gFunGetAcceptEx(Data^.WsaBuf.buf, 0, SizeOf(TSockAddrIn) + 16, SizeOf(TSockAddrIn) + 16,263 PSockAddr(localAddr), localLen, PSockAddr(remoteAddr), remoteLen);264 WriteLn(‘接入IP: ‘ + inet_ntoa(remoteAddr^.sin_addr) + ‘ Port: ‘ + Inttostr(ntohs(remoteAddr^.sin_port)));265 SetSockOpt(data^.sock, SOL_SOCKET, SO_UPDATE_ACCEPT_CONTEXT, PAnsiChar(@(gListen)), SizeOf(gListen));266 SetKeepLive(data^.sock);267 268 if PostRecv(data) then269 WriteLn(‘Accept 数据 socket: ‘ + Inttostr(data^.sock))270 else271 WriteError(‘Accept 错误 socket: ‘ + inttostr(data^.sock));272 end;273 soDisconnect:274 begin275 if PostAccept(data) then276 WriteLn(‘投递Acceptex socket: ‘ + Inttostr(data^.sock))277 else278 WriteError(‘Disconnectex投递错误 socket: ‘ + Inttostr(data^.sock));279 end;280 soWrite:281 begin282 if workCount = 0 then283 PostDisconnect(data)284 else begin285 data^.WsaBuf.len := data^.WsaBuf.len - workCount;286 if data^.WsaBuf.len <= 0 then287 begin288 WriteLn(‘服务器回复数据完成 socket: ‘ + inttostr(data^.sock));289 PostRecv(data);290 continue;291 end;292 dwSend := 0;293 dwFlags := 0;294 FillChar(data^.Ovelpd, Sizeof(data^.Ovelpd), 0);295 if (WSASend(data^.sock, @(data^.WsaBuf), 1, @dwSend, dwFlags, @data^.Ovelpd, nil)<>SOCKET_ERROR)296 or (ERROR_IO_PENDING = WSAGetLastError) then297 WriteLn(‘发送数据 socket: ‘ + Inttostr(data^.sock))298 else begin299 WriteError(‘发送数据错误 socket: ‘ + Inttostr(data^.sock));300 end;301 302 if dwSend = SOCKET_ERROR then//客户端断开303 PostDisconnect(data)304 end;305 end;306 soRead:307 begin308 if workCount = 0 then309 PostDisconnect(data)310 else begin311 sock := data^.sock;312 SetLength(str, workCount);313 Move(data^.Buffer[0], str[1], workCount);314 WriteLn(‘接收数据 socket: ‘ + Inttostr(sock) + ‘ str: ‘ + str);315 str := ‘服务器回复:‘ + str;316 317 dwSend := 0;318 dwFlags := 0;319 FillChar(data^, SizeOf(TIOCPRecord), 0);320 data^.WsaBuf.len := Length(str);321 data^.WsaBuf.buf := @(data^.Buffer[0]);322 data^.operate := soWrite;323 data^.sock := sock;324 Move(str[1], data^.Buffer[0], data^.WsaBuf.len);325 if not ( (WSASend(sock, @(data^.WsaBuf), 1, @dwSend, dwFlags, @(data^.Ovelpd), nil) <> SOCKET_ERROR)326 or (WSAGetlastError = ERROR_IO_PENDING) ) then327 begin328 WriteLn(‘服务器回复数据失败 socket: ‘ + Inttostr(sock));329 PostRecv(data);330 end331 else332 WriteLn(‘服务器回复数据 socket: ‘ + Inttostr(sock));333 end;334 end;335 end; // end of case336 end;// end of while337 end;338 339 var340 threadID: Cardinal;341 i: Integer;342 need: Boolean;343 WSData: TWSAData;344 addr: TSockAddr;345 console: array[0..255] of AnsiChar;346 label exitLabel;347 begin348 349 try350 need := True;351 352 WriteLn(‘初始化网络环境‘);353 if WSAStartup(WINSOCK_VERSION, WSData) <> 0 then goto exitLabel;354 355 WriteLn(‘创建完成端口‘);356 gIOCP := CreateIoCompletionPort(INVALID_HANDLE_VALUE, 0, 0, 0);357 if gIOCP = 0 then goto exitLabel;358 359 WriteLn(‘创建监听Socket‘);360 gListen := WSASocket(PF_INET, SOCK_STREAM, 0, nil, 0, WSA_FLAG_OVERLAPPED);361 if gListen = INVALID_SOCKET then goto exitLabel;362 363 WriteLn(‘加载扩展函数‘);364 if not LoadFunEx(gListen) then goto exitLabel;365 366 WriteLn(‘邦定监听Socket 与IOCP [‘ + inttostr(gListen) + ‘][‘ + inttostr(gIOCP) + ‘]‘);367 if CreateIoCompletionPort(gListen, gIOCP, gListen, 0) = 0 then goto exitLabel;368 369 WriteLn(‘邦定监听Socket 与IP‘);370 FillChar(Addr, SizeOf(TSockAddrIn), 0);371 Addr.sin_family := AF_INET;372 Addr.sin_port := htons(LISTEN_PORT);373 Addr.sin_addr.S_addr := htonl(INADDR_ANY);374 //Addr.sin_addr.S_addr := inet_addr(‘0.0.0.0‘);375 if WinSock2.bind(gListen, @Addr, SizeOf(TSockAddr)) <> 0 then goto exitLabel;376 377 WriteLn(‘开始监听Socket‘);378 if WinSock2.listen(gListen, SOMAXCONN) <> 0 then goto exitLabel;379 380 WriteLn(‘投递接收Socket‘);381 FillChar(gOverls[0], SizeOf(gOverls), 0);382 for i := 0 to ACCEPT_COUNT - 1 do383 begin384 New(gOverls[i]);385 gOverls[i]^.sock := WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, nil, 0, WSA_FLAG_OVERLAPPED);386 if gOverls[i]^.sock = INVALID_SOCKET then goto exitLabel;387 if CreateIoCompletionPort(gOverls[i]^.sock, gIOCP, gOverls[i]^.sock, 0) = 0 then goto exitLabel;388 if PostAccept(gOverls[i]) then389 WriteLn(‘投递Socket:‘ + Inttostr(gOverls[i]^.sock))390 else goto exitLabel;391 end;392 393 //处理完端口请求394 WriteLn(‘创建工作线程 ‘);395 gWorker := BeginThread(nil, 0, @WorkProc, nil, 0, threadID);396 if gWorker = 0 then goto exitLabel;397 398 WriteLn(‘键入exit退出 ‘);399 while True do400 begin401 ReadLn(console);402 if console = ‘exit‘ then403 Break;404 console := ‘‘;405 end;406 407 WriteLn(‘投递完成端口结束标记, 退出工作线程‘);408 if not PostQueuedCompletionStatus(gIOCP, 0, 0, Pointer(SHUTDOWN_FLAG)) then409 begin410 WriteLn(‘投递完成端口结束标记错误:‘ + inttostr(GetLastError));411 TerminateThread(gWorker, 0);412 end;413 414 WriteLn(‘关闭工作线程句柄‘);415 CloseHandle(gWorker);416 417 WriteLn(‘释放socket资源‘);418 for i := 0 to ACCEPT_COUNT - 1 do419 Dispose(gOverls[i]);420 FillChar(gOverls[0], SizeOf(gOverls), 0);421 422 WriteLn(‘关闭监听端口‘);423 FunCloseSocket(gListen);424 425 WriteLn(‘关闭完成端口‘);426 CloseHandle(gIOCP);427 428 WriteLn(‘释放网络环境‘);429 WSACleanup;430 431 need := false;432 433 exitLabel:434 if need then435 WriteLn(‘运行错误:‘ + Inttostr(WSAGetLastError));436 437 except438 on e:Exception do439 WriteLn(e.Message);440 end;441 442 end.
1 {*******************************************************} 2 { } 3 { Windows Sockets API v. 2.20 Header File } 4 { } 5 { Prerelease 16.09.96 } 6 { } 7 { Base: WinSock2.h from Winsock SDK 1.6 BETA } 8 { Delphi 2 RTL Windows32 API Interface } 9 { } 10 { (c) 1996 by Artur Grajek ‘AHS‘ } 11 { email: c51725ag@juggernaut.eti.pg.gda.pl } 12 { } 13 {*******************************************************} 14 {$MINENUMSIZE 4} { Force 4 bytes enumeration size} 15 16 unit WinSock2; 17 18 interface 19 20 uses Windows; 21 22 type 23 u_char = Char; 24 u_short = Word; 25 u_int = Integer; 26 u_long = Longint; 27 pu_long = ^u_long; 28 pu_short = ^u_short; 29 30 { The new type to be used in all 31 instances which refer to sockets. } 32 TSocket = u_int; 33 34 const 35 FD_SETSIZE = 64; 36 37 type 38 PFDSet = ^TFDSet; 39 TFDSet = packed record 40 fd_count: u_int; 41 fd_array: array[0..FD_SETSIZE-1] of TSocket; 42 end; 43 44 PTimeVal = ^TTimeVal; 45 TTimeVal = packed record 46 tv_sec: Longint; 47 tv_usec: Longint; 48 end; 49 50 const 51 IOCPARM_MASK = $7f; 52 IOC_VOID = $20000000; 53 IOC_OUT = $40000000; 54 IOC_IN = $80000000; 55 IOC_INOUT = (IOC_IN or IOC_OUT); 56 57 FIONREAD = IOC_OUT or { get # bytes to read } 58 ((Longint(SizeOf(Longint)) and IOCPARM_MASK) shl 16) or 59 (Longint(Byte(‘f‘)) shl 8) or 127; 60 FIONBIO = IOC_IN or { set/clear non-blocking i/o } 61 ((Longint(SizeOf(Longint)) and IOCPARM_MASK) shl 16) or 62 (Longint(Byte(‘f‘)) shl 8) or 126; 63 FIOASYNC = IOC_IN or { set/clear async i/o } 64 ((Longint(SizeOf(Longint)) and IOCPARM_MASK) shl 16) or 65 (Longint(Byte(‘f‘)) shl 8) or 125; 66 67 68 const 69 70 { Protocols } 71 72 IPPROTO_IP = 0; { Dummy } 73 IPPROTO_ICMP = 1; { Internet Control Message Protocol } 74 IPPROTO_IGMP = 2; { Internet Group Management Protocol} 75 IPPROTO_GGP = 3; { Gateway } 76 IPPROTO_TCP = 6; { TCP } 77 IPPROTO_PUP = 12; { PUP } 78 IPPROTO_UDP = 17; { User Datagram Protocol } 79 IPPROTO_IDP = 22; { XNS IDP } 80 IPPROTO_ND = 77; { UNOFFICIAL Net Disk Protocol } 81 82 IPPROTO_RAW = 255; 83 IPPROTO_MAX = 256; 84 85 { Port/socket numbers: network standard functions} 86 87 IPPORT_ECHO = 7; 88 IPPORT_DISCARD = 9; 89 IPPORT_SYSTAT = 11; 90 IPPORT_DAYTIME = 13; 91 IPPORT_NETSTAT = 15; 92 IPPORT_FTP = 21; 93 IPPORT_TELNET = 23; 94 IPPORT_SMTP = 25; 95 IPPORT_TIMESERVER = 37; 96 IPPORT_NAMESERVER = 42; 97 IPPORT_WHOIS = 43; 98 IPPORT_MTP = 57; 99 100 { Port/socket numbers: host specific functions } 101 102 IPPORT_TFTP = 69; 103 IPPORT_RJE = 77; 104 IPPORT_FINGER = 79; 105 IPPORT_TTYLINK = 87; 106 IPPORT_SUPDUP = 95; 107 108 { UNIX TCP sockets } 109 110 IPPORT_EXECSERVER = 512; 111 IPPORT_LOGINSERVER = 513; 112 IPPORT_CMDSERVER = 514; 113 IPPORT_EFSSERVER = 520; 114 115 { UNIX UDP sockets } 116 117 IPPORT_BIFFUDP = 512; 118 IPPORT_WHOSERVER = 513; 119 IPPORT_ROUTESERVER = 520; 120 121 { Ports < IPPORT_RESERVED are reserved for 122 privileged processes (e.g. root). } 123 124 IPPORT_RESERVED = 1024; 125 126 { Link numbers } 127 128 IMPLINK_IP = 155; 129 IMPLINK_LOWEXPER = 156; 130 IMPLINK_HIGHEXPER = 158; 131 132 type 133 SunB = packed record 134 s_b1, s_b2, s_b3, s_b4: u_char; 135 end; 136 137 SunW = packed record 138 s_w1, s_w2: u_short; 139 end; 140 141 PInAddr = ^TInAddr; 142 TInAddr = packed record 143 case integer of 144 0: (S_un_b: SunB); 145 1: (S_un_w: SunW); 146 2: (S_addr: u_long); 147 end; 148 149 PSockAddrIn = ^TSockAddrIn; 150 TSockAddrIn = packed record 151 case Integer of 152 0: (sin_family: u_short; 153 sin_port: u_short; 154 sin_addr: TInAddr; 155 sin_zero: array[0..7] of Char); 156 1: (sa_family: u_short; 157 sa_data: array[0..13] of Char) 158 end; 159 type 160 PHostEnt = ^THostEnt; 161 THostEnt = packed record 162 h_name: PChar; 163 h_aliases: ^PChar; 164 h_addrtype: Smallint; 165 h_length: Smallint; 166 case integer of 167 0: (h_addr_list: ^PChar); 168 1: (h_addr: ^PInAddr); 169 end; 170 171 PNetEnt = ^TNetEnt; 172 TNetEnt = packed record 173 n_name: PChar; 174 n_aliases: ^PChar; 175 n_addrtype: Smallint; 176 n_net: u_long; 177 end; 178 179 PServEnt = ^TServEnt; 180 TServEnt = packed record 181 s_name: PChar; 182 s_aliases: ^PChar; 183 s_port: Smallint; 184 s_proto: PChar; 185 end; 186 187 PProtoEnt = ^TProtoEnt; 188 TProtoEnt = packed record 189 p_name: PChar; 190 p_aliases: ^Pchar; 191 p_proto: Smallint; 192 end; 193 194 const 195 INADDR_ANY = $00000000; 196 INADDR_LOOPBACK = $7F000001; 197 INADDR_BROADCAST = $FFFFFFFF; 198 INADDR_NONE = $FFFFFFFF; 199 200 ADDR_ANY = INADDR_ANY; 201 202 const 203 WSADESCRIPTION_LEN = 256; 204 WSASYS_STATUS_LEN = 128; 205 206 type 207 PWSAData =http://www.mamicode.com/ ^TWSAData; 208 TWSAData = http://www.mamicode.com/packed record 209 wVersion: Word; 210 wHighVersion: Word; 211 szDescription: array[0..WSADESCRIPTION_LEN] of Char; 212 szSystemStatus: array[0..WSASYS_STATUS_LEN] of Char; 213 iMaxSockets: Word; 214 iMaxUdpDg: Word; 215 lpVendorInfo: PChar; 216 end; 217 218 PTransmitFileBuffers = ^TTransmitFileBuffers; 219 TTransmitFileBuffers = packed record 220 Head: Pointer; 221 HeadLength: DWORD; 222 Tail: Pointer; 223 TailLength: DWORD; 224 end; 225 226 const 227 228 { Options for use with [gs]etsockopt at the IP level. } 229 230 IP_OPTIONS = 1; 231 IP_MULTICAST_IF = 2; { set/get IP multicast interface } 232 IP_MULTICAST_TTL = 3; { set/get IP multicast timetolive } 233 IP_MULTICAST_LOOP = 4; { set/get IP multicast loopback } 234 IP_ADD_MEMBERSHIP = 5; { add an IP group membership } 235 IP_DROP_MEMBERSHIP = 6; { drop an IP group membership } 236 237 IP_DEFAULT_MULTICAST_TTL = 1; { normally limit m‘casts to 1 hop } 238 IP_DEFAULT_MULTICAST_LOOP = 1; { normally hear sends if a member } 239 IP_MAX_MEMBERSHIPS = 20; { per socket; must fit in one mbuf } 240 241 { This is used instead of -1, since the 242 TSocket type is unsigned.} 243 244 INVALID_SOCKET = TSocket(NOT(0)); 245 SOCKET_ERROR = -1; 246 247 { The following may be used in place of the address family, socket type, or 248 protocol in a call to WSASocket to indicate that the corresponding value 249 should be taken from the supplied WSAPROTOCOL_INFO structure instead of the 250 parameter itself. 251 } 252 253 FROM_PROTOCOL_INFO = -1; 254 255 { Types } 256 257 SOCK_STREAM = 1; { stream socket } 258 SOCK_DGRAM = 2; { datagram socket } 259 SOCK_RAW = 3; { raw-protocol interface } 260 SOCK_RDM = 4; { reliably-delivered message } 261 SOCK_SEQPACKET = 5; { sequenced packet stream } 262 263 { Option flags per-socket. } 264 265 SO_DEBUG = $0001; { turn on debugging info recording } 266 SO_ACCEPTCONN = $0002; { socket has had listen() } 267 SO_REUSEADDR = $0004; { allow local address reuse } 268 SO_KEEPALIVE = $0008; { keep connections alive } 269 SO_DONTROUTE = $0010; { just use interface addresses } 270 SO_BROADCAST = $0020; { permit sending of broadcast msgs } 271 SO_USELOOPBACK = $0040; { bypass hardware when possible } 272 SO_LINGER = $0080; { linger on close if data present } 273 SO_OOBINLINE = $0100; { leave received OOB data in line } 274 275 SO_DONTLINGER = $ff7f; 276 277 { Additional options. } 278 279 SO_SNDBUF = $1001; { send buffer size } 280 SO_RCVBUF = $1002; { receive buffer size } 281 SO_SNDLOWAT = $1003; { send low-water mark } 282 SO_RCVLOWAT = $1004; { receive low-water mark } 283 SO_SNDTIMEO = $1005; { send timeout } 284 SO_RCVTIMEO = $1006; { receive timeout } 285 SO_ERROR = $1007; { get error status and clear } 286 SO_TYPE = $1008; { get socket type } 287 288 { Options for connect and disconnect data and options. Used only by 289 non-TCP/IP transports such as DECNet, OSI TP4, etc. } 290 291 SO_CONNDATA = http://www.mamicode.com/$7000; 292 SO_CONNOPT = $7001; 293 SO_DISCDATA = http://www.mamicode.com/$7002; 294 SO_DISCOPT = $7003; 295 SO_CONNDATALEN = $7004; 296 SO_CONNOPTLEN = $7005; 297 SO_DISCDATALEN = $7006; 298 SO_DISCOPTLEN = $7007; 299 300 { WinSock 2 extension -- new options } 301 302 SO_GROUP_ID = $2001; { ID of a socket group} 303 SO_GROUP_PRIORITY = $2002; { the relative priority within a group} 304 SO_MAX_MSG_SIZE = $2003; { maximum message size } 305 SO_PROTOCOL_INFOA = $2004; { WSAPROTOCOL_INFOA structure } 306 SO_PROTOCOL_INFOW = $2005; { WSAPROTOCOL_INFOW structure } 307 308 {$IFDEF UNICODE } 309 SO_PROTOCOL_INFO = SO_PROTOCOL_INFOW; 310 {$ELSE } 311 SO_PROTOCOL_INFO = SO_PROTOCOL_INFOA; 312 {$ENDIF UNICODE } 313 PVD_CONFIG = $3001; {configuration info for service provider } 314 315 { Option for opening sockets for synchronous access. } 316 317 SO_OPENTYPE = $7008; 318 319 SO_SYNCHRONOUS_ALERT = $10; 320 SO_SYNCHRONOUS_NONALERT = $20; 321 322 { Other NT-specific options. } 323 324 SO_MAXDG = $7009; 325 SO_MAXPATHDG = $700A; 326 327 { TCP options. } 328 329 TCP_NODELAY = $0001; 330 TCP_BSDURGENT = $7000; 331 332 { Address families. } 333 334 AF_UNSPEC = 0; { unspecified } 335 AF_UNIX = 1; { local to host (pipes, portals) } 336 AF_INET = 2; { internetwork: UDP, TCP, etc. } 337 AF_IMPLINK = 3; { arpanet imp addresses } 338 AF_PUP = 4; { pup protocols: e.g. BSP } 339 AF_CHAOS = 5; { mit CHAOS protocols } 340 AF_IPX = 6; { IPX and SPX } 341 AF_NS = 6; { XEROX NS protocols } 342 AF_ISO = 7; { ISO protocols } 343 AF_OSI = AF_ISO; { OSI is ISO } 344 AF_ECMA = 8; { european computer manufacturers } 345 AF_DATAKIT = 9; { datakit protocols } 346 AF_CCITT = 10; { CCITT protocols, X.25 etc } 347 AF_SNA = 11; { IBM SNA } 348 AF_DECnet = 12; { DECnet } 349 AF_DLI = 13; { Direct data link interface } 350 AF_LAT = 14; { LAT } 351 AF_HYLINK = 15; { NSC Hyperchannel } 352 AF_APPLETALK = 16; { AppleTalk } 353 AF_NETBIOS = 17; { NetBios-style addresses } 354 AF_VOICEVIEW = 18; { VoiceView } 355 AF_FIREFOX = 19; { FireFox } 356 AF_UNKNOWN1 = 20; { Somebody is using this! } 357 AF_BAN = 21; { Banyan } 358 AF_ATM = 22; { Native ATM Services } 359 AF_INET6 = 23; { Internetwork Version 6 } 360 361 AF_MAX = 24; 362 363 type 364 { Structure used by kernel to store most addresses. } 365 366 PSockAddr = ^TSockAddr; 367 TSockAddr = TSockAddrIn; 368 369 { Structure used by kernel to pass protocol information in raw sockets. } 370 PSockProto = ^TSockProto; 371 TSockProto = packed record 372 sp_family: u_short; 373 sp_protocol: u_short; 374 end; 375 376 const 377 { Protocol families, same as address families for now. } 378 379 PF_UNSPEC = AF_UNSPEC; 380 PF_UNIX = AF_UNIX; 381 PF_INET = AF_INET; 382 PF_IMPLINK = AF_IMPLINK; 383 PF_PUP = AF_PUP; 384 PF_CHAOS = AF_CHAOS; 385 PF_NS = AF_NS; 386 PF_IPX = AF_IPX; 387 PF_ISO = AF_ISO; 388 PF_OSI = AF_OSI; 389 PF_ECMA = AF_ECMA; 390 PF_DATAKIT = AF_DATAKIT; 391 PF_CCITT = AF_CCITT; 392 PF_SNA = AF_SNA; 393 PF_DECnet = AF_DECnet; 394 PF_DLI = AF_DLI; 395 PF_LAT = AF_LAT; 396 PF_HYLINK = AF_HYLINK; 397 PF_APPLETALK = AF_APPLETALK; 398 PF_VOICEVIEW = AF_VOICEVIEW; 399 PF_FIREFOX = AF_FIREFOX; 400 PF_UNKNOWN1 = AF_UNKNOWN1; 401 PF_BAN = AF_BAN; 402 PF_ATM = AF_ATM; 403 PF_INET6 = AF_INET6; 404 405 PF_MAX = AF_MAX; 406 407 type 408 { Structure used for manipulating linger option. } 409 PLinger = ^TLinger; 410 TLinger = packed record 411 l_onoff: u_short; 412 l_linger: u_short; 413 end; 414 415 const 416 { Level number for (get/set)sockopt() to apply to socket itself. } 417 418 SOL_SOCKET = $ffff; {options for socket level } 419 420 { Maximum queue length specifiable by listen. } 421 422 SOMAXCONN = $7fffffff; { AHS - by硂 5 !?????? } 423 424 MSG_OOB = $1; {process out-of-band data } 425 MSG_PEEK = $2; {peek at incoming message } 426 MSG_DONTROUTE = $4; {send without using routing tables } 427 428 MSG_MAXIOVLEN = 16; 429 430 MSG_PARTIAL = $8000; {partial send or recv for message xport } 431 432 { WinSock 2 extension -- new flags for WSASend(), WSASendTo(), WSARecv() and 433 WSARecvFrom() } 434 435 MSG_INTERRUPT = $10; {send/recv in the interrupt context} 436 437 { Define constant based on rfc883, used by gethostbyxxxx() calls. } 438 439 MAXGETHOSTSTRUCT = 1024; 440 441 { Define flags to be used with the WSAAsyncSelect() call. } 442 443 FD_READ = $01; 444 FD_WRITE = $02; 445 FD_OOB = $04; 446 FD_ACCEPT = $08; 447 FD_CONNECT = $10; 448 FD_CLOSE = $20; 449 FD_QOS = $40; 450 FD_GROUP_QOS = $80; 451 FD_MAX_EVENTS = 8; 452 FD_ALL_EVENTS = $100; { AHS - trudno powiedzie? ile powinno by?} 453 454 { All Windows Sockets error constants are biased by WSABASEERR from the "normal" } 455 456 WSABASEERR = 10000; 457 458 { Windows Sockets definitions of regular Microsoft C error constants } 459 460 WSAEINTR = (WSABASEERR+4); 461 WSAEBADF = (WSABASEERR+9); 462 WSAEACCES = (WSABASEERR+13); 463 WSAEFAULT = (WSABASEERR+14); 464 WSAEINVAL = (WSABASEERR+22); 465 WSAEMFILE = (WSABASEERR+24); 466 467 { Windows Sockets definitions of regular Berkeley error constants } 468 469 WSAEWOULDBLOCK = (WSABASEERR+35); 470 WSAEINPROGRESS = (WSABASEERR+36); 471 WSAEALREADY = (WSABASEERR+37); 472 WSAENOTSOCK = (WSABASEERR+38); 473 WSAEDESTADDRREQ = (WSABASEERR+39); 474 WSAEMSGSIZE = (WSABASEERR+40); 475 WSAEPROTOTYPE = (WSABASEERR+41); 476 WSAENOPROTOOPT = (WSABASEERR+42); 477 WSAEPROTONOSUPPORT = (WSABASEERR+43); 478 WSAESOCKTNOSUPPORT = (WSABASEERR+44); 479 WSAEOPNOTSUPP = (WSABASEERR+45); 480 WSAEPFNOSUPPORT = (WSABASEERR+46); 481 WSAEAFNOSUPPORT = (WSABASEERR+47); 482 WSAEADDRINUSE = (WSABASEERR+48); 483 WSAEADDRNOTAVAIL = (WSABASEERR+49); 484 WSAENETDOWN = (WSABASEERR+50); 485 WSAENETUNREACH = (WSABASEERR+51); 486 WSAENETRESET = (WSABASEERR+52); 487 WSAECONNABORTED = (WSABASEERR+53); 488 WSAECONNRESET = (WSABASEERR+54); 489 WSAENOBUFS = (WSABASEERR+55); 490 WSAEISCONN = (WSABASEERR+56); 491 WSAENOTCONN = (WSABASEERR+57); 492 WSAESHUTDOWN = (WSABASEERR+58); 493 WSAETOOMANYREFS = (WSABASEERR+59); 494 WSAETIMEDOUT = (WSABASEERR+60); 495 WSAECONNREFUSED = (WSABASEERR+61); 496 WSAELOOP = (WSABASEERR+62); 497 WSAENAMETOOLONG = (WSABASEERR+63); 498 WSAEHOSTDOWN = (WSABASEERR+64); 499 WSAEHOSTUNREACH = (WSABASEERR+65); 500 WSAENOTEMPTY = (WSABASEERR+66); 501 WSAEPROCLIM = (WSABASEERR+67); 502 WSAEUSERS = (WSABASEERR+68); 503 WSAEDQUOT = (WSABASEERR+69); 504 WSAESTALE = (WSABASEERR+70); 505 WSAEREMOTE = (WSABASEERR+71); 506 507 { Extended Windows Sockets error constant definitions } 508 509 WSASYSNOTREADY = (WSABASEERR+91); 510 WSAVERNOTSUPPORTED = (WSABASEERR+92); 511 WSANOTINITIALISED = (WSABASEERR+93); 512 WSAEDISCON = (WSABASEERR+101); 513 WSAENOMORE = (WSABASEERR+102); 514 WSAECANCELLED = (WSABASEERR+103); 515 WSAEEINVALIDPROCTABLE = (WSABASEERR+104); 516 WSAEINVALIDPROVIDER = (WSABASEERR+105); 517 WSAEPROVIDERFAILEDINIT = (WSABASEERR+106); 518 WSASYSCALLFAILURE = (WSABASEERR+107); 519 WSASERVICE_NOT_FOUND = (WSABASEERR+108); 520 WSATYPE_NOT_FOUND = (WSABASEERR+109); 521 WSA_E_NO_MORE = (WSABASEERR+110); 522 WSA_E_CANCELLED = (WSABASEERR+111); 523 WSAEREFUSED = (WSABASEERR+112); 524 525 { Error return codes from gethostbyname() and gethostbyaddr() 526 (when using the resolver). Note that these errors are 527 retrieved via WSAGetLastError() and must therefore follow 528 the rules for avoiding clashes with error numbers from 529 specific implementations or language run-time systems. 530 For this reason the codes are based at WSABASEERR+1001. 531 Note also that [WSA]NO_ADDRESS is defined only for 532 compatibility purposes. } 533 534 { Authoritative Answer: Host not found } 535 536 WSAHOST_NOT_FOUND = (WSABASEERR+1001); 537 HOST_NOT_FOUND = WSAHOST_NOT_FOUND; 538 539 { Non-Authoritative: Host not found, or SERVERFAIL } 540 541 WSATRY_AGAIN = (WSABASEERR+1002); 542 TRY_AGAIN = WSATRY_AGAIN; 543 544 { Non recoverable errors, FORMERR, REFUSED, NOTIMP } 545 546 WSANO_RECOVERY = (WSABASEERR+1003); 547 NO_RECOVERY = WSANO_RECOVERY; 548 549 { Valid name, no data record of requested type } 550 551 WSANO_DATA = http://www.mamicode.com/(WSABASEERR+1004); 552 NO_DATA =http://www.mamicode.com/ WSANO_DATA; 553 554 { no address, look for MX record } 555 556 WSANO_ADDRESS = WSANO_DATA; 557 NO_ADDRESS = WSANO_ADDRESS; 558 559 { Windows Sockets errors redefined as regular Berkeley error constants. 560 These are commented out in Windows NT to avoid conflicts with errno.h. 561 Use the WSA constants instead. } 562 563 EWOULDBLOCK = WSAEWOULDBLOCK; 564 EINPROGRESS = WSAEINPROGRESS; 565 EALREADY = WSAEALREADY; 566 ENOTSOCK = WSAENOTSOCK; 567 EDESTADDRREQ = WSAEDESTADDRREQ; 568 EMSGSIZE = WSAEMSGSIZE; 569 EPROTOTYPE = WSAEPROTOTYPE; 570 ENOPROTOOPT = WSAENOPROTOOPT; 571 EPROTONOSUPPORT = WSAEPROTONOSUPPORT; 572 ESOCKTNOSUPPORT = WSAESOCKTNOSUPPORT; 573 EOPNOTSUPP = WSAEOPNOTSUPP; 574 EPFNOSUPPORT = WSAEPFNOSUPPORT; 575 EAFNOSUPPORT = WSAEAFNOSUPPORT; 576 EADDRINUSE = WSAEADDRINUSE; 577 EADDRNOTAVAIL = WSAEADDRNOTAVAIL; 578 ENETDOWN = WSAENETDOWN; 579 ENETUNREACH = WSAENETUNREACH; 580 ENETRESET = WSAENETRESET; 581 ECONNABORTED = WSAECONNABORTED; 582 ECONNRESET = WSAECONNRESET; 583 ENOBUFS = WSAENOBUFS; 584 EISCONN = WSAEISCONN; 585 ENOTCONN = WSAENOTCONN; 586 ESHUTDOWN = WSAESHUTDOWN; 587 ETOOMANYREFS = WSAETOOMANYREFS; 588 ETIMEDOUT = WSAETIMEDOUT; 589 ECONNREFUSED = WSAECONNREFUSED; 590 ELOOP = WSAELOOP; 591 ENAMETOOLONG = WSAENAMETOOLONG; 592 EHOSTDOWN = WSAEHOSTDOWN; 593 EHOSTUNREACH = WSAEHOSTUNREACH; 594 ENOTEMPTY = WSAENOTEMPTY; 595 EPROCLIM = WSAEPROCLIM; 596 EUSERS = WSAEUSERS; 597 EDQUOT = WSAEDQUOT; 598 ESTALE = WSAESTALE; 599 EREMOTE = WSAEREMOTE; 600 601 { AHS } 602 { WinSock 2 extension -- new error codes and type definition } 603 604 type 605 WSAEVENT = THANDLE; 606 LPHANDLE = PHANDLE; 607 LPWSAEVENT = LPHANDLE; 608 WSAOVERLAPPED = TOVERLAPPED; 609 LPWSAOVERLAPPED = POverlapped; 610 611 const 612 613 WSA_IO_PENDING = ERROR_IO_PENDING; 614 WSA_IO_INCOMPLETE = ERROR_IO_INCOMPLETE; 615 WSA_INVALID_HANDLE = ERROR_INVALID_HANDLE; 616 WSA_INVALID_PARAMETER = ERROR_INVALID_PARAMETER; 617 WSA_NOT_ENOUGH_MEMORY = ERROR_NOT_ENOUGH_MEMORY; 618 WSA_OPERATION_ABORTED = ERROR_OPERATION_ABORTED; 619 620 WSA_INVALID_EVENT = WSAEVENT(NiL); 621 WSA_MAXIMUM_WAIT_EVENTS = MAXIMUM_WAIT_OBJECTS; 622 WSA_WAIT_FAILED = DWORD($FFFFFFFF); { ahs } 623 WSA_WAIT_EVENT_0 = WAIT_OBJECT_0; 624 WSA_WAIT_IO_COMPLETION = WAIT_IO_COMPLETION; 625 WSA_WAIT_TIMEOUT = WAIT_TIMEOUT; 626 WSA_INFINITE = INFINITE; 627 628 629 { WinSock 2 extension -- WSABUF and QOS struct } 630 631 type 632 633 PWSABUF = ^TWSABUF; 634 TWSABUF = packed record 635 len : u_long; { the length of the buffer } 636 buf : Pointer; //PChar; { the pointer to the buffer } 637 end; 638 639 GUARANTEE = ( 640 BestEffortService, 641 ControlledLoadService, 642 PredictiveService, 643 GuaranteedDelayService, 644 GuaranteedService 645 ); 646 647 PFlowspec = ^TFlowspec; 648 TFlowspec = packed record 649 TokenRate : LongInt; { In Bytes/sec } 650 TokenBucketSize : LongInt; { In Bytes } 651 PeakBandwidth : LongInt; { In Bytes/sec } 652 Latency : LongInt; { In microseconds } 653 DelayVariation : LongInt; { In microseconds } 654 LevelOfGuarantee : Guarantee; { Guaranteed, Predictive } 655 { or Best Effort } 656 CostOfCall : LongInt; { Reserved for future use, } 657 { must be set to 0 now } 658 NetworkAvailability : LongInt; { read-only: } 659 { 1 if accessible, } 660 { 0 if not } 661 end; 662 663 PQOS = ^TQualityOfService; 664 TQualityOfService = packed record 665 SendingFlowspec :TFlowspec; { the flow spec for data sending } 666 ReceivingFlowspec :TFlowspec; { the flow spec for data receiving } 667 ProviderSpecific :TWSABuf; { additional provider specific stuff } 668 end; 669 670 const 671 672 { WinSock 2 extension -- manifest constants for return values of the 673 condition function} 674 675 CF_ACCEPT = $0000; 676 CF_REJECT = $0001; 677 CF_DEFER = $0002; 678 679 {WinSock 2 extension -- manifest constants for shutdown() } 680 681 SD_RECEIVE = $00; 682 SD_SEND = $01; 683 SD_BOTH = $02; 684 685 { WinSock 2 extension -- data type and manifest constants for socket groups } 686 type 687 688 TGroup = u_int; 689 PGroup = ^TGroup; 690 691 const 692 693 SG_UNCONSTRAINED_GROUP = $01; 694 SG_CONSTRAINED_GROUP = $02; 695 696 { WinSock 2 extension -- data type for WSAEnumNetworkEvents() } 697 698 type 699 700 PWSANETWORKEVENTS = ^TWSANETWORKEVENTS; 701 TWSANETWORKEVENTS = packed record 702 lNetworkEvents :u_long; 703 iErrorCode : array [0..FD_MAX_EVENTS-1] of u_int; 704 end; 705 706 {WinSock 2 extension -- WSAPROTOCOL_INFO structure and associated 707 manifest constants} 708 709 PGUID = ^TGUID; 710 TGUID = packed record 711 Data1 :u_long; 712 Data2 :u_short; 713 Data3 :u_short; 714 Data4 : array [0..8-1] of u_char; 715 end; 716 717 const 718 719 MAX_PROTOCOL_CHAIN = 7; 720 721 BASE_PROTOCOL = 1; 722 LAYERED_PROTOCOL = 0; 723 724 type 725 726 PWSAPROTOCOLCHAIN = ^TWSAPROTOCOLCHAIN; 727 TWSAPROTOCOLCHAIN = packed record 728 ChainLen : Integer; { the length of the chain, } 729 { length = 0 means layered protocol, } 730 { length = 1 means base protocol, } 731 { length > 1 means protocol chain } 732 ChainEntries : array[0..MAX_PROTOCOL_CHAIN-1] of DWORD; { a list of dwCatalogEntryIds } 733 end; 734 735 const 736 737 WSAPROTOCOL_LEN = 255; 738 739 type 740 741 PWSAPROTOCOL_INFOA = ^TWSAPROTOCOL_INFOA; 742 TWSAPROTOCOL_INFOA = packed record 743 dwServiceFlags1 : DWORD; 744 dwServiceFlags2 : DWORD; 745 dwServiceFlags3 : DWORD; 746 dwServiceFlags4 : DWORD; 747 dwProviderFlags : DWORD; 748 ProviderId : TGUID; 749 dwCatalogEntryId : DWORD; 750 ProtocolChain : TWSAPROTOCOLCHAIN; 751 iVersion : u_int; 752 iAddressFamily : u_int; 753 iMaxSockAddr : u_int; 754 iMinSockAddr : u_int; 755 iSocketType : u_int; 756 iProtocol : u_int; 757 iProtocolMaxOffset : u_int; 758 iNetworkByteOrder : u_int; 759 iSecurityScheme : u_int; 760 dwMessageSize : DWORD; 761 dwProviderReserved : DWORD; 762 szProtocol : array [0..WSAPROTOCOL_LEN+1-1] of u_char; 763 end; 764 765 PWSAPROTOCOL_INFOW = ^TWSAPROTOCOL_INFOW; 766 TWSAPROTOCOL_INFOW = packed record 767 dwServiceFlags1 : DWORD; 768 dwServiceFlags2 : DWORD; 769 dwServiceFlags3 : DWORD; 770 dwServiceFlags4 : DWORD; 771 dwProviderFlags : DWORD; 772 ProviderId : TGUID; 773 dwCatalogEntryId : DWORD; 774 ProtocolChain : TWSAPROTOCOLCHAIN; 775 iVersion : u_int; 776 iAddressFamily : u_int; 777 iMaxSockAddr : u_int; 778 iMinSockAddr : u_int; 779 iSocketType : u_int; 780 iProtocol : u_int; 781 iProtocolMaxOffset : u_int; 782 iNetworkByteOrder : u_int; 783 iSecurityScheme : u_int; 784 dwMessageSize : DWORD; 785 dwProviderReserved : DWORD; 786 szProtocol : array [0..WSAPROTOCOL_LEN+1-1] of WCHAR; 787 end; 788 789 {$ifdef UNICODE} 790 791 TWSAPROTOCOL_INFO = TWSAPROTOCOL_INFOW ; 792 PWSAPROTOCOL_INFO = PWSAPROTOCOL_INFOW ; 793 794 {$else} 795 796 TWSAPROTOCOL_INFO = TWSAPROTOCOL_INFOA ; 797 PWSAPROTOCOL_INFO = PWSAPROTOCOL_INFOA ; 798 799 {$endif UNICODE} 800 801 const 802 { Flag bit definitions for dwProviderFlags } 803 804 PFL_MULTIPLE_PROTO_ENTRIES =$00000001; 805 PFL_RECOMMENDED_PROTO_ENTRY =$00000002; 806 PFL_HIDDEN =$00000004; 807 PFL_MATCHES_PROTOCOL_ZERO =$00000008; 808 809 { Flag bit definitions for dwServiceFlags1 } 810 XP1_CONNECTIONLESS =$00000001; 811 XP1_GUARANTEED_DELIVERY =$00000002; 812 XP1_GUARANTEED_ORDER =$00000004; 813 XP1_MESSAGE_ORIENTED =$00000008; 814 XP1_PSEUDO_STREAM =$00000010; 815 XP1_GRACEFUL_CLOSE =$00000020; 816 XP1_EXPEDITED_DATA =http://www.mamicode.com/$00000040; 817 XP1_CONNECT_DATA =http://www.mamicode.com/$00000080; 818 XP1_DISCONNECT_DATA =http://www.mamicode.com/$00000100; 819 XP1_SUPPORT_BROADCAST =$00000200; 820 XP1_SUPPORT_MULTIPOINT =$00000400; 821 XP1_MULTIPOINT_CONTROL_PLANE =$00000800; 822 XP1_MULTIPOINT_DATA_PLANE =$00001000; 823 XP1_QOS_SUPPORTED =$00002000; 824 XP1_INTERRUPT =$00004000; 825 XP1_UNI_SEND =$00008000; 826 XP1_UNI_RECV =$00010000; 827 XP1_IFS_HANDLES =$00020000; 828 XP1_PARTIAL_MESSAGE =$00040000; 829 830 BIGENDIAN =$0000; 831 LITTLEENDIAN =$0001; 832 833 SECURITY_PROTOCOL_NONE =$0000; 834 835 { WinSock 2 extension -- manifest constants for WSAJoinLeaf() } 836 837 JL_SENDER_ONLY =$01; 838 JL_RECEIVER_ONLY =$02; 839 JL_BOTH =$04; 840 841 { WinSock 2 extension -- manifest constants for WSASocket() } 842 843 WSA_FLAG_OVERLAPPED =$01; 844 WSA_FLAG_MULTIPOINT_C_ROOT =$02; 845 WSA_FLAG_MULTIPOINT_C_LEAF =$04; 846 WSA_FLAG_MULTIPOINT_D_ROOT =$08; 847 WSA_FLAG_MULTIPOINT_D_LEAF =$10; 848 849 { WinSock 2 extension -- manifest constants for WSAIoctl() } 850 851 IOC_UNIX =$00000000; 852 IOC_WS2 =$08000000; 853 IOC_PROTOCOL =$10000000; 854 IOC_VENDOR =$18000000; 855 856 SIO_ASSOCIATE_HANDLE = IOC_IN or IOC_WS2 or 1; 857 SIO_ENABLE_CIRCULAR_QUEUEING = IOC_VOID or IOC_WS2 or 2; 858 SIO_FIND_ROUTE = IOC_OUT or IOC_WS2 or 3; 859 SIO_FLUSH = IOC_VOID or IOC_WS2 or 4; 860 SIO_GET_BROADCAST_ADDRESS = IOC_OUT or IOC_WS2 or 5; 861 SIO_GET_EXTENSION_FUNCTION_POINTER = IOC_INOUT or IOC_WS2 or 6; 862 SIO_GET_QOS = IOC_INOUT or IOC_WS2 or 7; 863 SIO_GET_GROUP_QOS = IOC_INOUT or IOC_WS2 or 8; 864 SIO_MULTIPOINT_LOOPBACK = IOC_IN or IOC_WS2 or 9; 865 SIO_MULTICAST_SCOPE = IOC_IN or IOC_WS2 or 10; 866 SIO_SET_QOS = IOC_IN or IOC_WS2 or 11; 867 SIO_SET_GROUP_QOS = IOC_IN or IOC_WS2 or 12; 868 SIO_TRANSLATE_HANDLE = IOC_INOUT or IOC_WS2 or 13; 869 870 { WinSock 2 extension -- manifest constants for SIO_TRANSLATE_HANDLE ioctl } 871 872 TH_NETDEV = $00000001; 873 TH_TAPI = $00000002; 874 875 { Microsoft Windows Extended data types required for the functions to 876 convert back and forth between binary and string forms of 877 addresses. } 878 879 type 880 881 SOCKADDR = TSockAddr; { AHS ?} 882 { PSOCKADDR = PSockaddr;} 883 LPSOCKADDR = PSockaddr; 884 885 { Manifest constants and type definitions related to name resolution and 886 registration (RNR) API } 887 888 PBLOB = ^TBLOB; 889 TBLOB = packed record 890 cbSize :ULONG; 891 pBlobData :^byte; 892 end; 893 894 { Service Install Flags } 895 896 const 897 898 SERVICE_MULTIPLE =$00000001; 899 900 { Name Spaces } 901 902 NS_ALL = 0; 903 904 NS_SAP = 1; 905 NS_NDS = 2; 906 NS_PEER_BROWSE = 3; 907 908 NS_TCPIP_LOCAL = 10; 909 NS_TCPIP_HOSTS = 11; 910 NS_DNS = 12; 911 NS_NETBT = 13; 912 NS_WINS = 14; 913 914 NS_NBP = 20; 915 916 NS_MS = 30; 917 NS_STDA = 31; 918 NS_NTDS = 32; 919 920 NS_X500 = 40; 921 NS_NIS = 41; 922 NS_NISPLUS = 42; 923 924 NS_WRQ = 50; 925 926 927 { Resolution flags for WSAGetAddressByName(). 928 Note these are also used by the 1.1 API GetAddressByName, so 929 leave them around. } 930 931 RES_UNUSED_1 =$00000001; 932 RES_FLUSH_CACHE =$00000002; 933 RES_SERVICE =$00000004; 934 935 936 { Well known value names for Service Types } 937 938 SERVICE_TYPE_VALUE_IPXPORTA =‘IpxSocket‘; 939 SERVICE_TYPE_VALUE_IPXPORTW = ‘IpxSocket‘; 940 SERVICE_TYPE_VALUE_SAPIDA = ‘SapId‘; 941 SERVICE_TYPE_VALUE_SAPIDW =‘SapId‘; 942 943 SERVICE_TYPE_VALUE_TCPPORTA = ‘TcpPort‘; 944 SERVICE_TYPE_VALUE_TCPPORTW =‘TcpPort‘; 945 946 SERVICE_TYPE_VALUE_UDPPORTA = ‘UdpPort‘; 947 SERVICE_TYPE_VALUE_UDPPORTW =‘UdpPort‘; 948 949 SERVICE_TYPE_VALUE_OBJECTIDA = ‘ObjectId‘; 950 SERVICE_TYPE_VALUE_OBJECTIDW =‘ObjectId‘; 951 952 {$ifdef UNICODE} 953 954 SERVICE_TYPE_VALUE_SAPID =SERVICE_TYPE_VALUE_SAPIDW; 955 SERVICE_TYPE_VALUE_TCPPORT =SERVICE_TYPE_VALUE_TCPPORTW; 956 SERVICE_TYPE_VALUE_UDPPORT =SERVICE_TYPE_VALUE_UDPPORTW; 957 SERVICE_TYPE_VALUE_OBJECTID =SERVICE_TYPE_VALUE_OBJECTIDW; 958 959 {$else} { not UNICODE } 960 961 SERVICE_TYPE_VALUE_SAPID =SERVICE_TYPE_VALUE_SAPIDA; 962 SERVICE_TYPE_VALUE_TCPPORT =SERVICE_TYPE_VALUE_TCPPORTA; 963 SERVICE_TYPE_VALUE_UDPPORT =SERVICE_TYPE_VALUE_UDPPORTA; 964 SERVICE_TYPE_VALUE_OBJECTID =SERVICE_TYPE_VALUE_OBJECTIDA; 965 966 {$endif} 967 968 { SockAddr Information } 969 type 970 971 PSOCKET_ADDRESS = ^TSOCKET_ADDRESS; 972 TSOCKET_ADDRESS = packed record 973 lpSockaddr : PSOCKADDR ; 974 iSockaddrLength : u_int; 975 end; 976 977 { CSAddr Information } 978 979 PCSADDR_INFO = ^TCSADDR_INFO; 980 TCSADDR_INFO = packed record 981 LocalAddr :TSOCKET_ADDRESS; 982 RemoteAddr :TSOCKET_ADDRESS; 983 iSocketType :u_int; 984 iProtocol :u_int; 985 end; 986 987 988 { Address Family/Protocol Tuples } 989 990 PAFPROTOCOLS = ^TAFPROTOCOLS; 991 TAFPROTOCOLS = packed record 992 iAddressFamily :u_int; 993 iProtocol :u_int; 994 end; 995 996 { Client Query API Typedefs } 997 998 { The comparators } 999 1000 PWSAEcomparator = ^TWSAEcomparator;1001 TWSAEcomparator = (1002 COMP_EQUAL,1003 COMP_NOTLESS1004 );1005 1006 PWSAVersion = ^TWSAVersion;1007 TWSAVersion = packed record1008 dwVersion : Dword;1009 ecHow : TWSAEComparator;1010 end;1011 1012 PWSAQuerySetA = ^TWSAQuerySetA;1013 TWSAQuerySetA = packed record1014 dwSize : DWord;1015 lpszServiceInstanceName : PChar;1016 lpServiceClassId : PGUID;1017 lpVersion : PWSAVERSION;1018 lpszComment : PChar;1019 dwNameSpace : DWord;1020 lpNSProviderId : PGUID;1021 lpszContext : PChar;1022 dwNumberOfProtocols : DWORD;1023 lpafpProtocols : PAFPROTOCOLS;1024 lpszQueryString : PChar;1025 dwNumberOfCsAddrs : Dword;1026 lpcsaBuffer : PCSADDR_INFO;1027 dwOutputFlags : DWord;1028 lpBlob : PBLOB;1029 end;1030 1031 PWSAQuerySetW = ^TWSAQuerySetW;1032 TWSAQuerySetW = packed record1033 dwSize : DWord;1034 lpszServiceInstanceName : PWideChar; //MIO, antes WideChar1035 lpServiceClassId : PGUID;1036 lpVersion : PWSAVERSION;1037 lpszComment : PWideChar; //MIO, antes WideChar1038 dwNameSpace : DWord;1039 lpNSProviderId : PGUID;1040 lpszContext : PWideChar; //MIO, antes WideChar1041 dwNumberOfProtocols : DWORD;1042 lpafpProtocols : PAFPROTOCOLS;1043 lpszQueryString : PWideChar; //MIO, antes WideChar1044 dwNumberOfCsAddrs : Dword;1045 lpcsaBuffer : PCSADDR_INFO;1046 dwOutputFlags : DWord;1047 lpBlob : PBLOB;1048 end;1049 1050 {$ifdef UNICODE}1051 1052 WSAQUERYSET = TWSAQUERYSETW;1053 PWSAQUERYSET = PWSAQUERYSETW;1054 1055 {$else}1056 1057 WSAQUERYSET = TWSAQUERYSETA;1058 PWSAQUERYSET = PWSAQUERYSETA;1059 1060 {$endif }1061 1062 const1063 1064 LUP_DEEP = $0001;1065 LUP_CONTAINERS = $0002;1066 LUP_NOCONTAINERS = $0004;1067 LUP_NEAREST = $0008;1068 LUP_RETURN_NAME = $0010;1069 LUP_RETURN_TYPE = $0020;1070 LUP_RETURN_VERSION = $0040;1071 LUP_RETURN_COMMENT = $0080;1072 LUP_RETURN_ADDR = $0100;1073 LUP_RETURN_BLOB = $0200;1074 LUP_RETURN_ALIASES = $0400;1075 LUP_RETURN_QUERY_STRING = $0800;1076 LUP_RETURN_ALL = $0FF0;1077 LUP_RES_SERVICE = $8000;1078 1079 LUP_FLUSHCACHE = $1000;1080 LUP_FLUSHPREVIOUS = $2000;1081 1082 1083 { Return flags }1084 1085 RESULT_IS_ALIAS = $0001;1086 1087 { Service Address Registration and Deregistration Data Types. }1088 1089 type1090 1091 PWSAESETSERVICEOP = ^TWSAESETSERVICEOP;1092 TWSAESETSERVICEOP = (1093 RNRSERVICE_REGISTER,1094 RNRSERVICE_DEREGISTER,1095 RNRSERVICE_DELETE1096 );1097 1098 { Service Installation/Removal Data Types. }1099 1100 PWSANSClassInfoA = ^TWSANSClassInfoA;1101 TWSANSClassInfoA = packed record1102 lpszName : PChar;1103 dwNameSpace : Dword;1104 dwValueType : Dword;1105 dwValueSize : Dword;1106 lpValue : Pointer;1107 end;1108 1109 PWSANSClassInfoW = ^TWSANSClassInfoW;1110 TWSANSClassInfoW = packed record1111 lpszName : WideChar;1112 dwNameSpace : Dword;1113 dwValueType : Dword;1114 dwValueSize : Dword;1115 lpValue : Pointer;1116 end;1117 1118 {$ifdef UNICODE }1119 1120 TWSANSCLASSINFO = TWSANSCLASSINFOW;1121 PWSANSCLASSINFO = PWSANSCLASSINFOW;1122 1123 {$else}1124 1125 TWSANSCLASSINFO = TWSANSCLASSINFOA;1126 PWSANSCLASSINFO = PWSANSCLASSINFOA;1127 1128 {$endif UNICODE}1129 1130 PWSAServiceClassInfoA = ^TWSAServiceClassInfoA;1131 TWSAServiceClassInfoA = packed record1132 lpServiceClassId : PGUID;1133 lpszServiceClassName : PChar;1134 dwCount : DWord;1135 lpClassInfos : PWSANSCLASSINFOA;1136 end;1137 1138 PWSAServiceClassInfoW = ^TWSAServiceClassInfoW;1139 TWSAServiceClassInfoW = packed record1140 lpServiceClassId : PGUID;1141 lpszServiceClassName : WideChar;1142 dwCount : DWord;1143 lpClassInfos : PWSANSCLASSINFOW;1144 end;1145 1146 {$ifdef UNICODE}1147 1148 TWSASERVICECLASSINFO = TWSASERVICECLASSINFOW;1149 PWSASERVICECLASSINFO = PWSASERVICECLASSINFOW;1150 1151 {$else}1152 1153 TWSASERVICECLASSINFO = TWSASERVICECLASSINFOA;1154 PWSASERVICECLASSINFO = PWSASERVICECLASSINFOA;1155 1156 {$endif UNICODE}1157 1158 PWSANAMESPACE_INFOA = ^TWSANAMESPACE_INFOA;1159 TWSANAMESPACE_INFOA = packed record1160 NSProviderId : TGUID;1161 dwNameSpace : DWord;1162 fActive : BOOL;1163 dwVersion : DWord;1164 lpszIdentifier : PChar;1165 end;1166 1167 PWSANAMESPACE_INFOW = ^TWSANAMESPACE_INFOW;1168 TWSANAMESPACE_INFOW = packed record1169 NSProviderId : TGUID;1170 dwNameSpace : DWord;1171 fActive : BOOL;1172 dwVersion : DWord;1173 lpszIdentifier : WideChar;1174 end;1175 1176 {$ifdef UNICODE}1177 1178 TWSANAMESPACE_INFO = TWSANAMESPACE_INFOW;1179 PWSANAMESPACE_INFO = PWSANAMESPACE_INFOW;1180 1181 {$else}1182 1183 TWSANAMESPACE_INFO = TWSANAMESPACE_INFOA;1184 PWSANAMESPACE_INFO = PWSANAMESPACE_INFOA;1185 1186 {$endif UNICODE}1187 1188 { AHS END }1189 1190 { Socket function prototypes }1191 1192 // Using "var addr:TSockAddr" in accept makes impossible to compile for IPv61193 function accept(s: TSocket; addr: PSockAddr; var addrlen: Integer): TSocket; stdcall;1194 // Using "var addr:TSockAddr" in bind makes impossible to compile for IPv61195 function bind(s: TSocket; addr: PSockAddr; namelen: Integer): Integer; stdcall;1196 function closesocket(s: TSocket): Integer; stdcall;1197 // Using "var addr:TSockAddr" in connect makes impossible to compile for IPv61198 function connect(s: TSocket; name: PSockAddr; namelen: Integer): Integer; stdcall;1199 function ioctlsocket(s: TSocket; cmd: Longint; var arg: u_long): Integer; stdcall;1200 function getpeername(s: TSocket; var name: TSockAddr; var namelen: Integer): Integer; stdcall;1201 // Using "var addr:TSockAddr" in getsockname makes impossible to compile for IPv61202 function getsockname(s: TSocket; name: PSockAddr; var namelen: Integer): Integer; stdcall;1203 function getsockopt(s: TSocket; level, optname: Integer; optval: PChar; var optlen: Integer): Integer; stdcall;1204 function htonl(hostlong: u_long): u_long; stdcall;1205 function htons(hostshort: u_short): u_short; stdcall;1206 function inet_addr(cp: PChar): u_long; stdcall; {PInAddr;} { TInAddr }1207 function inet_ntoa(inaddr: TInAddr): PChar; stdcall;1208 function listen(s: TSocket; backlog: Integer): Integer; stdcall;1209 function ntohl(netlong: u_long): u_long; stdcall;1210 function ntohs(netshort: u_short): u_short; stdcall;1211 function recv(s: TSocket; var Buf; len, flags: Integer): Integer; stdcall;1212 // Using "var from: TSockAddr" in recvfrom makes impossible to compile for IPv61213 function recvfrom(s: TSocket; var Buf; len, flags: Integer;1214 from: PSockAddr; var fromlen: Integer): Integer; stdcall;1215 function select(nfds: Integer; readfds, writefds, exceptfds: PFDSet;1216 timeout: PTimeVal): Longint; stdcall;1217 function send(s: TSocket; var Buf; len, flags: Integer): Integer; stdcall;1218 // Using "var addrto: TSockAddr" in sendto makes impossible to compile for IPv61219 function sendto(s: TSocket; var Buf; len, flags: Integer; addrto: PSockAddr;1220 tolen: Integer): Integer; stdcall;1221 function setsockopt(s: TSocket; level, optname: Integer; optval: PChar;1222 optlen: Integer): Integer; stdcall;1223 function shutdown(s: TSocket; how: Integer): Integer; stdcall;1224 function socket(af, struct, protocol: Integer): TSocket; stdcall;1225 function gethostbyaddr(addr: Pointer; len, struct: Integer): PHostEnt; stdcall;1226 function gethostbyname(name: PChar): PHostEnt; stdcall;1227 function gethostname(name: PChar; len: Integer): Integer; stdcall;1228 function getservbyport(port: Integer; proto: PChar): PServEnt; stdcall;1229 function getservbyname(name, proto: PChar): PServEnt; stdcall;1230 function getprotobynumber(proto: Integer): PProtoEnt; stdcall;1231 function getprotobyname(name: PChar): PProtoEnt; stdcall;1232 function WSAStartup(wVersionRequired: word; var WSData: TWSAData): Integer; stdcall;1233 function WSACleanup: Integer; stdcall;1234 procedure WSASetLastError(iError: Integer); stdcall;1235 function WSAGetLastError: Integer; stdcall;1236 function WSAIsBlocking: BOOL; stdcall;1237 function WSAUnhookBlockingHook: Integer; stdcall;1238 function WSASetBlockingHook(lpBlockFunc: TFarProc): TFarProc; stdcall;1239 function WSACancelBlockingCall: Integer; stdcall;1240 function WSAAsyncGetServByName(HWindow: HWND; wMsg: u_int;1241 name, proto, buf: PChar; buflen: Integer): THandle; stdcall;1242 function WSAAsyncGetServByPort( HWindow: HWND; wMsg, port: u_int;1243 proto, buf: PChar; buflen: Integer): THandle; stdcall;1244 function WSAAsyncGetProtoByName(HWindow: HWND; wMsg: u_int;1245 name, buf: PChar; buflen: Integer): THandle; stdcall;1246 function WSAAsyncGetProtoByNumber(HWindow: HWND; wMsg: u_int; number: Integer;1247 buf: PChar; buflen: Integer): THandle; stdcall;1248 function WSAAsyncGetHostByName(HWindow: HWND; wMsg: u_int;1249 name, buf: PChar; buflen: Integer): THandle; stdcall;1250 function WSAAsyncGetHostByAddr(HWindow: HWND; wMsg: u_int; addr: PChar;1251 len, struct: Integer; buf: PChar; buflen: Integer): THandle; stdcall;1252 function WSACancelAsyncRequest(hAsyncTaskHandle: THandle): Integer; stdcall;1253 function WSAAsyncSelect(s: TSocket; HWindow: HWND; wMsg: u_int; lEvent: Longint): Integer; stdcall;1254 1255 { WinSock 2 extensions -- data types for the condition function in1256 WSAAccept() and overlapped I/O completion routine. }1257 1258 type1259 1260 PCONDITIONPROC = function ( lpCallerId:PWSABUF; lpCallerData:PWSABUF;1261 lpSQOS:PQOS; lpGQOS:PQOS; lpCalleeId:PWSABUF;lpCalleeData:PWSABUF;1262 g:PGroup; dwCallbackData:DWord) :u_int; stdcall;1263 1264 PWSAOVERLAPPED_COMPLETION_ROUTINE = procedure ( dwError :Dword;1265 cbTransferred: DWORD; lpOverlapped: LPWSAOVERLAPPED; dwFlags: Dword1266 ); stdcall;1267 1268 { WinSock 2 API new function prototypes }1269 1270 function WSAAccept( s: TSOCKET; addr: psockaddr; addrlen: PINT;1271 lpfnCondition: PCONDITIONPROC; dwCallbackData: DWORD): TSOCKET; stdcall;1272 function WSACloseEvent( hEvent: WSAEVENT ):BOOL; stdcall;1273 function WSAConnect(s: TSOCKET; name: psockaddr;namelen: u_int;1274 lpCallerData: PWSABUF; lpCalleeData: PWSABUF; lpSQOS: PQOS;1275 lpGQOS: PQOS) : u_int; stdcall;1276 function WSACreateEvent:WSAEVENT; stdcall;1277 1278 {$IFDEF UNICODE}1279 function WSADuplicateSocket(s: TSocket; dwProcessId: DWORD;1280 lpProtocolInfo: PWSAPROTOCOL_INFOW): u_int; stdcall;1281 {$ELSE}1282 function WSADuplicateSocket(s: TSocket; dwProcessId: DWORD;1283 lpProtocolInfo: PWSAPROTOCOL_INFOA): u_int; stdcall;1284 {$ENDIF} {UNICODE}1285 1286 function WSAEnumNetworkEvents( s: TSocket; hEventObject: WSAEVENT;1287 lpNetworkEvents: PWSANETWORKEVENTS): u_int; stdcall;1288 1289 {$IFDEF UNICODE}1290 function WSAEnumProtocols( lpiProtocols: PInt;1291 lpProtocolBuffer: PWSAPROTOCOL_INFOW; lpdwBufferLength: PDWORD1292 ): u_int; stdcall;1293 {$ELSE}1294 function WSAEnumProtocols( lpiProtocols: PInt;1295 lpProtocolBuffer: PWSAPROTOCOL_INFOA; lpdwBufferLength: PDWORD1296 ): u_int; stdcall;1297 {$ENDIF} {UNICODE}1298 1299 function WSAEventSelect(s: TSOCKET; hEventObject: WSAEVENT;1300 lNetworkEvents: u_long): u_int; stdcall;1301 function WSAGetOverlappedResult( s: TSocket; lpOverlapped: LPWSAOVERLAPPED;1302 lpcbTransfer: PDWORD; fWait: BOOL; lpdwFlags: PDWORD): BOOL; stdcall;1303 function WSAGetQOSByName( s: TSocket; lpQOSName: PWSABUF;1304 lpQOS: PQOS): BOOL; stdcall;1305 function WSAHtonl( s: TSocket; hostlong: u_long; lpnetlong: pu_long)1306 : u_int; stdcall;1307 function WSAHtons( s: TSocket; hostshort: u_short; lpnetshort: pu_short)1308 : u_int; stdcall;1309 function WSAIoctl( s: TSocket; dwIoControlCode: DWORD; lpvInBuffer: Pointer;1310 cbInBuffer: DWORD; lpvOutBuffer: Pointer; cbOutBuffer: DWORD;1311 lpcbBytesReturned: PDWORD; lpOverlapped: LPWSAOVERLAPPED;1312 lpCompletionRoutine: PWSAOVERLAPPED_COMPLETION_ROUTINE): u_int; stdcall;1313 function WSAJoinLeaf( s: TSocket; name: psockaddr; namelen: u_int;1314 lpCallerData: PWSABUF; lpCalleeData: PWSABUF; lpSQOS: PQOS; lpGQOS: PQOS;1315 dwFlags: DWORD): TSocket; stdcall;1316 function WSANtohl( s: TSocket; netlong: u_long; lphostlong: pu_long)1317 : u_int; stdcall;1318 function WSANtohs( s: TSocket; netshort: u_short; lphostshort: pu_short)1319 : u_int; stdcall;1320 function WSARecv( s: TSocket; lpBuffers: PWSABUF; dwBufferCount: DWORD;1321 lpNumberOfBytesRecvd: PDWORD; lpFlags: PDWORD;lpOverlapped: LPWSAOVERLAPPED;1322 lpCompletionRoutine: PWSAOVERLAPPED_COMPLETION_ROUTINE): u_int; stdcall;1323 function WSARecvDisconnect( s: TSocket; lpInboundDisconnectData: PWSABUF)1324 : u_int; stdcall;1325 function WSARecvFrom( s: TSocket; lpBuffers: PWSABUF; dwBufferCount: DWORD;1326 lpNumberOfBytesRecvd: PDWORD; lpFlags: PDWORD; lpFrom: psockaddr;1327 lpFromlen: PInt; lpOverlapped: LPWSAOVERLAPPED;1328 lpCompletionRoutine: PWSAOVERLAPPED_COMPLETION_ROUTINE): u_int; stdcall;1329 function WSAResetEvent( hEvent: WSAEVENT): BOOL; stdcall;1330 function WSASend( s: TSocket; lpBuffers: PWSABUF; dwBufferCount: DWORD;1331 lpNumberOfBytesSent: PDWORD; dwFlags: DWORD; lpOverlapped: LPWSAOVERLAPPED;1332 lpCompletionRoutine: PWSAOVERLAPPED_COMPLETION_ROUTINE): u_int; stdcall;1333 function WSASendDisconnect( s: TSocket; lpOutboundDisconnectData: PWSABUF)1334 : u_int; stdcall;1335 function WSASendTo(s: TSocket; lpBuffers: PWSABUF; dwBufferCount: DWORD;1336 lpNumberOfBytesSent: PDWORD;dwFlags: DWORD; lpTo: psockaddr; iTolen: u_int;1337 lpOverlapped: LPWSAOVERLAPPED;1338 lpCompletionRoutine: PWSAOVERLAPPED_COMPLETION_ROUTINE): u_int; stdcall;1339 function WSASetEvent( hEvent: WSAEVENT): BOOL; stdcall;1340 {$IFDEF UNICODE}1341 function WSASocket( af: u_int; atype: u_int; protocol: u_int;1342 lpProtocolInfo: PWSAPROTOCOL_INFOW; g: TGroup; dwFlags: Dword)1343 : TSocket; stdcall;1344 {$ELSE}1345 function WSASocket( af: u_int; atype: u_int; protocol: u_int;1346 lpProtocolInfo: PWSAPROTOCOL_INFOA; g: TGroup; dwFlags: Dword)1347 : TSocket; stdcall;1348 {$ENDIF} { UNICODE}1349 function WSAWaitForMultipleEvents( cEvents: DWORD; lphEvents: LPWSAEVENT;1350 fWaitAll: BOOL; dwTimeout: DWORD; fAlertable: BOOL): DWORD; stdcall;1351 {$IFDEF UNICODE}1352 function WSAAddressToString( lpsaAddress: PSOCKADDR; dwAddressLength: DWORD;1353 lpProtocolInfo: PWSAPROTOCOL_INFOW; lpszAddressString: WideChar;1354 lpdwAddressStringLength: PDWORD): u_int; stdcall;1355 {$ELSE}1356 function WSAAddressToString( lpsaAddress: PSOCKADDR; dwAddressLength: DWORD;1357 lpProtocolInfo: PWSAPROTOCOL_INFOA; lpszAddressString: PChar;1358 lpdwAddressStringLength: PDWORD): u_int; stdcall;1359 {$ENDIF} { UNICODE}1360 1361 {$IFDEF UNICODE}1362 function WSAStringToAddress( AddressString: WideChar; AddressFamily: u_int;1363 lpProtocolInfo: PWSAPROTOCOL_INFOW; lpAddress: PSOCKADDR;1364 lpAddressLength: PInt): u_int; stdcall;1365 {$ELSE}1366 function WSAStringToAddress( AddressString: PChar; AddressFamily: u_int;1367 lpProtocolInfo: PWSAPROTOCOL_INFOA; lpAddress: PSOCKADDR;1368 lpAddressLength: PInt): u_int; stdcall;1369 {$ENDIF} { UNICODE}1370 1371 { Registration and Name Resolution API functions }1372 1373 {$IFDEF UNICODE}1374 function WSALookupServiceBegin( lpqsRestrictions: PWSAQUERYSETW;1375 dwControlFlags: DWORD; lphLookup: LPHANDLE): u_int; stdcall;1376 {$ELSE}1377 function WSALookupServiceBegin( lpqsRestrictions: PWSAQUERYSETA;1378 dwControlFlags: DWORD; lphLookup: LPHANDLE): u_int; stdcall;1379 {$ENDIF} { UNICODE}1380 1381 {$IFDEF UNICODE}1382 function WSALookupServiceNext( hLookup: THANDLE; dwControlFlags: DWORD;1383 lpdwBufferLength: PDWORD; lpqsResults: PWSAQUERYSETW): u_int; stdcall;1384 {$ELSE}1385 function WSALookupServiceNext( hLookup: THANDLE; dwControlFlags: DWORD;1386 lpdwBufferLength: PDWORD; lpqsResults: PWSAQUERYSETA): LongInt; stdcall;1387 {$ENDIF} { UNICODE}1388 1389 function WSALookupServiceEnd( hLookup: THandle): u_int; stdcall;1390 1391 {$IFDEF UNICODE}1392 function WSAInstallServiceClass( lpServiceClassInfo: PWSASERVICECLASSINFOW1393 ): u_int; stdcall;1394 {$ELSE}1395 function WSAInstallServiceClass( lpServiceClassInfo: PWSASERVICECLASSINFOA1396 ): u_int; stdcall;1397 {$ENDIF} { UNICODE}1398 1399 function WSARemoveServiceClass(lpServiceClassId: PGUID): u_int; stdcall;1400 1401 {$IFDEF UNICODE}1402 function WSAGetServiceClassInfo( lpProviderId: PGUID; lpServiceClassId: PGUID;1403 lpdwBufSize: PDWORD; lpServiceClassInfo: PWSASERVICECLASSINFOW)1404 : u_int; stdcall;1405 {$ELSE}1406 function WSAGetServiceClassInfo( lpProviderId: PGUID; lpServiceClassId: PGUID;1407 lpdwBufSize: PDWORD; lpServiceClassInfo: PWSASERVICECLASSINFOA)1408 : u_int; stdcall;1409 {$ENDIF} { UNICODE}1410 1411 {$IFDEF UNICODE}1412 function WSAEnumNameSpaceProviders( lpdwBufferLength: PDWORD;1413 lpnspBuffer: PWSANAMESPACE_INFOW):u_int; stdcall;1414 {$ELSE}1415 function WSAEnumNameSpaceProviders( lpdwBufferLength: PDWORD;1416 lpnspBuffer: PWSANAMESPACE_INFOA):u_int; stdcall;1417 {$ENDIF} { UNICODE}1418 1419 {$IFDEF UNICODE}1420 function WSAGetServiceClassNameByClassId( lpServiceClassId: PGUID;1421 lpszServiceClassName: WideChar; lpdwBufferLength: PDWORD): u_int; stdcall;1422 {$ELSE}1423 function WSAGetServiceClassNameByClassId( lpServiceClassId: PGUID;1424 lpszServiceClassName: PChar; lpdwBufferLength: PDWORD): u_int; stdcall;1425 {$ENDIF} { UNICODE}1426 1427 {$IFDEF UNICODE}1428 function WSASetService( lpqsRegInfo: PWSAQUERYSETW;1429 essoperation: TWSAESETSERVICEOP; dwControlFlags: DWORD):u_int; stdcall;1430 {$ELSE}1431 function WSASetService( lpqsRegInfo: PWSAQUERYSETA;1432 essoperation: TWSAESETSERVICEOP; dwControlFlags: DWORD):u_int; stdcall;1433 {$ENDIF} { UNICODE}1434 1435 function WSARecvEx(s: TSocket; var buf; len: Integer; var flags: Integer): Integer; stdcall;1436 1437 function TransmitFile(hSocket: TSocket; hFile: THandle; nNumberOfBytesToWrite: DWORD;1438 nNumberOfBytesPerSend: DWORD; lpOverlapped: POverlapped;1439 lpTransmitBuffers: PTransmitFileBuffers; dwReserved: DWORD): BOOL; stdcall;1440 1441 function WSAMakeASyncReply(Buflen, Error: Word): Longint;1442 function WSAMakeSelectReply(Event, Error: Word): Longint;1443 function WSAGetAsyncBuflen(Param: Longint): Word;1444 function WSAGetAsyncError(Param: Longint): Word;1445 function WSAGetSelectEvent(Param: Longint): Word;1446 function WSAGetSelectError(Param: Longint): Word;1447 1448 implementation1449 1450 const1451 winsocket = ‘ws2_32.dll‘;1452 1453 function WSAMakeASyncReply;1454 begin1455 WSAMakeASyncReply:= MakeLong(Buflen, Error);1456 end;1457 1458 function WSAMakeSelectReply;1459 begin1460 WSAMakeSelectReply:= MakeLong(Event, Error);1461 end;1462 1463 function WSAGetAsyncBuflen;1464 begin1465 WSAGetAsyncBuflen:= LOWORD(Param);1466 end;1467 1468 function WSAGetAsyncError;1469 begin1470 WSAGetAsyncError:= HIWORD(Param);1471 end;1472 1473 function WSAGetSelectEvent;1474 begin1475 WSAGetSelectEvent:= LOWORD(Param);1476 end;1477 1478 function WSAGetSelectError;1479 begin1480 WSAGetSelectError:= HIWORD(Param);1481 end;1482 1483 function accept;1484 external winsocket name ‘accept‘;1485 function bind;1486 external winsocket name ‘bind‘;1487 function closesocket;1488 external winsocket name ‘closesocket‘;1489 function connect;1490 external winsocket name ‘connect‘;1491 function getpeername;1492 external winsocket name ‘getpeername‘;1493 function getsockname;1494 external winsocket name ‘getsockname‘;1495 function getsockopt;1496 external winsocket name ‘getsockopt‘;1497 function htonl;1498 external winsocket name ‘htonl‘;1499 function htons;1500 external winsocket name ‘htons‘;1501 function inet_addr;1502 external winsocket name ‘inet_addr‘;1503 function inet_ntoa;1504 external winsocket name ‘inet_ntoa‘;1505 function ioctlsocket;1506 external winsocket name ‘ioctlsocket‘;1507 function listen;1508 external winsocket name ‘listen‘;1509 function ntohl;1510 external winsocket name ‘ntohl‘;1511 function ntohs;1512 external winsocket name ‘ntohs‘;1513 function recv;1514 external winsocket name ‘recv‘;1515 function recvfrom;1516 external winsocket name ‘recvfrom‘;1517 function select;1518 external winsocket name ‘select‘;1519 function send;1520 external winsocket name ‘send‘;1521 function sendto;1522 external winsocket name ‘sendto‘;1523 function setsockopt;1524 external winsocket name ‘setsockopt‘;1525 function shutdown;1526 external winsocket name ‘shutdown‘;1527 function socket;1528 external winsocket name ‘socket‘;1529 1530 function gethostbyaddr;1531 external winsocket name ‘gethostbyaddr‘;1532 function gethostbyname;1533 external winsocket name ‘gethostbyname‘;1534 function getprotobyname;1535 external winsocket name ‘getprotobyname‘;1536 function getprotobynumber;1537 external winsocket name ‘getprotobynumber‘;1538 function getservbyname;1539 external winsocket name ‘getservbyname‘;1540 function getservbyport;1541 external winsocket name ‘getservbyport‘;1542 function gethostname;1543 external winsocket name ‘gethostname‘;1544 1545 function WSAAsyncSelect;1546 external winsocket name ‘WSAAsyncSelect‘;1547 function WSARecvEx;1548 external winsocket name ‘WSARecvEx‘;1549 function WSAAsyncGetHostByAddr;1550 external winsocket name ‘WSAAsyncGetHostByAddr‘;1551 function WSAAsyncGetHostByName;1552 external winsocket name ‘WSAAsyncGetHostByName‘;1553 function WSAAsyncGetProtoByNumber;1554 external winsocket name ‘WSAAsyncGetProtoByNumber‘;1555 function WSAAsyncGetprotoByName;1556 external winsocket name ‘WSAAsyncGetprotoByName‘;1557 function WSAAsyncGetServByPort;1558 external winsocket name ‘WSAAsyncGetServByPort‘;1559 function WSAAsyncGetServByName;1560 external winsocket name ‘WSAAsyncGetServByName‘;1561 function WSACancelAsyncRequest;1562 external winsocket name ‘WSACancelAsyncRequest‘;1563 function WSASetBlockingHook;1564 external winsocket name ‘WSASetBlockingHook‘;1565 function WSAUnhookBlockingHook;1566 external winsocket name ‘WSAUnhookBlockingHook‘;1567 function WSAGetLastError;1568 external winsocket name ‘WSAGetLastError‘;1569 procedure WSASetLastError;1570 external winsocket name ‘WSASetLastError‘;1571 function WSACancelBlockingCall;1572 external winsocket name ‘WSACancelBlockingCall‘;1573 function WSAIsBlocking;1574 external winsocket name ‘WSAIsBlocking‘;1575 function WSAStartup;1576 external winsocket name ‘WSAStartup‘;1577 function WSACleanup;1578 external winsocket name ‘WSACleanup‘;1579 1580 {$IFDEF UNICODE}1581 function WSASetService;1582 external winsocket name ‘WSASetServiceW‘;1583 function WSAGetServiceClassNameByClassId;1584 external winsocket name ‘WSAGetServiceClassNameByClassIdW‘;1585 function WSAEnumNameSpaceProviders;1586 external winsocket name ‘WSAEnumNameSpaceProvidersW‘;1587 function WSAGetServiceClassInfo;1588 external winsocket name ‘WSAGetServiceClassInfoW‘;1589 function WSAInstallServiceClass;1590 external winsocket name ‘WSAInstallServiceClassW‘;1591 function WSALookupServiceNext;1592 external winsocket name ‘WSALookupServiceNextW‘;1593 function WSALookupServiceBegin;1594 external winsocket name ‘WSALookupServiceBeginW‘;1595 function WSAStringToAddress;1596 external winsocket name ‘WSAStringToAddressW‘;1597 function WSAAddressToString;1598 external winsocket name ‘WSAAddressToStringW‘;1599 function WSASocket;1600 external winsocket name ‘WSASocketW‘;1601 function WSAEnumProtocols;1602 external winsocket name ‘WSAEnumProtocolsW‘;1603 function WSADuplicateSocket;1604 external winsocket name ‘WSADuplicateSocketW‘;1605 {$ELSE}1606 function WSASetService;1607 external winsocket name ‘WSASetServiceA‘;1608 function WSAGetServiceClassNameByClassId;1609 external winsocket name ‘WSAGetServiceClassNameByClassIdA‘;1610 function WSAEnumNameSpaceProviders;1611 external winsocket name ‘WSAEnumNameSpaceProvidersA‘;1612 function WSAGetServiceClassInfo;1613 external winsocket name ‘WSAGetServiceClassInfoA‘;1614 function WSAInstallServiceClass;1615 external winsocket name ‘WSAInstallServiceClassA‘;1616 function WSALookupServiceNext;1617 external winsocket name ‘WSALookupServiceNextA‘;1618 function WSALookupServiceBegin;1619 external winsocket name ‘WSALookupServiceBeginA‘;1620 function WSAStringToAddress;1621 external winsocket name ‘WSAStringToAddressA‘;1622 function WSAAddressToString;1623 external winsocket name ‘WSAAddressToStringA‘;1624 function WSASocket;1625 external winsocket name ‘WSASocketA‘;1626 function WSAEnumProtocols;1627 external winsocket name ‘WSAEnumProtocolsA‘;1628 function WSADuplicateSocket;1629 external winsocket name ‘WSADuplicateSocketA‘;1630 {$ENDIF} { UNICODE}1631 1632 1633 function WSALookupServiceEnd;1634 external winsocket name ‘WSALookupServiceEnd‘;1635 function WSARemoveServiceClass;1636 external winsocket name ‘WSARemoveServiceClass‘;1637 function WSAWaitForMultipleEvents;1638 external winsocket name ‘WSAWaitForMultipleEvents‘;1639 function WSASetEvent;1640 external winsocket name ‘WSASetEvent‘;1641 function WSASendTo;1642 external winsocket name ‘WSASendTo‘;1643 function WSASendDisconnect;1644 external winsocket name ‘WSASendDisconnect‘;1645 function WSASend;1646 external winsocket name ‘WSASend‘;1647 function WSAResetEvent;1648 external winsocket name ‘WSAResetEvent‘;1649 function WSARecvFrom;1650 external winsocket name ‘WSARecvFrom‘;1651 function WSARecvDisconnect;1652 external winsocket name ‘WSARecvDisconnect‘;1653 function WSARecv;1654 external winsocket name ‘WSARecv‘;1655 function WSAIoctl;1656 external winsocket name ‘WSAIoctl‘;1657 function WSAJoinLeaf;1658 external winsocket name ‘WSAJoinLeaf‘;1659 function WSANtohl;1660 external winsocket name ‘WSANtohl‘;1661 function WSANtohs;1662 external winsocket name ‘WSANtohs‘;1663 function WSAHtons;1664 external winsocket name ‘WSAHtons‘;1665 function WSAHtonl;1666 external winsocket name ‘WSAHtonl‘;1667 function WSAGetQOSByName;1668 external winsocket name ‘WSAGetQOSByName‘;1669 function WSAGetOverlappedResult;1670 external winsocket name ‘WSAGetOverlappedResult‘;1671 function WSAEventSelect;1672 external winsocket name ‘WSAEventSelect‘;1673 function WSAEnumNetworkEvents;1674 external winsocket name ‘WSAEnumNetworkEvents‘;1675 function WSACreateEvent;1676 external winsocket name ‘WSACreateEvent‘;1677 function WSAConnect;1678 external winsocket name ‘WSAConnect‘;1679 function WSACloseEvent;1680 external winsocket name ‘WSACloseEvent‘;1681 function WSAAccept;1682 external winsocket name ‘WSAAccept‘;1683 1684 function TransmitFile;1685 external winsocket name ‘TransmitFile‘;1686 1687 end.
参考资料:
【完成端口详解】http://blog.csdn.net/piggyxp/article/details/6922277
【TCP的那些事】http://coolshell.cn/articles/11564.html
IOCP扩展方法AcceptEx, DisconnectEx, GetAcceptExSockaddr用法示例