Practice (101) - Create Simple Connection to SGC Server

<< Click to Display Table of Contents >>

Navigation:  All About SAP Interface > SAP r/3 and Delphi > Create Desktop Application using SGC Server > Basic Knowledge >

Practice (101) - Create Simple Connection to SGC Server

Objective

Creating Program

Result

SGC Server

v1.0, v1.1

Support

Source Code is available (Download)

 

No

Description

 

1

Difficulty Level

Easy

2

Delphi Component

TButton, TEdit, TCheckBox, TSpinEdit, TGroupBox, TMemo

3

Indy Client Component

TIdTCPClient, TIdAntiFreeze

 

Creating Program

1.Create Folder \Desktop-Application\

2.Run Delphi Application

3.Save Unit as Main.pas and Project as Practice101.dpr to folder \Desktop-Application\Practice101\

delphi_da0001

4.Rename Form1 to Main1

5.Add and design a components according to component list bellow

No

Component

Properties

Value

Design

1

TButton

Name

bConnect

delphi_da0002

 

 

Caption

&Connect

2

TEdit

Name

eIPAddress

 

 

Text

127.0.0.1

3

TSpinEdit

Name

sePort1

 

 

Value

10001

5

TButton

Name

bLogin

 

 

Caption

&Login

 

 

Enabled

False

5

TMemo

Name

Memo1

6

TMemo

Name

Memo2

7

TMemo

Name

mSLog

8

TidTCpClient

Name

TCPClient

 

 

Host

127.0.0.1

9

TIdAntiFreeze

Name

IdAntiFreeze

 

6.Create Global Variable "isConnected" and "isLogin" as boolean and write it bellow code "Main1: TMain1;"

7.Create Global Variable "ClientString" and "LoginString" as TStringList and SGC Login Variables

var

  Main1: TMain1;

  isConnected, isLogin : boolean;

  ClientString,LoginString:TStringList;

 

  //SGC Login Variables

  cEncryptionType:string;

  cUserID,cPassword,cCompanyCode,

  cProgramType, cProgramCode:string;

 

8.Create new Procedure "SendRequest" for sending Client Command to SGC Server and receive the response from SGC Server.

...

  public

    { Public declarations }

    procedure SendRequest(strRequest,strReceive:TStringList);

  end;

...

procedure TMain1.SendRequest(strRequest,strReceive:TStringList);

var strTemp : string;

    whileLimit : Extended;

begin

  whileLimit:=0;

  try

    mSLog.Lines.Add('Receiving data form SGC Server');

    TCPClient.Write(strRequest.Text);

    //Read Connection and ended with '</SGC_SERVER>'

    while not TCPClient.ClosedGracefully do

    begin

      whileLimit:=whileLimit+1;

      try

        strTemp:=TCPClient.ReadLn;

        strReceive.Add(strTemp);

        if strTemp = '</SGC_SERVER>' then break;

      except

        break;

      end;

      if whileLimit > 1000000 then

      begin

        strTemp:='';

        break;

      end;

    end;

    mSLog.Lines.Add('Data received');

  except

    mSLog.Lines.Add('Error While Receiving Data');

  end;

 

end;

 

9.Add Event OnClick at bConnect button and then write code to connect and disconnect to SGC Server

procedure TMain1.bConnectClick(Sender: TObject);

var tmpReceive:TStringList;

begin

  if bConnect.Caption = '&Connect' then

  begin

          mSLog.Lines.Add('Try connect to SGC Server');

    try

      //Connect to SGC Server

      TCPClient.Host := eIPAddress.Text;

      TCPClient.Port := sePort1.Value;

      TCPClient.Connect;

 

      //Create temporary data

      tmpReceive:=TStringList.Create;

 

      //Sending command to SGC Server

      SendRequest(ClientString,tmpReceive);

 

      //Display XML Data

      Memo1.Lines.Text:= ClientString.Text;

      Memo2.Lines.Text:= tmpReceive.Text;

 

      //Free temporary data

      tmpReceive.Destroy;

      

    except

      mSLog.Lines.Add('Connection Failed');

    end;

  end

  else

  begin

    TCPClient.Disconnect;

  end;

end;

 

10.Add Event OnConnected at TCPClient and then write code for condition after application connected to SGC Server

procedure TMain1.TCPClientConnected(Sender: TObject);

