• Latest

    Monday, June 27, 2016

    Delphi tutorial Client server application

     Delphi tutorial   Client server application

    This is Delphi Tutorial about client-server application, and as usual you can find here source code for this app, and also exe files at address :
    https://drive.google.com/open?id=0B_njK7HczCjDekg0WG1hamUyWTA

    Here is video tutorial :




    and here is source code :

    (code style formatted by http://hilite.me/ )


    Source code for client app :


    unit MainForm;
    
    interface
    
    uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, ScktComp, StdCtrls;
    
    type
    TForm1 = class(TForm) Edit1: TEdit; Edit2: TEdit; Label1: TLabel; Label2: TLabel; GroupBox1: TGroupBox; CheckBox1: TCheckBox; Label3: TLabel; Label4: TLabel; Edit3: TEdit; Edit4: TEdit; Edit5: TEdit; Button1: TButton; Button2: TButton; Memo1: TMemo; ClientSocket1: TClientSocket; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure ClientSocket1Connect(Sender: TObject; Socket: TCustomWinSocket); procedure ClientSocket1Disconnect(Sender: TObject; Socket: TCustomWinSocket); procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
    private { Private declarations }
    public { Public declarations }
    end;
    
    var
    Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    procedure TForm1.Button1Click(Sender: TObject);
    begin
    ClientSocket1.Active:=False;
    ClientSocket1.Host:=Edit1.Text;
    ClientSocket1.Port:=StrToInt(Edit2.Text)
    ;
    ClientSocket1.Active:=True;
    end;
    
    procedure TForm1.Button2Click(Sender: TObject);
    begin
    ClientSocket1.Socket.SendText(Edit5.Text);
    Memo1.Lines.Add('Me : '+edit5.Text);
    end;
    
    procedure TForm1.ClientSocket1Connect(Sender: TObject;
    Socket: TCustomWinSocket);
    begin
    CheckBox1.Checked:=Socket.Connected;
    Edit3.Text:=Socket.LocalAddress;
    Edit4.Text:=Socket.LocalHost;
    Memo1.Lines.Clear;
    
    end;
    
    procedure TForm1.ClientSocket1Disconnect(Sender: TObject;
    Socket: TCustomWinSocket);
    begin
    CheckBox1.Checked:=Socket.Connected;
    end;
    
    procedure TForm1.ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
    begin
    Memo1.Lines.Add(ClientSocket1.Socket.ReceiveText);
    end;
    
    end.
    Source code for server app :
    unit Main;
    
    interface
    
    uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, ScktComp, StdCtrls;
    
    type
    TForm1 = class(TForm) GroupBox1: TGroupBox; GroupBox2: TGroupBox; Label1: TLabel; Label2: TLabel; Label3: TLabel; Label4: TLabel; Label5: TLabel; CheckBox1: TCheckBox; Edit1: TEdit; Button1: TButton; Edit2: TEdit; Edit3: TEdit; Edit4: TEdit; Edit5: TEdit; Edit6: TEdit; Label6: TLabel; Button2: TButton; Memo1: TMemo; ServerSocket1: TServerSocket; procedure Button1Click(Sender: TObject); procedure ServerSocket1Accept(Sender: TObject; Socket: TCustomWinSocket); procedure Button2Click(Sender: TObject); procedure ServerSocket1ClientRead(Sender: TObject; Socket: TCustomWinSocket);
    private { Private declarations }
    public { Public declarations }
    end;
    
    var
    Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
    port:Integer;
    begin
    ServerSocket1.Active:=False;
    port:=StrToInt(Edit1.Text);
    ServerSocket1.Port:=port;
    ServerSocket1.Active:=True;
    Edit4.Text:='Listening...';
    end;
    
    procedure TForm1.Button2Click(Sender: TObject);
    begin
    ServerSocket1.Socket.Connections[0].SendText(Edit6.Text);
    Memo1.Lines.Add('Me : '+Edit6.Text);
    Edit6.Text:='';
    end;
    
    procedure TForm1.ServerSocket1Accept(Sender: TObject; Socket: TCustomWinSocket);
    begin
    CheckBox1.Checked:=true;
    Edit2.Text:=Socket.RemoteAddress;
    Edit3.Text:=Socket.RemoteHost;
    Edit5.Text:=Socket.LocalAddress;
    Edit4.Text:='Connected';
    end;
    
    procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
    Socket: TCustomWinSocket);
    begin
    Memo1.Lines.Add(Socket.RemoteHost+' : '+Socket.ReceiveText);
    end;
    

    Fashion

    Beauty

    Travel