|
<< Click to Display Table of Contents >> Navigation: All About SAP Interface > SAP r/3 and VB.Net > Create Desktop Application using SGC Server > Basic Knowledge > Practice (101) - Create Simple Connection to SGC Server |
Objective |
|
SGC Server |
v1.0, v1.1 |
Support |
Source Code is available (Download) |
No |
Description |
|
1 |
Difficulty Level |
Easy |
2 |
.Net Library |
System System.Data System.Deployment System.Drawing System.Windows.Forms System.XML |
1.Create Folder \Desktop-Application\
2.Run Microsoft Visual Studio
3.Create New Project
4.Enter Project name as "Practice101" Save into \Desktop-Application\Practice101\

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

6.Add and design a components according to component list bellow
No |
Component |
Properties |
Value |
Design |
1 |
Forms.Button |
Name |
bConnect |
|
|
|
Text |
&Connect |
|
2 |
Forms.TextBox |
Name |
eIPAddress |
|
|
|
Text |
127.0.0.1 |
|
3 |
Forms.NumericUpDown |
Name |
sePort1 |
|
|
|
Value |
10001 |
|
5 |
Forms.Button |
Name |
bLogin |
|
|
|
Text |
&Login |
|
|
|
Enabled |
False |
|
5 |
Forms.TextBox |
Name |
Memo1 |
|
|
|
ScrollBars |
Both |
|
|
|
Multiline |
True |
|
|
|
WordWarp |
False |
|
6 |
Forms.TextBox |
Name |
Memo2 |
|
|
|
ScrollBars |
Both |
|
|
|
Multiline |
True |
|
|
|
WordWarp |
False |
|
7 |
Forms.TextBox |
Name |
mSLog |
|
|
|
ScrollBars |
Vertical |
|
|
|
Multiline |
True |
7.Create New Module "mdlGlobalVariabel.vb"

8.Create New Class "clsClientSocket.vb"

