delphi tcp/ip IdTCPServer1实例一

时间:2022-09-26 03:30:22

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, IdBaseComponent, IdComponent, IdTCPServer, StdCtrls;

type
  TForm1 = class(TForm)
    IdTCPServer1: TIdTCPServer;
    Button1: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure IdTCPServer1Execute(AThread: TIdPeerThread);
    procedure Button2Click(Sender: TObject);

private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
 IdTCPServer1.Bindings.Clear;
 IdTCPServer1.Bindings.Add.IP:='192.168.1.126';
 IdTCPServer1.Bindings.Add.Port:=168;
 IdTCPServer1.Active:=true;
 if  IdTCPServer1.Active then
       //showmessage('Server running')
       edit1.Text:='Server running'
       else
       edit1.Text:='Server stop'

end;

procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);

var
   m_string:string;
begin
  m_string:=AThread.Connection.ReadLn();

if m_string='begin' then
  begin
       AThread.Connection.WriteLn('我是要传的文字!');
       edit1.Text:='准备传输';
       exit;
       end;
  if m_string='end' then
  begin
       edit1.Text:='传输结束';
       exit;
       end;
  if m_string='qx' then
  begin
        edit1.Text:='取消传输';
        exit;
        end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
   IdTCPServer1.Destroy;

edit1.Text:='Server stop';
end;

end.

源代码下载:http://download.csdn.net/source/1261880