Monday, June 27, 2016

Delphi tutorial Client server application

 Delphi tutorial   Client server application

This is Delphi Tutorial about client-server application.



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;


Update 22.03.2018 Source code for Client and Server Forms :

Client Form source code :


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.


Server Form source code :


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;

end.

6 comments:

  1. Hey.
    I made a similar server and client, but I can’t connect on the local network. The firewall is disabled, there is no antivirus ...

    ReplyDelete
  2. Hello. Could you give the embarcadero project?

    ReplyDelete
    Replies
    1. Sorry , no. But you have complete procedure here with source code.

      Delete
  3. It works if Sever and Client are in the same internet connection (as you did). I sent Client pro to my friend at different location, it did not work (we turned off all fire wall). I thought that Client.host or Client.Address problems. Could you please test it as what I did to see if it work? Thank you,

    ReplyDelete
  4. I will try to simulate on different networks and let you know.
    It should work, but probably ports should be forwarded.

    ReplyDelete

Popular Posts

Recent Posts

Unordered List

Text Widget

Pages

Search This Blog

Powered by Blogger.

Contributors

Text Widget