Practice (101) - Create Simple Connection to SGC Server

<< Click to Display Table of Contents >>

Navigation:  All About SAP Interface > SAP r/3 and VB 6.0 > 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

ActiveX (OCX) library

Microsoft Winsock Control 6.0

 

Creating Program

1.Create Folder \Desktop-Application\

2.Run Microsoft Visual Basic 6.0

3.Create New Project, select New > Standard EXE

4.Save Project into  \Desktop-Application\Practice101\ , then change "Form1.frm" to "Main1.frm" and "Project1.vbp" to "Practice101.vbp"

VB_6.0_da0001

5.Rename Project name with "Practice101"

6.Rename File name and Form name from "Form1" to "Main1"

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

No

Component

Properties

Value

Design

1

Button

Name

bConnect

VB_6.0_da0002

 

 

Caption

&Connect

2

TextBox

Name

eIPAddress

 

 

Text

127.0.0.1

3

TextBox

Name

sePort1

 

 

Value

10001

5

Button

Name

bLogin

 

 

Caption

&Login

 

 

Enabled

False

5

TextBox

Name

Memo1

 

 

ScrollBars

3 - Both

 

 

Multiline

True

6

TextBox

Name

Memo2

 

 

ScrollBars

3 - Both

 

 

Multiline

True

7

TextBox

Name

mSLog

 

 

ScrollBars

2 - Vertical

 

 

Multiline

True

8

Winsock

Name

TCpClient

 

8.Create New Module "mdlGlobalVariabel" and save the Module

  'SGC Info

  Public isConnected, isLogin As Boolean

 

  'SGC Login Variable

  Public cEncryptionType As String

  Public cUserID, cPassword, cCompanyCode, cProgramType, cProgramCode As String

 

  'SGC Command

  Public ClientString, LoginString As String

 

9.Open the Code of Main1.frm and Write this code bellow

Private Sub Form_Load()

    Dim NumDate, NumTime As Double

 

    mSLog.Text = "Run Application"

 

    NumDate = Format(Date, "###")

    NumTime = Format(Time, "###0.0000000000")

 

    ClientString = "<?xml version=""1.0"" encoding=""utf-8""?>"

    ClientString = ClientString & vbCrLf & "<SGC_CLIENT DATE=""" & NumDate & """ TIME=""" & NumTime & """>"

    ClientString = ClientString & vbCrLf & "  <CLIENT_STRING COMMAND=""Get Server String"" USERNAME="""" PASSWORD="""" COMPANY_CODE="""" PROGRAM_TYPE="""" PROGRAM_CODE="""" PARAMETER1="""" PARAMETER2="""" PARAMETER3=""""/>"

    ClientString = ClientString & vbCrLf & "</SGC_CLIENT>"

    ClientString = ClientString & vbCrLf & " " '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 = "<?xml version=""1.0"" encoding=""utf-8""?>"

    LoginString = LoginString & vbCrLf & "<SGC_CLIENT DATE=""" & NumDate & """ TIME=""" & NumTime & """>"

    LoginString = LoginString & vbCrLf & "  <CLIENT_STRING COMMAND=""Login"" " & _

      "USERNAME=""" & cUserID & """ " & _

      "PASSWORD=""" & cPassword & """ " & _

      "COMPANY_CODE=""" & cCompanyCode & """ " & _

      "PROGRAM_TYPE=""" & cProgramType & """ " & _

      "PROGRAM_CODE=""" & cProgramCode & """ " & _

      "PARAMETER1="""" PARAMETER2="""" PARAMETER3=""""/>"

    LoginString = LoginString & vbCrLf & "</SGC_CLIENT>"

    LoginString = LoginString & vbCrLf & " " 'Add empty line after "</SGC_CLIENT>"

 

End Sub

 

10.Add Event Click at bConnect button and then write code to connect and disconnect to SGC Server

Private Sub bConnect_Click()

  If bConnect.Caption = "&Connect" Then

    mSLog.Text = mSLog.Text & vbCrLf & "Try connect to SGC Server"

    Memo1.Text = ""

    Memo2.Text = ""

    

    On Error GoTo LogA

      'Connect to SGC Server

      TCpClient.RemoteHost = eIPAddress.Text

      TCpClient.RemotePort = sePort1.Text

      TCpClient.Connect

      

  Else

    TCpClient_Close

  End If

  Exit Sub

  

LogA:

  Msg = Err.Description

  If Msg = "" Then

    Msg = "Unkown error !!"

  End If

  

  MsgBox Msg, vbExclamation

  TCpClient_Close

  

End Sub

 

11.Add Event Click at bLogin button and then write code to send "Login String"

Private Sub bLogin_Click()

  Memo1.Text = ""

  Memo2.Text = ""

  

  'Sending command to SGC Server

  TCpClient.SendData LoginString

      

  'Display XML Data

  Memo1.Text = LoginString

  

End Sub

 

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

Private Sub TCpClient_Connect()

  'Set Component Atribute

  bConnect.Caption = "&Disconnect"

  mSLog.Text = mSLog.Text & vbCrLf & "Connections is succeed"

 

  eIPAddress.Enabled = False

  sePort1.Enabled = False

  bLogin.Enabled = True

  isConnected = True

  bLogin.Enabled = True

 

  'Sending command to SGC Server

  TCpClient.SendData ClientString

      

  'Display XML Data

  Memo1.Text = ClientString

  

End Sub

 

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

Private Sub TCpClient_Close()

  TCpClient.Close

 

  'Set Component Atribute

  bConnect.Caption = "&Connect"

  mSLog.Text = mSLog.Text & vbCrLf & "Disconnected form server"

 

  eIPAddress.Enabled = True

  sePort1.Enabled = True

  bLogin.Enabled = False

  isConnected = False

  bLogin.Enabled = False

  

  Memo1.Text = ""

  Memo2.Text = ""

  

End Sub

 

14.Add Event DataArrival at TCPClient and then write code for condition when application receives data from SGC Server

Private Sub TCpClient_DataArrival(ByVal bytesTotal As Long)

  Dim tmpReceive As String

  

  mSLog.Text = mSLog.Text & vbCrLf & "Receiving data form SGC Server"

  TCpClient.GetData tmpReceive, vbString

  Memo2.Text = Memo2.Text & tmpReceive & vbCrLf

End Sub

 

15.Add Event Error at TCPClient and then write code for condition when TCPClient goes wrong

Private Sub TCpClient_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)

  mSLog.Text = mSLog.Text & vbCrLf & "Error : " & Description

End Sub

 

16.Save and Run the application

 

Result

Screen 1. Connected to SGC Server

VB_6.0_da0003

 

Screen 2. Login to SGC Server

VB_6.0_da0004