Bookstore

Recent

Author Topic: Indy Client (android) Server (windows)  (Read 5660 times)

zeljkoc

  • Full Member
  • ***
  • Posts: 104
    • Zeljko Cvijanovic
Indy Client (android) Server (windows)
« on: November 20, 2012, 01:58:33 pm »
example: 
send and receive:  message 
send and receive:  bitmap

possible to send SQL requests

(indy-10.2.0.3.zip)



 server (windows)
Code: [Select]
unit mainform;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, windows,
  ExtCtrls, StdCtrls,  ComCtrls, IdCmdTCPServer, IdCustomTCPServer, IdContext,
  IdCommandHandlers;

type

  { TForm1 }
  TForm1 = class(TForm)
    IdCmdTCPServer1: TIdCmdTCPServer;
    lbLog: TMemo;
    Panel1: TPanel;
    procedure IdCmdTCPServer1CommandHandlers1Command(ASender: TIdCommand);
    procedure IdCmdTCPServer1Execute(AContext: TIdContext);
    procedure IdCmdTCPServerCommandHandlers0Command(ASender: TIdCommand);
    procedure IdCmdTCPServer1Connect(AContext: TIdContext);
    procedure IdCmdTCPServer1Disconnect(AContext: TIdContext);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

uses HTTPDefs;

{$R *.lfm}

var
  ID: Integer;

{ TForm1 }
procedure TForm1.IdCmdTCPServer1Connect(AContext: TIdContext);
begin
    lbLog.lines.Add ('Connected from: ' +  AContext.Connection.Socket.Binding.PeerIP );
end;

procedure TForm1.IdCmdTCPServerCommandHandlers0Command(ASender: TIdCommand);
var
   filename: string;
   fstream: TFileStream;
begin
   if Assigned (ASender.Params) then filename := HttpDecode(ASender.Params[0]);
   if not FileExists(filename) then begin
     ASender.Response.Text:='File not found';
     lbLog.Lines.Add('File not found: '+ filename);
     raise EIdTCPServerError.Create('File not found: '+filename);
   end else begin
     fstream := TFileStream.Create(filename, fmOpenRead);
     try
       ASender.Context.Connection.IOHandler.Write(fstream, fstream.Size, True);
       lbLog.Lines.Add('File returned: '+ filename +
       ' ('+ IntToStr(fstream.Size)+')');
     finally
       fstream.Free;
     end;
   end;

end;

procedure TForm1.IdCmdTCPServer1CommandHandlers1Command(ASender: TIdCommand);
begin
   ASender.Context.Connection.IOHandler.WriteLn('= This is a dynamic response =');
end;

procedure TForm1.IdCmdTCPServer1Disconnect(AContext: TIdContext);
begin
    lbLog.lines.Add ('Diconnected from: ' +AContext.Connection.Socket.Binding.PeerIP);
end;

initialization
  // Setup first ID for this session
  ID := GetTickCount;

end. 

 client (android)

Code: [Select]
unit mainform;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils,  Forms, Controls, Graphics, Dialogs,
  ExtCtrls, StdCtrls, LCLType, IdTCPClient;

type
  { TForm1 }
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    IdTCPClient1: TIdTCPClient;
    Image1: TImage;
    Panel1: TPanel;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

uses HTTPDefs, IdGlobal;

{ TForm1 }
procedure TForm1.Button3Click(Sender: TObject);
var
   stream: TStream;
begin
   IdTCPClient1.SendCmd('getfile '+ HttpEncode('C:\ClientList.bmp'));  //file bitmap
   stream := TMemoryStream.Create;
   try
     IdTCPClient1.IOHandler.ReadStream(stream);
     stream.Position:=0;
     Image1.Picture.Bitmap.LoadFromStream(stream);
   finally
     stream.Free;
   end;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
   IdTCPClient1.SendCmd('test');
   Showmessage(IdTCPClient1.LastCmdResult.Code +' : '+
     IdTCPClient1.LastCmdResult.Text.Text);
end;

procedure TForm1.Button5Click(Sender: TObject);
var Encoding: Tidencoding;
begin
  IdTCPClient1.SendCmd('execute');
  ShowMessage(IdTCPClient1.IOHandler.ReadLn());
end;

procedure TForm1.Button6Click(Sender: TObject);
begin
  Close;
end;

procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
    if IdTCPClient1.Connected then IdTCPClient1.Disconnect;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
    if IdTCPClient1. Connected then IdTCPClient1.Disconnect;
    IdTCPClient1.Connect;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  if IdTCPClient1.Connected then IdTCPClient1.Disconnect;
end;

end.


« Last Edit: November 20, 2012, 08:02:47 pm by zeljkoc »