TServerSocket和TClientSocket

时间:2011-01-05 19:51:08

标签: 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;

2 个答案:

答案 0 :(得分:3)

您尝试在任何客户端发送数据时从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; 

答案 1 :(得分:-1)

用Socket param替换ServerSocket.Socket并再次测试。当然要删除for循环

相关问题