Delphi TServerSocket 发送消息之前检查客户端是否仍处于连接状态

TServerSocket:如何在发送消息之前检查ListView上的特定客户端是否仍处于连接状态?

无需检查连接。如果客户端实际上已断开连接,则在触发处理程序Connections[]时,该客户端将不再位于服务器列表中OnTimer。您应该为OnClientDisconnect分配了处理程序,以TServerSocket从中删除客户端TListView。

如果由于某种原因,客户端仍在Connections[]列表中(即,由于基础连接已丢失但TServerSocket尚未检测到),则套接字将仅缓存所有传出数据,直到其出站缓冲区填满为止,然后将开始WSAWOULDBLOCK为每个发送返回错误。最终,操作系统将使死连接超时并TServerSocket从Connections[]列表中将其删除,从而触发OnClientDisconnect事件。

至少,在显示的代码中,应将send循环更新为Close()实际上无法发送的任何套接字,从而触发OnClientDisconnect事件以从中删除该客户端TListView,例如:

procedure TMainForm.ServerSocket1ClientConnect(Sender: TObject; Socket: TCustomWinSocket);

var

  Item: TListItem;

begin

  Item := ListView1.Items.Add;

  Item.Data := Socket;

  ...

end;

 

procedure TMainForm.ServerSocket1ClientDisconnect(Sender: TObject; Socket: TCustomWinSocket);

var

  Item: TListItem;

begin

  Item := ListView1.FindData(0, Socket, True, False);

  if Item <> nil then

    Item.Delete;

end;

 

procedure TMainForm.Timer1Timer(Sender: TObject);

const

  s: AnsiString = 'ping' + #13#10;

var

  Item: TListItem;

  Socket: TCustomWinSocket;

  p: PAnsiChar;

  i, len, sent: Integer;

begin

  for i := 0 to ListView1.Items.Count - 1 do

  begin

    Item := ListView1.Items[i];

    Item.SubItems.Objects[2] := TObject(GetTickCount);

    Socket := TCustomWinSocket(Item.Data);

    try

      // SendText() does not handle partial sends, or Unicode strings...

      //Socket.SendText('ping' + #13#10);

      p := PAnsiChar(s);

      len := Length(s);

      repeat

        sent := Socket.SendBuf(p^, len);

        if sent = -1 then

        being

          if WSAGetLastError() <> WSAEWOULDBLOCK then

            Break;

          // TODO: stop trying after several attempts fail...

          Continue;

        end;

        Inc(p, sent);

        Dec(len, sent);

      until len = 0;

      if len = 0 then

        Continue;

    except

    end;

    Socket.Close;

  end;

end;

 

posted @ 2022-05-20 19:26  朱颂东  阅读(333)  评论(0编辑  收藏  举报