Socket Listener and Connecter
Download: 20111227.Listener.Connecter as zip package
;==========================================================================================================================================
; Socket Listener and Connecter scripts.
;==========================================================================================================================================
; In this example there are two separate scripts running on the same computer (can be different computers too).
; There is the "Listener" script that listens for a connection.
; There is the "Connecter" script that connects to the listener script and requests information.
; The "Connecter" script needs to know the IP address and port name/number of the "Listener" computer.
;------------------------------------------------------------------------------------------------------------------------------------------
; These "Listener" and "Connecter" scripts are based on the WinBatch Tech Data Base article
; "Socket Connecter and Listener Script", Article ID: W16381.
;------------------------------------------------------------------------------------------------------------------------------------------
; (c)Detlev Dalitz.20111227.
;==========================================================================================================================================

;----------------------
; The Connecter script.
;----------------------

DirChange (DirScript ())
WinIconize ("")

; Load the appropriate Extender
If WinMetrics (-2) == 3 Then AddExtender ("WWWSK64I.DLL") ; 64-bit
   Else AddExtender ("WWWSK44I.DLL") ; 32-bit

blnCleanup = @FALSE
rbVariable = 3 ; Preset service selector to "Quote".

While @TRUE
   ; Let the user select a service request.
   MyDialogFormat=`WWWDLGED,6.2`
   MyDialogCaption=`Select Service Request`
   MyDialogX=-01
   MyDialogY=-01
   MyDialogWidth=084
   MyDialogHeight=072
   MyDialogNumControls=006
   MyDialogProcedure=`DEFAULT`
   MyDialogFont=`Microsoft Sans Serif|6656|40|34`
   MyDialogTextColor=`0|0|0`
   MyDialogBackground=`DEFAULT,DEFAULT`
   MyDialogConfig=0
   MyDialog001=`005,003,056,012,RADIOBUTTON,"RadioButton_1",rbVariable,"TimeYmdHms",1,1,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
   MyDialog002=`005,015,044,012,RADIOBUTTON,"RadioButton_2",rbVariable,"TimeDate",2,2,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
   MyDialog003=`005,027,044,012,RADIOBUTTON,"RadioButton_3",rbVariable,"Quote",3,3,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
   MyDialog004=`005,039,044,012,RADIOBUTTON,"RadioButton_4",rbVariable,"Stop",4,4,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
   MyDialog005=`003,055,036,012,PUSHBUTTON,"PushButton_OK",DEFAULT,"OK",1,5,32,DEFAULT,DEFAULT,DEFAULT`
   MyDialog006=`043,055,036,012,PUSHBUTTON,"PushButton_Cancel",DEFAULT,"Cancel",0,6,DEFAULT,DEFAULT,DEFAULT,DEFAULT`

   ButtonPushed = Dialog("MyDialog")
   strRequest = ItemExtract (rbVariable, "TimeYmdHms,TimeDate,Quote,Stop", ",")

   ;;; intTestMax = 10                ; Enable this line for test loop.
   ;;; For intTest = 1 To intTestMax  ; Enable this line for test loop.

   hdlSocket = sOpen ()
   If !hdlSocket
      intErrNr = wxGetLastErr ()
      strErrMsg = wxGetErrDesc (intErrNr)
      Message ("Connecter Error", "Cannot open socket." : @LF : intErrNr : @LF : strErrMsg)
      blnCleanup = @TRUE
      Break
   EndIf
   ; If !sConnect (hdlSocket, "alpha.mike-r.com", "qotd") ; Some currently operational QOTD server.
   If !sConnect (hdlSocket, "localhost", "qotd") ; Service = 17 qotd tcp udp. Set parameter "hostaddr" to the host as needed.
      intErrNr = wxGetLastErr ()
      strErrMsg = wxGetErrDesc (intErrNr)
      Message ("Connecter Error", "Connection failed." : @LF : intErrNr : @LF : strErrMsg)
      blnCleanup = @TRUE
      Break
   EndIf
   If !sSendLine (hdlSocket, strRequest)
      intErrNr = wxGetLastErr ()
      strErrMsg = wxGetErrDesc (intErrNr)
      Message ("Connecter Error", "Cannot send service request." : @LF : intErrNr : @LF : strErrMsg)
      blnCleanup = @TRUE
      Break
   EndIf
   strResponse = sRecvLine (hdlSocket, 1024)

   ;;; strSocketInfo = "IP local = " : wxGetInfo (1, hdlSocket) : @LF : "IP remote = " : wxGetInfo (2, hdlSocket)  ; Enable this line for test loop.

   If sClose (hdlSocket)
      hdlSocket = 0
   Else
      intErrNr = wxGetLastErr ()
      strErrMsg = wxGetErrDesc (intErrNr)
      Message ("Connecter Error", "Cannot close socket." : @LF : intErrNr : @LF : strErrMsg)
      blnCleanup = @TRUE
      Break
   EndIf

   Message (strRequest, strResponse) ; Disable this line for test loop.

   ;;; strMsgTitle = "Step " : intTestMax : "." : intTest : "|Connecter"                                       ; Enable this line for test loop.
   ;;; strMsgText = "Request = " : strRequest : @LF : "Response = " : strResponse : @LF : @LF : strSocketInfo  ; Enable this line for test loop.
   ;;; Display (2, strMsgTitle, strMsgText) ; Boxtitle alternative.                                            ; Enable this line for test loop.
   ;;; BoxTitle ("Step " : intTestMax : "." : intTest : "|" : strResponse) ; Display alternative.              ; Enable this line for test loop.

   ;;; Next ; Enable this line for test loop.

