TCP/IP Client.

 

When ScriptEZ.API based script file is running, an identification area is appended

to the system menu of the console windows as following:

 

 

'//////////////////////////////////////////////////////////////////////////////

' Author: SY

' Demo ScriptEZ.API based application which uses facilities from RAD API:

' . TCP/IP usage

' . Threadpool usage

' . Echo() method usage which displays always messages into a console window

'   instead of WScript.Echo() with 2 behaviours for cscript.exe or wscript.exe

' . ScriptEZ.API Remoting Control Interface usage by NPTelnet.exe program

'   (Connect to ScriptEZ.API process with <processid (PID)> as connection name)

'

' Purpose: Simple TCP/IP Client receiving random packets and sending back at

'          every eight received packets

'//////////////////////////////////////////////////////////////////////////////

 

const SCRIPTEZ_API = "ScriptEZ.API" ' Component name

 

Dim ScriptEZMain

Dim hSocketClient

Dim ReplyCount

Dim StartTime

Dim ReadyToSend

Dim TotalRecv

Dim TotalSend

Dim LastRecvTime

Dim Err_num

Dim hThreadPool

Dim onExit

Dim InputString

Dim showRecvData

Dim Resend

Dim ScrollOutput

Dim PendingTasks

Dim Socket_ver

Dim bstrData

Dim g_hOwnerSocket

' start the script

'/////////////////

Main()

 

' Main Subroutine

'////////////////

Sub Main()

 

  Dim HostAndPort

 

  ScrollOutput = 1

  Resend       = 0

  showRecvData = 0

  onExit       = 0

  ReplyCount = 0

  ReadyToSend= 0

 

  TotalRecv  = 0

  TotalSend  = 0

 

  StartTime = Now()

  LastRecvTime = Now()

 

  ' instantiate component object with callback interface

  Set ScriptEZMain = WScript.CreateObject(SCRIPTEZ_API,"ScriptEZMain_")

  'ScriptEZMain.SetCPUCoreAffinity "0"

 

  ' create client socket object + xtra encryption

  hSocketClient = ScriptEZMain.CreateSocketClientObjectEx

  ScriptEZMain.EnableSocketObjectDataEncryption hSocketClient,true,-1

 

  ' connect to server

  InputString = InputBox("Enter your hostname and port, such like localhost:12346", _

                         "Choose a hostname", _

                         "localhost:12346")

  HostAndPort = Split(InputString,":")

 

  Err_num  = ScriptEZMain.ConnectSocketServerObject(HostAndPort(0), HostAndPort(1), hSocketClient)

  If Err_num <> 0 Then Exit Sub

 

  Socket_ver = ScriptEZMain.GetSocketObjectVersion(hSocketClient)

 

  ' Register App for receiving AppEvent(s) with onAppEvent(EventID)

  ScriptEZMain.AppRegister

 

  ' creating thread pool with 4 threads for polling info

  hThreadPool = ScriptEZMain.CreateTaskQueueObject(4)

  r = ScriptEZMain.AddTask( hThreadPool, hSocketClient )   

 

  ' block until socket break or exit event

  Do Until Err_num <> 0  Or onExit = 3

      WScript.Sleep 1000

  Loop

 

  ' empty queued msg for sending

  r = ScriptEZMain.SendSocketObjectData(hSocketClient,vbNullString,0)

 

  ' App unregister before ending

  ScriptEZMain.AppUnregister

  WScript.Sleep 4000

  Set ScriptEZMain = Nothing

 

End Sub

 

Function DataHandler()

 

 Dim data_size

 data_size  = Len(bstrData)

 If showRecvData = 1 Then ScriptEZMain.Echo CStr(Now)  + " size(" + CStr(data_size) + "): " + CStr(bstrData)

 

 If Resend = 1 Then

   Err_num = ScriptEZMain.SendSocketObjectData(hSocketClient,bstrData,data_size)

   TotalSend = TotalSend + data_size

   ReplyCount = ReplyCount + 1

 Else

   TotalSend = 0

 End If

 DataHandler = 0

 

End Function

 

' Callbacks for App Events notification and fetching help text

'/////////////////////////////////////////////////////////////

Sub ScriptEZMain_onAppEvent(EventID)

 

 ScriptEZMain.Echo " -> EventID:" + CStr(EventID)

 Select Case EventID

   Case 3  onExit= 3

   Case 0  showRecvData = 0

   Case 1  showRecvData = 1

   Case 2  Resend    = 0

   Case 4  Resend    = 1

   Case 5  ScrollOutput = 0

   Case 6  ScrollOutput = 1

   Case 7  r = ScriptEZMain.EnableSocketObjectInOutBoundBandwidthPolling(g_hOwnerSocket,0)

 End Select

 

End Sub

 

Function ScriptEZMain_AppEventHelp()

 

ScriptEZMain_AppEventHelp = vbLF +_

  CStr("Filename: " + Chr(34) + CStr(WScript.ScriptFullName) + Chr(34) + vbLF + _

       "  **** Supported AppEvents ****" + vbLF +_

       "AppEventID = 0" + vbTab + "to hide received data (default)" + vbLF + _

       "AppEventID = 1" + vbTab + "to display received data" + vbLF + _

       "AppEventID = 2" + vbTab + "to not resend back data (default)" + vbLF +_

       "AppEventID = 4" + vbTab + "to start resending back data" + vbLF + _

       "AppEventID = 5" + vbTab + "to disable scrolling" + vbLF + _

       "AppEventID = 6" + vbTab + "to enable scrolling (default)" + vbLF + _

       "AppEventID = 7" + vbTab + "to disable enable bandwidth polling" + vbLF + _

       "AppEventID = 3" + vbTab + "to exit" + vbLF _

       )

 

