扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:delphilxh 来源:Delphi先锋网 2007年10月31日
关键字:
procedure TListenThread.Execute; var hEvent : WSAEvent; ret : Integer; ne : TWSANetworkEvents; sock : TSocket; adr : TSockAddrIn; sMsg : String; Index, EventTotal : DWORD; EventArray : Array [0..WSA_MAXIMUM_WAIT_EVENTS-1] of WSAEVENT; begin ...socket...bind... hEvent := WSACreateEvent(); WSAEventSelect( ListenSock, hEvent, FD_ACCEPT or FD_CLOSE ); ...listen... while ( not Terminated ) do begin Index := WSAWaitForMultipleEvents( EventTotal, @EventArray[0], FALSE, WSA_INFINITE, FALSE ); FillChar( ne, sizeof(ne), 0 ); WSAEnumNetworkEvents( SockArray[Index-WSA_WAIT_EVENT_0], EventArray[Index-WSA_WAIT_EVENT_0], @ne ); if ( ne.lNetworkEvents and FD_ACCEPT ) > 0 then begin if ne.iErrorCode[FD_ACCEPT_BIT] <> 0 then continue; ret := sizeof(adr); sock := accept( SockArray[Index-WSA_WAIT_EVENT_0], adr, ret ); if EventTotal > WSA_MAXIMUM_WAIT_EVENTS-1 then//这里WSA_MAXIMUM_WAIT_EVENTS同样是64 begin closesocket( sock ); continue; end; hEvent := WSACreateEvent(); WSAEventSelect( sock, hEvent, FD_READ or FD_WRITE or FD_CLOSE ); SockArray[EventTotal] := sock; EventArray[EventTotal] := hEvent; Inc( EventTotal ); end; if ( ne.lNetworkEvents and FD_READ ) > 0 then begin if ne.iErrorCode[FD_READ_BIT] <> 0 then continue; FillChar( RecvBuf[0], PACK_SIZE_RECEIVE, 0 ); ret := recv( SockArray[Index-WSA_WAIT_EVENT_0], RecvBuf[0], PACK_SIZE_RECEIVE, 0 ); ...... end; end; end; |
procedure TOverlapThread.Execute; var dwTemp : DWORD; ret : Integer; Index : DWORD; begin ...... while ( not Terminated ) do begin Index := WSAWaitForMultipleEvents( FLinks.Count, @FLinks.Events[0], FALSE, RECV_TIME_OUT, FALSE ); Dec( Index, WSA_WAIT_EVENT_0 ); if Index > WSA_MAXIMUM_WAIT_EVENTS-1 then //超时或者其他错误 continue; WSAResetEvent( FLinks.Events[Index] ); WSAGetOverlappedResult( FLinks.Sockets[Index], FLinks.pOverlaps[Index], @dwTemp, FALSE,FLinks.pdwFlags[Index]^ ); if dwTemp = 0 then //连接已经关闭 begin ...... continue; end else begin fmMain.ListBox1.Items.Add( FLinks.pBufs[Index]^.buf ); end; //初始化缓冲区 FLinks.pdwFlags[Index]^ := 0; FillChar( FLinks.pOverlaps[Index]^, sizeof(WSAOVERLAPPED), 0 ); FLinks.pOverlaps[Index]^.hEvent := FLinks.Events[Index]; FillChar( FLinks.pBufs[Index]^.buf^, BUFFER_SIZE, 0 ); //递一个接收数据请求 WSARecv( FLinks.Sockets[Index], FLinks.pBufs[Index], 1, FLinks.pdwRecvd[Index]^, FLinks.pdwFlags[Index]^, FLinks.pOverlaps[Index], nil ); end; end; |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者