EndWhile

If blnCleanup
   If IsDefined (hdlSocket)
      If sClose (hdlSocket)
         hdlSocket = 0
      Else
         intErrNr = wxGetLastErr ()
         strErrMsg = wxGetErrDesc (intErrNr)
         Message ("Connecter Error", "Cannot close socket." : @LF : intErrNr : @LF : strErrMsg)
      EndIf
   EndIf
EndIf

:CANCEL
Exit
;----------------------------------------------------------------------------------------------------------------------





;==========================================================================================================================================
; Socket Listener and Connecter scripts.
;==========================================================================================================================================
; In this example there are two separate scripts running on the same computer (can be different computers too).
; There is the "Listener" script that listens for a connection.
; There is the "Connecter" script that connects to the listener script and requests information.
; The "Connecter" script needs to know the IP address and port name/number of the "Listener" computer.
;------------------------------------------------------------------------------------------------------------------------------------------
; These "Listener" and "Connecter" scripts are based on the WinBatch Tech Data Base article
; "Socket Connecter and Listener Script", Article ID: W16381.
;------------------------------------------------------------------------------------------------------------------------------------------
; (c)Detlev Dalitz.20111227.
;==========================================================================================================================================

;----------------------------------------------------------------------------------------------
; The Listener script, Version 1.
; This script blocks code executing while waiting for a connection (sAccept blockflag = @TRUE).
; Use [Ctrl]+[Break] to exit.
;----------------------------------------------------------------------------------------------

DirChange (DirScript ())

; Load the appropriate Extender
If WinMetrics (-2) == 3 Then AddExtender ("WWWSK64I.DLL") ; 64-bit
   Else AddExtender ("WWWSK44I.DLL") ; 32-bit

; Define array of strings for the "Quote" request.
intQuoteLast = 9
arrQuote = ArrDimension (intQuoteLast + 1)
arrQuote[0] = "Few women admit their age, few men act it."
arrQuote[1] = "Don't anthropomorphize computers. They hate that."
arrQuote[2] = "ANIMAL LOVER ON BOARD. They're delicious."
arrQuote[3] = "My karma ran over my dogma."
arrQuote[4] = "The gene pool could use a little chlorine."
arrQuote[5] = "Time is what keeps things from happening all at once."
arrQuote[6] = "I didn't fight my way to the top of the food chain to be a vegetarian."
arrQuote[7] = "Women who seek to be equal with men lack ambition."
arrQuote[8] = "Your kid may be an honor student but you're still an idiot!"
arrQuote[9] = "If we aren't supposed to eat animals, why are they made with meat?"


