23.04.2009, 19:11
|
Модератор
|
|
Регистрация: 17.04.2008
Сообщения: 8,020
Версия Delphi: 7, XE3, 10.2
Репутация: 49089
|
|
На тебе простейший пример на TIdTCPServer/Client
Код:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdTCPConnection, IdTCPClient, IdBaseComponent, IdComponent,
IdCustomTCPServer, IdTCPServer, IdContext, StdCtrls;
type
TForm1 = class(TForm)
TCPServer: TIdTCPServer;
TCPClient: TIdTCPClient;
btStartServer: TButton;
btConnect: TButton;
edLog: TMemo;
procedure btStartServerClick(Sender: TObject);
procedure btConnectClick(Sender: TObject);
procedure TCPServerExecute(AContext: TIdContext);
procedure TCPServerConnect(AContext: TIdContext);
procedure TCPServerDisconnect(AContext: TIdContext);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.btStartServerClick(Sender: TObject);
begin
TCPServer.Active := True;
end;
procedure TForm1.btConnectClick(Sender: TObject);
begin
TCPClient.Connect;
edLog.Lines.Add('Client: Connected.');
TCPClient.Disconnect;
edLog.Lines.Add('Client: Disconnected.');
end;
procedure TForm1.TCPServerExecute(AContext: TIdContext);
begin
edLog.Lines.Add('Server: client executed.');
end;
procedure TForm1.TCPServerConnect(AContext: TIdContext);
begin
edLog.Lines.Add('Server: client connected.');
end;
procedure TForm1.TCPServerDisconnect(AContext: TIdContext);
begin
edLog.Lines.Add('Server: client disconnected.');
end;
end.
В обоих компонентах в настройке указан порт 8080, а в клиенте еще и Host = 127.0.0.1. То, что они на одной форме - это не принципиально. Просто лень было несколько приложений городить.
|