9.Create Global Variable at module "mdlGlobalVariabel.vb"
Module mdlGlobalVariabel
'SGC Info
Public isConnected, isLogin As Boolean
'SGC Login Variable
Public cEncryptionType As String
Public cUserID, cPassword, cCompanyCode, cProgramType, cProgramCode As String
Public TCPClient As New clsClientSocket
'SGC Command
Public ClientString, LoginString As String
End Module
10.Copy this Source Code bellow to "clsClientSocket.vb"
'Class Name : Client Socket (Revision 1) 'Compatible with : SGC Server v1.0, v1.1 'Created by Albertus Reinandang '08-05-2008
Imports System Imports System.IO Imports System.Net Imports System.Net.Sockets Imports System.Net.Sockets.NetworkStream Imports System.Text Imports System.Xml
Public Class clsClientSocket Dim gvTCPClient As New System.Net.Sockets.TcpClient() Dim gvNetworkStream As NetworkStream Dim gvTimeOut As Integer Dim gvHost As String Dim gvPort As Int32
Public Function Connect(ByVal Host As String, ByVal Port As Int32, ByVal TimeOut As Integer) As Boolean Try gvPort = Port gvHost = Host gvTCPClient.Connect(gvHost, gvPort) gvNetworkStream = gvTCPClient.GetStream() Catch ex As Exception
End Try Connect = gvTCPClient.Connected() End Function
Public Function Disconnect() As Boolean Try gvTCPClient.Close() gvTCPClient = Nothing gvNetworkStream = Nothing Disconnect = True Catch ex As Exception Disconnect = False End Try End Function
Public Function isConnected() As Boolean Try isConnected = gvTCPClient.Connected Catch ex As Exception End Try
End Function
Public Function SendText(ByRef DisplayLog As TextBox, ByVal TextRequest As String, ByRef TextReplay As String) As Boolean Try If gvTCPClient.Connected And gvNetworkStream.CanWrite And gvNetworkStream.CanRead Then 'Send XML Data to SGC Server Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(TextRequest) gvNetworkStream.Write(sendBytes, 0, sendBytes.Length)
' Read the NetworkStream into a byte buffer. Dim readBuffer(1024) As Byte Dim strTemp As StringBuilder = New StringBuilder() Dim numberOfBytesRead As Integer = 0 Dim maxReceiveData As Integer DisplayLog.Text = DisplayLog.Text & Environment.NewLine & "Receiving data form SGC Server" Do numberOfBytesRead = gvNetworkStream.Read(readBuffer, 0, readBuffer.Length) strTemp.AppendFormat("{0}", Encoding.ASCII.GetString(readBuffer, 0, numberOfBytesRead))
'--If you want to limit received data for SGC Server 'maxReceiveData = maxReceiveData + 1 'If maxReceiveData = 100 Then ' Exit Do 'End If Loop While gvNetworkStream.DataAvailable
' Output the data received from SGC Server to string. TextReplay = strTemp.ToString
SendText = True DisplayLog.Text = DisplayLog.Text & Environment.NewLine & "Data received" Else TextReplay = "" SendText = False End If Catch ex As Exception
End Try
End Function
Public Function SendTextToXMLDocument(ByRef DisplayLog As TextBox, ByVal TextRequest As String, ByRef XMLReplayDoc As XmlDocument) As Boolean Try If gvTCPClient.Connected And gvNetworkStream.CanWrite And gvNetworkStream.CanRead Then 'Send XML Data to SGC Server Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(TextRequest) gvNetworkStream.Write(sendBytes, 0, sendBytes.Length)
' Read the NetworkStream into a byte buffer. ' Incoming message may be larger than the buffer size. Dim readBuffer(1024) As Byte Dim strTemp As StringBuilder = New StringBuilder() Dim numberOfBytesRead As Integer = 0 Dim maxReceiveData As Integer Do numberOfBytesRead = gvNetworkStream.Read(readBuffer, 0, readBuffer.Length) strTemp.AppendFormat("{0}", Encoding.ASCII.GetString(readBuffer, 0, numberOfBytesRead))
'--If you want to limit received data for SGC Server 'maxReceiveData = maxReceiveData + 1 'If maxReceiveData = 100 Then ' Exit Do 'End If Loop While gvNetworkStream.DataAvailable
' Output the data received from SGC Server to XMLDocument. Try XMLReplayDoc.LoadXml(strTemp.ToString) Catch ex As Exception End Try SendTextToXMLDocument = True Else SendTextToXMLDocument = False End If Catch ex As Exception
End Try
End Function
End Class
|
11.Open Desain of Main1.vb
12.Double click the Form to add new Event Procedure "Main_Load"
13.Write this code bellow
Private Sub Main1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim NumDate, NumTime As Double
mSLog.Text = "Run Application"
NumDate = Date.Today.ToOADate
NumTime = DateAndTime.TimeOfDay.ToOADate
ClientString = "<?xml version=""1.0"" encoding=""utf-8""?>"
ClientString = ClientString & Environment.NewLine & "<SGC_CLIENT DATE=""" & NumDate & """ TIME=""" & NumTime & """>"
ClientString = ClientString & Environment.NewLine & " <CLIENT_STRING COMMAND=""Get Server String"" USERNAME="""" PASSWORD="""" COMPANY_CODE="""" PROGRAM_TYPE="""" PROGRAM_CODE="""" PARAMETER1="""" PARAMETER2="""" PARAMETER3=""""/>"
ClientString = ClientString & Environment.NewLine & "</SGC_CLIENT>"
ClientString = ClientString & Environment.NewLine & " " '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 & Environment.NewLine & "<SGC_CLIENT DATE=""" & NumDate & """ TIME=""" & NumTime & """>"
LoginString = LoginString & Environment.NewLine & " <CLIENT_STRING COMMAND=""Login"" " & _
"USERNAME=""" & cUserID & """ " & _
"PASSWORD=""" & cPassword & """ " & _
"COMPANY_CODE=""" & cCompanyCode & """ " & _
"PROGRAM_TYPE=""" & cProgramType & """ " & _
"PROGRAM_CODE=""" & cProgramCode & """ " & _
"PARAMETER1="""" PARAMETER2="""" PARAMETER3=""""/>"
LoginString = LoginString & Environment.NewLine & "</SGC_CLIENT>"
LoginString = LoginString & Environment.NewLine & " " 'Add empty line after "</SGC_CLIENT>"
End Sub
14.Add Event Click at bConnect button and then write code to connect and disconnect to SGC Server
Private Sub bConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bConnect.Click
Dim tmpReceive As String
If bConnect.Text = "&Connect" Then
mSLog.Text = mSLog.Text & Environment.NewLine & "Try connect to SGC Server"
'Connect to SGC Server
If TCPClient.Connect(eIPAddress.Text, sePort1.Value, 30000) Then '30000 > Time out 30 Second
'Sending command to SGC Server
TCPClient.SendText(mSLog, ClientString, tmpReceive)
'Display XML Data
Memo1.Text = ClientString
Memo2.Text = tmpReceive
'Free temporary data
tmpReceive = ""
End If
Else
TCPClient.Disconnect()
End If
'Set Component Atribute
If TCPClient.isConnected Then
bConnect.Text = "&Disconnect"
mSLog.Text = mSLog.Text & Environment.NewLine & "Connections is succeed"
Else
bConnect.Text = "&Connect"
mSLog.Text = mSLog.Text & Environment.NewLine & "Disconnected form server"
Memo1.Text = ""
Memo2.Text = ""
End If
eIPAddress.Enabled = Not TCPClient.isConnected
sePort1.Enabled = Not TCPClient.isConnected
bLogin.Enabled = TCPClient.isConnected
isConnected = TCPClient.isConnected
bLogin.Enabled = TCPClient.isConnected
End Sub
15.Add Event Click at bLogin button and then write code to send "Login String"
Private Sub bLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bLogin.Click
Dim tmpReceive As String
'Sending command to SGC Server
TCPClient.SendText(mSLog, LoginString, tmpReceive)
'Display XML Data
Memo1.Text = ClientString
Memo2.Text = tmpReceive
'Free temporary data
tmpReceive = ""
End Sub
16.Save and Run the application
▪Screen 1. Connected to SGC Server

▪Screen 2. Login to SGC Server