BoxOpen ("Listener", "")
strMsgTextExit = "Press [Ctrl]+[Break] to exit."
BoxText (strMsgTextExit)

hdlSocket = sOpen ()
If !hdlSocket
   intErrNr = wxGetLastErr ()
   strErrMsg = wxGetErrDesc (intErrNr)
   Message ("Listener Error", "Cannot open socket." : @LF : intErrNr : @LF : strErrMsg)
   Goto CleanUp
EndIf
If !sListen (hdlSocket, "qotd") ; 17 qotd tcp udp.
   intErrNr = wxGetLastErr ()
   strErrMsg = wxGetErrDesc (intErrNr)
   Message ("Listener Error", "Listen failed." : @LF : intErrNr : @LF : strErrMsg)
   Goto CleanUp
EndIf

intCountServed = 0
blnListenerDown = @FALSE

While @TRUE
   hdlSocketData = sAccept (hdlSocket, @TRUE) ; Block for a connection.
   If !hdlSocketData
      intErrNr = wxGetLastErr ()
      strErrMsg = wxGetErrDesc (intErrNr)
      Message ("Listener Error", "Connection failed." : @LF : intErrNr : @LF : strErrMsg)
      Break
   EndIf

   strRequest = sRecvLine (hdlSocketData, 11) ; Parameter MaxSize must set to one byte more than maximal expected string length.
   Switch @TRUE
   Case !!StrIndexNC (strRequest, "TimeYmdHms", 1, @FWDSCAN)
      strResponse = TimeYmdHms ()
      Break
   Case !!StrIndexNC (strRequest, "TimeDate", 1, @FWDSCAN)
      strResponse = TimeDate ()
      Break
   Case !!StrIndexNC (strRequest, "Quote", 1, @FWDSCAN)
      strResponse = arrQuote[Random (intQuoteLast) ]
      Break
   Case !!StrIndexNC (strRequest, "Stop", 1, @FWDSCAN)
      strResponse = "Service will be shut down now."
      blnListenerDown = @TRUE
      Break
   Case @TRUE
      strResponse = `Error: Service can only respond to "TimeYmdHms", "TimeDate", "Quote", "Stop".`
   EndSwitch

   If !sSendLine (hdlSocketData, strResponse)
      intErrNr = wxGetLastErr ()
      strErrMsg = wxGetErrDesc (intErrNr)
      Message ("Listener Error", "Cannot send service response." : @LF : intErrNr : @LF : strErrMsg)
   EndIf

   intCountServed = intCountServed + 1

   strMsgTitle = "Listener Status Info"
   BoxTitle (strMsgTitle)

   strMsgText = "Served = " : intCountServed
   strMsgText = strMsgText : @LF : "Request = " : strRequest
   strMsgText = strMsgText : @LF : "Response = " : strResponse
   ;strMsgText = strMsgText : @LF : "IP local = " : wxGetInfo (1, hdlSocketData)  ; In this case always, IP local = 0.
   ;strMsgText = strMsgText : @LF : "IP remote = " : wxGetInfo (2, hdlSocketData) ; In this case always, IP remote = 0.
   strMsgText = strMsgText : @LF : @LF : strMsgTextExit
   BoxText (strMsgText)

   If sClose (hdlSocketData)
      hdlSocketData = 0
   Else
      intErrNr = wxGetLastErr ()
      strErrMsg = wxGetErrDesc (intErrNr)
      Message ("Listener Error", "Cannot close data socket." : @LF : intErrNr : @LF : strErrMsg)
      Break
   EndIf

   If blnListenerDown
      For intI = 5 To 1 By -1
         strMsgText  = "*** Stop service requested, down in " : intI : " sec. ***"
         BoxText (strMsgText)
         TimeDelay (1)
      Next
      Break
   EndIf
EndWhile

:CleanUp
If IsDefined (hdlSocketData)
   If sClose (hdlSocketData)
      hdlSocketData = 0
   Else
      intErrNr = wxGetLastErr ()
      strErrMsg = wxGetErrDesc (intErrNr)
      Message ("Listener Error", "Cannot close data socket." : @LF : intErrNr : @LF : strErrMsg)
   EndIf
EndIf
If IsDefined (hdlSocket)
   If sClose (hdlSocket)
      hdlSocket = 0
   Else
      intErrNr = wxGetLastErr ()
      strErrMsg = wxGetErrDesc (intErrNr)
      Message ("Listener Error", "Cannot close socket." : @LF : intErrNr : @LF : strErrMsg)
   EndIf
EndIf

:CANCEL
Exit
;----------------------------------------------------------------------------------------------------------------------





;==========================================================================================================================================
; Socket Listener and Connecter scripts.
;==========================================================================================================================================
; In this example there are two separate scripts running on the same computer (can be different computers too).
; There is the "Listener" script that listens for a connection.
; There is the "Connecter" script that connects to the listener script and requests information.
; The "Connecter" script needs to know the IP address and port name/number of the "Listener" computer.
;------------------------------------------------------------------------------------------------------------------------------------------
; These "Listener" and "Connecter" scripts are based on the WinBatch Tech Data Base article
; "Socket Connecter and Listener Script", Article ID: W16381.
;------------------------------------------------------------------------------------------------------------------------------------------
; (c)Detlev Dalitz.20111227.
;==========================================================================================================================================

;-----------------------------------------------------------------------------------
; The Listener script, Version 2.
; This script does not block code execution while waiting for a connection,
; but waits in a while loop to allow other things to do (sAccept blockflag = @FALSE).
; Use [Ctrl]+[Break] to exit.
;-----------------------------------------------------------------------------------

DirChange (DirScript ())

; Load the appropriate Extender
If WinMetrics (-2) == 3 Then AddExtender ("WWWSK64I.DLL") ; 64-bit
   Else AddExtender ("WWWSK44I.DLL") ; 32-bit

; Define array of strings for the "Quote" request.
intQuoteLast = 9
arrQuote = ArrDimension (intQuoteLast + 1)
arrQuote[0] = "Few women admit their age, few men act it."
arrQuote[1] = "Don't anthropomorphize computers. They hate that."
arrQuote[2] = "ANIMAL LOVER ON BOARD. They're delicious."
arrQuote[3] = "My karma ran over my dogma."
arrQuote[4] = "The gene pool could use a little chlorine."
arrQuote[5] = "Time is what keeps things from happening all at once."
arrQuote[6] = "I didn't fight my way to the top of the food chain to be a vegetarian."
arrQuote[7] = "Women who seek to be equal with men lack ambition."
arrQuote[8] = "Your kid may be an honor student but you're still an idiot!"
arrQuote[9] = "If we aren't supposed to eat animals, why are they made with meat?"


BoxOpen ("Listener", "")
strMsgTextExit = "Press [Ctrl]+[Break] to exit."
BoxText (strMsgTextExit)

hdlSocket = sOpen ()
If !hdlSocket
   intErrNr = wxGetLastErr ()
   strErrMsg = wxGetErrDesc (intErrNr)
   Message ("Listener Error", "Cannot open socket." : @LF : intErrNr : @LF : strErrMsg)
   Goto CleanUp
EndIf
If !sListen (hdlSocket, "qotd") ; 17 qotd tcp udp.
   intErrNr = wxGetLastErr ()
   strErrMsg = wxGetErrDesc (intErrNr)
   Message ("Listener Error", "Listen failed." : @LF : intErrNr : @LF : strErrMsg)
   Goto CleanUp
EndIf

intCountServed = 0
blnListenerDown = @FALSE

