在Delphi7中通过TCP发送消息

在Delphi7中通过TCP发送消息,delphi,delphi-7,Delphi,Delphi 7,我一直试图在tcp上发送消息,但这些代码似乎在Delphi7上出现了奇怪的错误,我在DelphiXE上尝试了类似的代码,效果很好。我在XE和Delphi 7上都使用Indy 10 type TClient = class(TIdContext) PeerIP : String; RcvdMsg : String; procedure SendResponse(const AResponse: String); end;

我一直试图在tcp上发送消息,但这些代码似乎在Delphi7上出现了奇怪的错误,我在DelphiXE上尝试了类似的代码,效果很好。我在XE和Delphi 7上都使用Indy 10

type
  TClient   = class(TIdContext) 
    PeerIP      : String;          
    RcvdMsg     : String;

    procedure SendResponse(const AResponse: String);
  end;

procedure TForm1.IdTCPServer1Connect(AContext: TIdContext);
var
  NewClient:   TClient;
begin
  with TClient(AContext) do
  begin
    NewClient.PeerIP      := Connection.Socket.Binding.PeerIP;
    NewClient.RcvdMsg     := Connection.Socket.ReadLn;
  end;
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
var
  Context: TClient;
  List: TList;
  I: Integer;
begin

  List := IdTCPServer1.Contexts.LockList;
  try
    for I := 0 to List.Count-1 do
    begin
      Context := TClient(List[I]);
      MessageBox(0,pChar(Context.PeerIP),0,0); // shows wierd string
 (*     if (Context.PeerIP = IP) then
      begin 
        //didn't get to here
        Context.SendResponse('msg');
        Break;
      end              *)

    end;
  finally
    IdTCPServer1.Contexts.UnlockList;
  end;
end;

procedure TForm1.IdTCPServer1Connect(AContext: TIdContext);
var
  NewClient:   TClient;
begin
  with TClient(AContext) do
  begin
    NewClient.PeerIP      := Connection.Socket.Binding.PeerIP;
    NewClient.RcvdMsg     := Connection.Socket.ReadLn;
  end;
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
var
  Context: TClient;
  List: TList;
  I: Integer;
begin

  List := IdTCPServer1.Contexts.LockList;
  try
    for I := 0 to List.Count-1 do
    begin
      Context := TClient(List[I]);
      MessageBox(0,pChar(Context.PeerIP),0,0); // shows wierd string
 (*     if (Context.PeerIP = IP) then
      begin 
        //didn't get to here
        Context.SendResponse('msg');
        Break;
      end              *)

    end;
  finally
    IdTCPServer1.Contexts.UnlockList;
  end;
end;
有办法解决吗

编辑:

type
      TClient   = class(TIdServerContext) 
        PeerIP      : String;          
        RcvdMsg     : String;

        procedure SendResponse(const AResponse: String);
      end;

procedure TForm1.FormCreate(Sender: TObject);
begin

  IdTCPServer1.Bindings.Add.Port := 1234;
  IdTCPServer1.Active := not IdTCPServer1.Active;
  IdTCPServer1.ContextClass := TClient;
end;
我仍然无法发送消息

procedure TForm1.BitBtn1Click(Sender: TObject);
    var
      Context: TClient;
      List: TList;
      I: Integer;
    begin

      List := IdTCPServer1.Contexts.LockList;
      try
        for I := 0 to List.Count-1 do
        begin
          Context := TClient(List[I]);
          MessageBox(0,pChar(Context.PeerIP),0,0); // blank
     (*     if (Context.PeerIP = IP) then
          begin 
            //didn't get to here
            Context.SendResponse('msg');
            Break;
          end              *)

        end;
      finally
        IdTCPServer1.Contexts.UnlockList;
      end;
    end;

TClient
需要派生自
TIdServerContext
,而不是
TIdContext
。如果尚未激活服务器,请确保在激活服务器之前分配了
TIdTCPServer.ContextClass
属性,否则您的类型转换将无效:

type
  TClient = class(TIdServerContext)
    ...
  end;


输出真的是“奇怪的字符串”吗?我的意思是奇怪的ascii字符,或者我不知道你们怎么称呼它谢谢你们的帮助,我实际上尝试了
TIdServerContext
,但结果是一样的,如果添加
IdTCPServer1.ContextClass:=TClient
…我编辑了我的帖子您必须在激活服务器之前设置
ContextClass
,否则在更新
ContextClass
以使用自定义类之前,您可能会让客户端连接到服务器并最终使用默认的
TIdServerContext
类。