End Function

 

 

' Callbacks for socket object

' ( client/proxy side)

'///////////////////////////

Function ScriptEZMain_onConnectionState(hOwnerSocket,state_desc,error_number)

 

  g_hOwnerSocket = hOwnerSocket

  ScriptEZMain.Echo state_desc

  Err_num =  error_number

  r = ScriptEZMain.EnableSocketObjectInOutBoundBandwidthPolling(hOwnerSocket)

  ScriptEZMain_onConnectionState = CLng(0)

 

End Function

 

' ( client/proxy side)

'//////////////////////////

Function ScriptEZMain_onData(hOwnerSocket,raw_data,data_size)

 

  ' converting string to BSTR

  bstrData   = ScriptEZMain.ConvertStringToBSTR(raw_data)

  LastRecvTime = Now()

  TotalRecv = TotalRecv + data_size

 

  If showRecvData = 0 Then

   ScriptEZMain.Echo " ThreadID:" + CStr(ScriptEZMain.GetCurrentThreadId) _

                     + vbTab +" Data size..:" + CStr(data_size)

  End If

 

  ReadyToSend = ReadyToSend + 1

  If ReadyToSend = 8  Then 'sending back every 8 received packets

    ' Handle this packet of data

    r = DataHandler()

    ReadyToSend = 0

  End If

  ScriptEZMain.FreeBSTR bstrData

  ScriptEZMain.FreeString raw_data

  ScriptEZMain_onData = CLng(0)

 

End Function

 

' Notification on Task aborting

'//////////////////////////////

Sub ScriptEZMain_onAbort( lpString )

End Sub

 

 

' Callback for thread pool object (TaskQueueObject)

'//////////////////////////////////////////////////

Function ScriptEZMain_onRun( localStorage )

 

 

  If Err_num <> 0  Or onExit = 3 Then

   ScriptEZMain_onRun = CLng(0) ' stop this task

   Exit Function

  Else

   ScriptEZMain_onRun = CLng(1) ' run again this task

  End If

 

  WScript.Sleep 1000

  Dim hSocket

  Dim Total

 

  hSocket = localStorage

  ScriptEZMain.SetConsoleTitle CStr(StartTime) + " (ScriptEZ.API) - TCP/IP Client sentback(" + _

                               CStr(ReplyCount) +")/" + InputString 

  ScriptEZMain.PeekSocketClientObjectStatsOnReceivedData hSocket

  PendingTasks = ScriptEZMain.GetPendingTaskCount(hThreadPool)

  If ScrollOutput = 0 Then ScriptEZMain.ClearConsole

  Total = ScriptEZMain.SocketSplittedPackets + _

          ScriptEZMain.SocketOverlappedPackets + _

          ScriptEZMain.SocketSimplePackets + _

          ScriptEZMain.SocketWrongPackets

  If Total > 0 Then

   bandwidth_usage = ScriptEZMain.GetSocketObjectInOutBoundBandwidth(hSocket)

   bandwidth_array = Split(bandwidth_usage,"|")

   ScriptEZMain.Echo _

    " ." + CStr(Now) + vbLF+ " (WinSock version " + Socket_ver + ")" + vbLF + vbLF + _

    " Inbound..........:" + CStr(ScriptEZMain.SocketObjectPendingInboundMessageCount(hSocket))+ _

                  " ("+CStr(Round(TotalRecv/1024,2))+" KBytes)"+vbLF + _

    " Outbound.........:" + CStr(ScriptEZMain.SocketObjectPendingOutboundMessageCount(hSocket)) + _

                  " (" +CStr(Round(TotalSend/1024,2))+" KBytes)"+vbLF + _

    " Pending..........:" + CStr(PendingTasks) + vbLF + _

    vbLF + " ." + CStr(LastRecvTime) + vbLF + _

    " Splitted.........:" + CStr(ScriptEZMain.SocketSplittedPackets) + _

                  " ("+CStr(Round((ScriptEZMain.SocketSplittedPackets/Total)*100.0,2))+"%)"+vbLF+_

    " Overlapped.......:" + CStr(ScriptEZMain.SocketOverlappedPackets) + _

                  " ("+CStr(Round((ScriptEZMain.SocketOverlappedPackets/Total)*100.0,2))+"%)"+vbLF+_

    " Simple...........:" + CStr(ScriptEZMain.SocketSimplePackets) + _

                  " ("+CStr(Round((ScriptEZMain.SocketSimplePackets/Total)*100.0,2))+"%)"+vbLF+_

    " Wrong............:" + CStr(ScriptEZMain.SocketWrongPackets) + vbLF +_

    " PooledThreadId...:" + CStr(ScriptEZMain.GetCurrentPooledThreadId) + vbLF +_

    " Bandwidth Usage..:" + vbLF + _

    "  local inbound...:" + bandwidth_array(0) + vbLF + _

    "  local outbound..:" + bandwidth_array(1) + vbLF + _

    "  remote inbound..:" + bandwidth_array(2) + vbLF + _

    "  remote outbound.:" + bandwidth_array(3) + vbLF

    ScriptEZMain.FreeBSTR bandwidth_usage

 End If

 

End Function