简体   繁体   中英

How to set up a simple connection between TIdTcpClient and TIdTcpServer to send a string from client to server?

I am using Delphi xe6 to impelement a simple client/server connection. The client form should have a TEdit component and should sent Edit.text string to server memo. I want to use Indy components: TIdTcpServer and TIdTcpClient but I don't know how to setup a simple connection between client and server.

I would appreciate your help.

Server:
Anywhere in your Create, Initialize function:

FIndySrv:=TIdTCPServer.Create(nil);
FIndySrv.DefaultPort:=50000;
FIndySrv.OnExecute:=DoOnIndyExecute;
FIndySrv.Active:=true;

The OnExecute:

procedure TForm1.DoOnIndyExecute(AContext: TIdContext);
var recv:string;
begin
  recv := AContext.Connection.Socket.ReadLn;
  // ...
end;

Client:

FIndyClient:=TIdTCPClient.Create(nil);
FIndyClient.Host:='localhost';
FIndyClient.Port:=50000;
FIndyClient.Connect;
FIndyClient.Socket.WriteLn('Hallo Welt!');

Since the question is particular asking how to send from a VCL Component to another VCL Component and it's likely that this question will be asked/searched again.
Using IdTCPClient is as easy at it can be.
You will just have to assign Host and Port , open the connection and Write the content to the socket of IdTCPClient.
Since you will have to be able to read the data at the serverside and there is no need for a protocol (eg sending the length of the content as integer to let the server know how much he has to read) the most simple way is to use Socket.WriteLn to be able to use Socket.ReadLn at serverside.
Note that the OnExecute Event of IdTCPServer is run in an own threadcontext, so you will have to synchronize your calls to the mainthread.
One of the possible ways to do this is to use a descendant of TIDSync .

uses IDSync;

type
  TMySync = Class(TIDSync)
  private
    FContent: String;
    FDestination: TStrings;
    Constructor Create(const Content: String; Destination: TStrings); overload;
  protected
    Procedure DoSynchronize; override;
  public
  End;

constructor TMySync.Create(const Content: String; Destination: TStrings);
begin
  inherited Create;
  FContent := Content;
  FDestination := Destination;
end;

procedure TMySync.DoSynchronize;
begin
  inherited;
  FDestination.Add(FContent);
end;

procedure TaForm.Button1Click(Sender: TObject);
begin
  if not IdTCPClient.Connected then
    IdTCPClient.Connect;
  IdTCPClient.Socket.WriteLn(Edit.Text);
end;

procedure TaForm.FormCreate(Sender: TObject);
const
  // the port which will be used for Server and Client
  C_PORT = 12345;
begin
  IdTCPServer.DefaultPort := C_PORT;
  IdTCPServer.Active := true;
  IdTCPClient.Host := '127.0.0.1';
  IdTCPClient.Port := C_PORT;
end;

procedure TaForm.IdTCPServerExecute(AContext: TIdContext);
begin
  With TMySync.Create(AContext.Connection.Socket.ReadLn, Memo.Lines) do
  begin
    Synchronize;
    Free;
  end;
end;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM