Today, Delphi is one of the popular desktop programming tool. There are thousand even million programmer in this world using delphi as their favorit tool. This site try to collect the examples, tips and tricks of Delphi programming. We collect, test them and redistribute the collection of delphi programming for you.

Get Image from Server with Indy Socket

Author: admin | Category: Indy, Socket

//This is how we can make a client-server application to get image stored at server to be diplayed in client
//We Need IdTCPServer (TIdTCPServer) for server application and IdTCPClient (TIdTCPClient) in Client Application
//For this reason we have to make two application, Server and Client
//First This is the Server-Side Script
//We need to Put
    //Memo1 (TMemo ) --> Displaying Client connection status
    //IdTCPServer (From Indy Server Tab) --> Set the command property to "getfile" , CmdDelimiter and ParamDelimiter property to "#32"
    //Add TIdCommandHandler0 by clicking ... button in CommandHandlers property
    //Then set the TIdCommandHandler0 OnCommand Event. The script just like procedure TForm1.IdTCPServer1TIdCommandHandler0Command(ASender: TIdCommand) below:
 
{=========================Server Side========================}
unit U_server;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, IdBaseComponent, IdComponent, IdTCPServer, HTTPApp, StdCtrls,
  ExtCtrls;
 
type
  TForm1 = class(TForm)
    IdTCPServer1: TIdTCPServer;
    Memo1: TMemo;
    Panel1: TPanel;
    procedure IdTCPServer1TIdCommandHandler0Command(ASender: TIdCommand);
    procedure FormCreate(Sender: TObject);
    procedure IdTCPServer1Connect(AThread: TIdPeerThread);
    procedure IdTCPServer1Disconnect(AThread: TIdPeerThread);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
procedure TForm1.IdTCPServer1TIdCommandHandler0Command(
  ASender: TIdCommand);
var
  filename:TFileName;
  fstream:TFileStream;
begin
  if assigned(ASender.Params) then
  begin
    filename:=HttpDecode(ASender.Params[0]);
    if FileExists(filename) then
    begin
      fstream:=TFileStream.Create(filename,fmOpenRead);
      try
        ASender.Thread.Connection.WriteStream(fstream,true,true);
        Memo1.Lines.Add('Sending '+HTTPDecode(ASender.Params[0])+' to '+ASender.Thread.Connection.Socket.Binding.PeerIP);
      finally
        fstream.Free;
      end;
    end
    else
    begin
      ASender.Response.Text:='File Tidak Ditemukan';
      raise EIdTCPServerError.Create('File not found: '+filename);
    end;
  end;
end;
 
procedure TForm1.FormCreate(Sender: TObject);
begin
  IdTCPServer1.Active:=true;
end;
 
procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread);
begin
  Memo1.Lines.Add('Connect: '+AThread.Connection.Socket.Binding.PeerIP);
end;
 
procedure TForm1.IdTCPServer1Disconnect(AThread: TIdPeerThread);
begin
  Memo1.Lines.Add('Disconnect: '+AThread.Connection.Socket.Binding.PeerIP);
end;
 
end.
{======================End of Server Side======================}
 
//Then we have to make the client application
//Client application needs
    //Image1 (TImage)  --> for displaying the image taken from server
    //IdTCPClient1 (TIdTCPClient)  --> connect to IdTCPServer in Server Application
    //Label1 (TLabel) --> Displaying Socket Status ("Connected" and "Disconnected"). First set it caption to "Disconnected"
    //Button2 (TButton) --> To Create connection, Set the caption to "Connect"
    //Button3 (TButton) --> To Close connection to server, set the caption to "Disconnect"
    //Edit1 (TEdit) --> Set the text property to the file taken from server, First set it to "C:\Windows\Prairie Wind.bmp"
    //Button1 (TButton) --> To get image from server
    //Edit2 (TEdit) --> to give the server IP Address like "192.168.1.9"
 
{=========================Client Side========================}
unit U_Client;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  StdCtrls, ExtCtrls, HTTPApp;
 
type
  TForm1 = class(TForm)
    Image1: TImage;
    IdTCPClient1: TIdTCPClient;
    Label1: TLabel;
    Button2: TButton;
    Button3: TButton;
    Edit1: TEdit;
    Button1: TButton;
    Edit2: TEdit;
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
procedure TForm1.Button2Click(Sender: TObject);
begin
  IdTCPClient1.Host:=Edit2.Text;
  try
    IdTCPClient1.Connect;
    Label1.Caption:='Connected';
  except
    raise exception.Create('Koneksi Gagal');
  end;
end;
 
procedure TForm1.Button3Click(Sender: TObject);
begin
   try
      IdTCPClient1.Disconnect;
      Label1.Caption:='Disconnected';
   except
      raise exception.Create('Diskoneksi Gagal');
   end;
end;
 
procedure TForm1.Button1Click(Sender: TObject);
var
   stream:TStream;
begin
   IdTCPClient1.WriteLn('getfile '+HTTPEncode(Edit1.Text));
   stream:=TMemoryStream.Create;
   try
      IdTCPClient1.ReadStream(stream);
      stream.Position:=0;
      Image1.Picture.Bitmap.LoadFromStream(stream);
   finally
      stream.Free;
   end;
end;
 
end.
{======================End of Client Side======================}
 
Compile those Server and Client Application and run it
 

Tags: , , ,