delphi - TServerSocket 和 TClientSocket

标签 delphi

我使用此代码来接收数据: 但这不行。你能帮我吗?

procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
  Socket: TCustomWinSocket);
var
  i:integer;
  sRec : string;
begin
  for i := 0 to ServerSocket1.Socket.ActiveConnections-1 do
  begin
    with ServerSocket1.Socket.Connections[i] do
    begin
      sRec:=ReceiveText;
      if sRec <> '' then
      begin
        if RemoteAddress='192.168.0.1' then
        begin
          if ReceiveText='1' then
            Btn1.Color:=clNavy;
          ADOQuery1.Active:=True;
        end;
        if RemoteAddress='192.168.0.1' then
        begin
          if ReceiveText='2' then
            Btn1.Color:=clRed;
          Pnl1.Visible:=True;
        end;
      end;
    end;
  end;
end;

最佳答案

每当任何客户端发送数据时,您都会尝试从 TServerSocket.Socket.Connections 列表中的每个客户端连接读取数据。您应该改用事件提供的 TCustomWinSocket 参数。它告诉您正在发送数据的确切客户端。

您的代码中还存在一些其他逻辑错误。

试试这个:

procedure TForm1.ServerSocket1ClientRead(Sender: TObject; Socket: TCustomWinSocket);
var
  sRec : string;
begin
  sRec := Socket.ReceiveText;
  if sRec <> '' then
  begin
    if Socket.RemoteAddress = '192.168.0.1' then
    begin
      if sRec = '1' then Btn1.Color := clNavy;
      ADOQuery1.Active := True;
      if sRec = '2' then Btn1.Color := clRed;
      Pnl1.Visible := True;
    end;
  end;
end; 

或者也许您的意思更像是这样?

procedure TForm1.ServerSocket1ClientRead(Sender: TObject; Socket: TCustomWinSocket);
var
  sRec : string;
begin
  sRec := Socket.ReceiveText;
  if sRec <> '' then
  begin
    if Socket.RemoteAddress = '192.168.0.1' then
    begin
      if sRec = '1' then begin
        Btn1.Color := clNavy;
        ADOQuery1.Active := True;
      end
      else if sRec = '2' then begin
        Btn1.Color := clRed;
        Pnl1.Visible := True;
      end;
    end;
  end;
end; 

关于delphi - TServerSocket 和 TClientSocket,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4608533/

相关文章:

delphi - 如何在Delphi中有效地使用接口(interface)进行内存管理

delphi - 依靠 DeHL 进行新项目安全吗?

multithreading - 线程代码在阻塞循环中不稳定

android - 使用 Firemonkey/Delphi 打开 Android 26 PDF 文件时出现异常

delphi - EnumerateTraceGuids 返回 "The parameter is incorrect"(87)

Delphi 7、DUnit 和 FastMM 错误地报告字符串

image - 无法使用 postgreSQL lo_export 和 delphi 7 创建服务器文件 "c:/myimage.jpg"

delphi - 在 Delphi 中,当其父控件获得和失去焦点时,如何通知控件?

delphi - 为什么当我尝试实现接口(interface)时收到 "Invalid pointer operation"?

delphi - Delphi调试器?