begin

  bConnect.Caption := '&Disconnect';

  eIPAddress.Enabled:=false;

  sePort1.Enabled:=false;

  bLogin.Enabled:=true;

  isConnected:=true;

 

  mSLog.Lines.Add('Connections is succeed');

end;

 

11.Add Event OnDisconnected at TCPClient and then write code for condition after application disconnected from SGC Server

procedure TMain1.TCPClientDisconnected(Sender: TObject);

begin

  bConnect.Caption := '&Connect';

  eIPAddress.Enabled:=true;

  sePort1.Enabled:=true;

  bLogin.Enabled:=false;

  isLogin:=false;

  isConnected:=false;

 

  Memo1.Lines.Clear;

  Memo2.Lines.Clear;

 

  mSLog.Lines.Add('Disconnected form server');

end;

 

12.Add Event Create at Main Form and then write code for Client Command "Client String"  and "Login String"

procedure TMain1.FormCreate(Sender: TObject);

begin

  ClientString:=TStringList.Create;

  ClientString.Add('<?xml version="1.0" encoding="utf-8"?>');

  ClientString.Add('<SGC_CLIENT DATE="' +FloatToStr(Date)+ '" TIME="' +FloatToStr(Time)+ '">');

  ClientString.Add('  <CLIENT_STRING COMMAND="Get Server String" USERNAME="" PASSWORD="" COMPANY_CODE="" PROGRAM_TYPE="" PROGRAM_CODE="" PARAMETER1="" PARAMETER2="" PARAMETER3=""/>');

  ClientString.Add('</SGC_CLIENT>');

  ClientString.Add(' ');//Add empty line after '</SGC_CLIENT>'

 

 

  {

    SGC Server has an authorization system. The authorization system will be blocked and limited the connection from any SGC Client connection.

    There are a several required field to Login and go through the authorization, such as :

    => USERNAME=""

    => PASSWORD=""

    => COMPANY_CODE=""

    => PROGRAM_TYPE=""

    => PROGRAM_CODE=""

    If the required fields value doesn't match with authorization configuration at SGC Server then the connection will be refuse (Login Failed).

 

    Assume that you are not login to SGC Server, then Login String always automaticaly filled with this value bellow :

    => USERNAME="DEMO"

    => PASSWORD="123456"

    => COMPANY_CODE="1000"

    => PROGRAM_TYPE="Examples"

    => PROGRAM_CODE="Client_Example_01"

  }

  //Set Default SGC Client Login for Demo Only

  cUserID:= 'DEMO';

  cPassword:= '123456';

  cCompanyCode:= '1000';

  cProgramType:= 'Examples';

  cProgramCode:= 'Client_Example_01';

  

  LoginString:=TStringList.Create;

  LoginString.Add('<?xml version="1.0" encoding="utf-8"?>');

  LoginString.Add('<SGC_CLIENT DATE="' +FloatToStr(Date)+ '" TIME="' +FloatToStr(Time)+ '">');

  LoginString.Add('  <CLIENT_STRING COMMAND="Login" '+

                                   'USERNAME="'+cUserID+'" '+

                                   'PASSWORD="'+cPassword+'" '+

                                   'COMPANY_CODE="'+cCompanyCode+'" '+

                                   'PROGRAM_TYPE="'+cProgramType+'" '+

                                   'PROGRAM_CODE="'+cProgramCode+'" '+

                                   'PARAMETER1="" PARAMETER2="" PARAMETER3=""/>');

  LoginString.Add('</SGC_CLIENT>');

  LoginString.Add(' ');//Add empty line after '</SGC_CLIENT>'

 

end;

 

13.Add Event OnClick at bLogin button and then write code to send "Login String"

procedure TMain1.bLoginClick(Sender: TObject);

var tmpReceive:TStringList;

begin

  //Create temporary data

  tmpReceive:=TStringList.Create;

 

  //Sending command to SGC Server

  SendRequest(LoginString,tmpReceive);

 

  //Display XML Data

  Memo1.Lines.Text:= LoginString.Text;

  Memo2.Lines.Text:= tmpReceive.Text;

 

  //Free temporary data

  tmpReceive.Destroy;

 

end;

 

Note : When you has login to SGC Server then you can execute another SGC Command, such as "Get User Menu List", Get Function Info" and "Run Function".

 

14.Save and Run the application

 

Result

Screen 1. Connected to SGC Server

delphi_da0003

 

Screen 2. Login to SGC Server

delphi_da0004