While @TRUE
   While @TRUE
      hdlSocketData = sAccept (hdlSocket, @FALSE) ; Do not block for a connection.
      If hdlSocketData Then Break
      intErrNr = wxGetLastErr ()
      strErrMsg = wxGetErrDesc (intErrNr)
      ; BoxText ("Listener Error: Connection failed." : @LF : intErrNr : @LF : strErrMsg)
      BoxTitle ("Listener Error: Connection failed." : "|" : intErrNr : "|" : strErrMsg)
      ; TimeDelay (1)

      ; Here do some other processing.
      ; For example ... just for fun ...
      intTargetLength = 10
      strTarget = StrFill ("-", intTargetLength)
      For intLen = 1 To intTargetLength
         strOut = StrOverlay (strTarget, ">", "", intLen, 1)
         strOut = StrCat ("[", strOut, "]")
         BoxText (strOut)
         TimeDelay (0.2 / intTargetLength)
      Next
      For intLen = intTargetLength To 1 By -1
         strOut = StrOverlay (strTarget, "<", "", intLen, 1)
         strOut = StrCat ("[", strOut, "]")
         BoxText (strOut)
         TimeDelay (0.2 / intTargetLength)
      Next
   EndWhile

   strRequest = sRecvLine (hdlSocketData, 11) ; Parameter MaxSize must be set to one byte more than maximal expected string length.

   Switch @TRUE
   Case !!StrIndexNC (strRequest, "TimeYmdHms", 1, @FWDSCAN)
      strResponse = TimeYmdHms ()
      Break
   Case !!StrIndexNC (strRequest, "TimeDate", 1, @FWDSCAN)
      strResponse = TimeDate ()
      Break
   Case !!StrIndexNC (strRequest, "Quote", 1, @FWDSCAN)
      strResponse = arrQuote[Random (intQuoteLast) ]
      Break
   Case !!StrIndexNC (strRequest, "Stop", 1, @FWDSCAN)
      strResponse = "Service will be shut down now."
      blnListenerDown = @TRUE
      Break
   Case @TRUE
      strResponse = `Error: Service can only respond to "TimeYmdHms", "TimeDate", "Quote", "Stop".`
   EndSwitch

   If !sSendLine (hdlSocketData, strResponse)
      intErrNr = wxGetLastErr ()
      strErrMsg = wxGetErrDesc (intErrNr)
      Message ("Listener Error", "Cannot send service response." : @LF : intErrNr : @LF : strErrMsg)
   EndIf

   intCountServed = intCountServed + 1

   strMsgTitle = "Listener Status Info"
   BoxTitle (strMsgTitle)

   strMsgText = "Served = " : intCountServed
   strMsgText = strMsgText : @LF : "Request = " : strRequest
   strMsgText = strMsgText : @LF : "Response = " : strResponse
   ;strMsgText = strMsgText : @LF : "IP local = " : wxGetInfo (1, hdlSocketData)  ; In this case always, IP local = 0.
   ;strMsgText = strMsgText : @LF : "IP remote = " : wxGetInfo (2, hdlSocketData) ; In this case always, IP remote = 0.
   strMsgText = strMsgText : @LF : @LF : strMsgTextExit
   BoxText (strMsgText)
   TimeDelay (1)

   If sClose (hdlSocketData)
      hdlSocketData = 0
   Else
      intErrNr = wxGetLastErr ()
      strErrMsg = wxGetErrDesc (intErrNr)
      Message ("Listener Error", "Cannot close data socket." : @LF : intErrNr : @LF : strErrMsg)
      Break
   EndIf

   If blnListenerDown
      For intI = 5 To 1 By -1
         strMsgText  = "*** Stop service requested, down in " : intI : " sec. ***"
         BoxText (strMsgText)
         TimeDelay (1)
      Next
      Break
   EndIf
EndWhile

:CleanUp
If IsDefined (hdlSocketData)
   If sClose (hdlSocketData)
      hdlSocketData = 0
   Else
      intErrNr = wxGetLastErr ()
      strErrMsg = wxGetErrDesc (intErrNr)
      Message ("Listener Error", "Cannot close data socket." : @LF : intErrNr : @LF : strErrMsg)
   EndIf
EndIf
If IsDefined (hdlSocket)
   If sClose (hdlSocket)
      hdlSocket = 0
   Else
      intErrNr = wxGetLastErr ()
      strErrMsg = wxGetErrDesc (intErrNr)
      Message ("Listener Error", "Cannot close socket." : @LF : intErrNr : @LF : strErrMsg)
   EndIf
EndIf

:CANCEL
Exit
;----------------------------------------------------------------------------------------------------------------------