Обрабатываю сообщения от телнет-сокета
try
Event := WSACreateEvent;
WSAEventSelect(FSock, Event, FD_CONNECT or FD_READ or FD_WRITE or FD_CLOSE);
PeekMessage(msg, 0, WM_USER, WM_USER, PM_NOREMOVE);
Connect(FSock, @Addr, Sizeof(Addr));
while not Terminated do
begin
case MsgWaitForMultipleObjects(1, Event, False, INFINITE, QS_ALLINPUT) of
WAIT_TIMEOUT: ;
WAIT_OBJECT_0:
begin
EResult := WSAEnumNetworkEvents(FSock, Event, @Ev);
if EResult <> 0 then
Break;
if GetEvent(Ev.lNetworkEvents, FD_READ) then
begin
if Ev.iErrorCode[FD_READ_BIT] <> 0 then
begin
if Assigned(FOnErrorEvent) then
FOnErrorEvent(Self, Ev.iErrorCode[FD_WRITE_BIT]);
if Assigned(FOnNeedSynchAfterEvent) then
Synchronize(OnNeedSynchAfterProcedure);
Break;
end
else
begin
ioctlsocket(FSock, FIONREAD, Size);
SetLength(Buff, Size);
Recv(FSock, Buff[0], Size, 0);
if Assigned(FOnReadEvent) then
FOnReadEvent(Self, Buff, Size);
if Assigned(FOnAfterReadEvent) then
Synchronize(OnAfterReadProcedure);
end;
end;
if GetEvent(Ev.lNetworkEvents, FD_CONNECT) then
begin
if Ev.iErrorCode[FD_CONNECT_BIT] <> 0 then
begin
if Assigned(FOnErrorEvent) then
FOnErrorEvent(Self, Ev.iErrorCode[FD_WRITE_BIT]);
if Assigned(FOnNeedSynchAfterEvent) then
Synchronize(OnNeedSynchAfterProcedure);
Break;
end
else
begin
FSocketState := wsConnected;
if Assigned(FOnConnectEvent) then
FOnConnectEvent(Self);
if Assigned(FOnNeedSynchAfterEvent) then
Synchronize(OnNeedSynchAfterProcedure);
end;
end;
if GetEvent(Ev.lNetworkEvents, FD_CLOSE) then
begin
if Ev.iErrorCode[FD_CLOSE_BIT] <> 0 then
begin
if Assigned(FOnErrorEvent) then
FOnErrorEvent(Self, Ev.iErrorCode[FD_WRITE_BIT]);
if Assigned(FOnNeedSynchAfterEvent) then
Synchronize(OnNeedSynchAfterProcedure);
Break;
end
else
begin
FSocketState := wsClosed;
if Assigned(FOnDisconnectEvent) then
FOnDisconnectEvent(Self);
if Assigned(FOnNeedSynchAfterEvent) then
Synchronize(OnNeedSynchAfterProcedure);
Break;
end;
end;
end;
События FD_CONNECT, FD_READ - происходят. Но когда даешь комманду отсоединиться от сокета - отсоединение происходит, а событие FD_CLOSE не происходит. В чем может быть причина?