;==========================================================================================================================================; 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 ExtenderIfWinMetrics(-2)==3ThenAddExtender("WWWSK64I.DLL"); 64-bitElseAddExtender("WWWSK44I.DLL"); 32-bitblnCleanup=@FALSErbVariable=3; Preset service selector to "Quote".While@TRUE; Let the user select a service request.MyDialogFormat=`WWWDLGED,6.2`MyDialogCaption=`Select Service Request`MyDialogX=-01MyDialogY=-01MyDialogWidth=084MyDialogHeight=072MyDialogNumControls=006MyDialogProcedure=`DEFAULT`MyDialogFont=`Microsoft Sans Serif|6656|40|34`MyDialogTextColor=`0|0|0`MyDialogBackground=`DEFAULT,DEFAULT`MyDialogConfig=0MyDialog001=`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!hdlSocketintErrNr=wxGetLastErr()strErrMsg=wxGetErrDesc(intErrNr)Message("Connecter Error","Cannot open socket.":@LF:intErrNr:@LF:strErrMsg)blnCleanup=@TRUEBreakEndIf; 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=@TRUEBreakEndIfIf!sSendLine(hdlSocket,strRequest)intErrNr=wxGetLastErr()strErrMsg=wxGetErrDesc(intErrNr)Message("Connecter Error","Cannot send service request.":@LF:intErrNr:@LF:strErrMsg)blnCleanup=@TRUEBreakEndIfstrResponse=sRecvLine(hdlSocket,1024);;; strSocketInfo = "IP local = " : wxGetInfo (1, hdlSocket) : @LF : "IP remote = " : wxGetInfo (2, hdlSocket) ; Enable this line for test loop.IfsClose(hdlSocket)hdlSocket=0ElseintErrNr=wxGetLastErr()strErrMsg=wxGetErrDesc(intErrNr)Message("Connecter Error","Cannot close socket.":@LF:intErrNr:@LF:strErrMsg)blnCleanup=@TRUEBreakEndIfMessage(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.EndWhileIfblnCleanupIfIsDefined(hdlSocket)IfsClose(hdlSocket)hdlSocket=0ElseintErrNr=wxGetLastErr()strErrMsg=wxGetErrDesc(intErrNr)Message("Connecter Error","Cannot close socket.":@LF:intErrNr:@LF:strErrMsg)EndIfEndIfEndIf:CANCELExit;----------------------------------------------------------------------------------------------------------------------;==========================================================================================================================================; 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 ExtenderIfWinMetrics(-2)==3ThenAddExtender("WWWSK64I.DLL"); 64-bitElseAddExtender("WWWSK44I.DLL"); 32-bit; Define array of strings for the "Quote" request.intQuoteLast=9arrQuote=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!hdlSocketintErrNr=wxGetLastErr()strErrMsg=wxGetErrDesc(intErrNr)Message("Listener Error","Cannot open socket.":@LF:intErrNr:@LF:strErrMsg)GotoCleanUpEndIfIf!sListen(hdlSocket,"qotd"); 17 qotd tcp udp.intErrNr=wxGetLastErr()strErrMsg=wxGetErrDesc(intErrNr)Message("Listener Error","Listen failed.":@LF:intErrNr:@LF:strErrMsg)GotoCleanUpEndIfintCountServed=0blnListenerDown=@FALSEWhile@TRUEhdlSocketData=sAccept(hdlSocket,@TRUE); Block for a connection.If!hdlSocketDataintErrNr=wxGetLastErr()strErrMsg=wxGetErrDesc(intErrNr)Message("Listener Error","Connection failed.":@LF:intErrNr:@LF:strErrMsg)BreakEndIfstrRequest=sRecvLine(hdlSocketData,11); Parameter MaxSize must set to one byte more than maximal expected string length.Switch@TRUECase!!StrIndexNC(strRequest,"TimeYmdHms",1,@FWDSCAN)strResponse=TimeYmdHms()BreakCase!!StrIndexNC(strRequest,"TimeDate",1,@FWDSCAN)strResponse=TimeDate()BreakCase!!StrIndexNC(strRequest,"Quote",1,@FWDSCAN)strResponse=arrQuote[Random(intQuoteLast)]BreakCase!!StrIndexNC(strRequest,"Stop",1,@FWDSCAN)strResponse="Service will be shut down now."blnListenerDown=@TRUEBreakCase@TRUEstrResponse=`Error: Service can only respond to "TimeYmdHms", "TimeDate", "Quote", "Stop".`EndSwitchIf!sSendLine(hdlSocketData,strResponse)intErrNr=wxGetLastErr()strErrMsg=wxGetErrDesc(intErrNr)Message("Listener Error","Cannot send service response.":@LF:intErrNr:@LF:strErrMsg)EndIfintCountServed=intCountServed+1strMsgTitle="Listener Status Info"BoxTitle(strMsgTitle)strMsgText="Served = ":intCountServedstrMsgText=strMsgText:@LF:"Request = ":strRequeststrMsgText=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:strMsgTextExitBoxText(strMsgText)IfsClose(hdlSocketData)hdlSocketData=0ElseintErrNr=wxGetLastErr()strErrMsg=wxGetErrDesc(intErrNr)Message("Listener Error","Cannot close data socket.":@LF:intErrNr:@LF:strErrMsg)BreakEndIfIfblnListenerDownForintI=5To1By-1strMsgText="*** Stop service requested, down in ":intI:" sec. ***"BoxText(strMsgText)TimeDelay(1)NextBreakEndIfEndWhile:CleanUpIfIsDefined(hdlSocketData)IfsClose(hdlSocketData)hdlSocketData=0ElseintErrNr=wxGetLastErr()strErrMsg=wxGetErrDesc(intErrNr)Message("Listener Error","Cannot close data socket.":@LF:intErrNr:@LF:strErrMsg)EndIfEndIfIfIsDefined(hdlSocket)IfsClose(hdlSocket)hdlSocket=0ElseintErrNr=wxGetLastErr()strErrMsg=wxGetErrDesc(intErrNr)Message("Listener Error","Cannot close socket.":@LF:intErrNr:@LF:strErrMsg)EndIfEndIf:CANCELExit;----------------------------------------------------------------------------------------------------------------------;==========================================================================================================================================; 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 ExtenderIfWinMetrics(-2)==3ThenAddExtender("WWWSK64I.DLL"); 64-bitElseAddExtender("WWWSK44I.DLL"); 32-bit; Define array of strings for the "Quote" request.intQuoteLast=9arrQuote=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!hdlSocketintErrNr=wxGetLastErr()strErrMsg=wxGetErrDesc(intErrNr)Message("Listener Error","Cannot open socket.":@LF:intErrNr:@LF:strErrMsg)GotoCleanUpEndIfIf!sListen(hdlSocket,"qotd"); 17 qotd tcp udp.intErrNr=wxGetLastErr()strErrMsg=wxGetErrDesc(intErrNr)Message("Listener Error","Listen failed.":@LF:intErrNr:@LF:strErrMsg)GotoCleanUpEndIfintCountServed=0blnListenerDown=@FALSEWhile@TRUEWhile@TRUEhdlSocketData=sAccept(hdlSocket,@FALSE); Do not block for a connection.IfhdlSocketDataThenBreakintErrNr=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=10strTarget=StrFill("-",intTargetLength)ForintLen=1TointTargetLengthstrOut=StrOverlay(strTarget,">","",intLen,1)strOut=StrCat("[",strOut,"]")BoxText(strOut)TimeDelay(0.2/intTargetLength)NextForintLen=intTargetLengthTo1By-1strOut=StrOverlay(strTarget,"<","",intLen,1)strOut=StrCat("[",strOut,"]")BoxText(strOut)TimeDelay(0.2/intTargetLength)NextEndWhilestrRequest=sRecvLine(hdlSocketData,11); Parameter MaxSize must be set to one byte more than maximal expected string length.Switch@TRUECase!!StrIndexNC(strRequest,"TimeYmdHms",1,@FWDSCAN)strResponse=TimeYmdHms()BreakCase!!StrIndexNC(strRequest,"TimeDate",1,@FWDSCAN)strResponse=TimeDate()BreakCase!!StrIndexNC(strRequest,"Quote",1,@FWDSCAN)strResponse=arrQuote[Random(intQuoteLast)]BreakCase!!StrIndexNC(strRequest,"Stop",1,@FWDSCAN)strResponse="Service will be shut down now."blnListenerDown=@TRUEBreakCase@TRUEstrResponse=`Error: Service can only respond to "TimeYmdHms", "TimeDate", "Quote", "Stop".`EndSwitchIf!sSendLine(hdlSocketData,strResponse)intErrNr=wxGetLastErr()strErrMsg=wxGetErrDesc(intErrNr)Message("Listener Error","Cannot send service response.":@LF:intErrNr:@LF:strErrMsg)EndIfintCountServed=intCountServed+1strMsgTitle="Listener Status Info"BoxTitle(strMsgTitle)strMsgText="Served = ":intCountServedstrMsgText=strMsgText:@LF:"Request = ":strRequeststrMsgText=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:strMsgTextExitBoxText(strMsgText)TimeDelay(1)IfsClose(hdlSocketData)hdlSocketData=0ElseintErrNr=wxGetLastErr()strErrMsg=wxGetErrDesc(intErrNr)Message("Listener Error","Cannot close data socket.":@LF:intErrNr:@LF:strErrMsg)BreakEndIfIfblnListenerDownForintI=5To1By-1strMsgText="*** Stop service requested, down in ":intI:" sec. ***"BoxText(strMsgText)TimeDelay(1)NextBreakEndIfEndWhile:CleanUpIfIsDefined(hdlSocketData)IfsClose(hdlSocketData)hdlSocketData=0ElseintErrNr=wxGetLastErr()strErrMsg=wxGetErrDesc(intErrNr)Message("Listener Error","Cannot close data socket.":@LF:intErrNr:@LF:strErrMsg)EndIfEndIfIfIsDefined(hdlSocket)IfsClose(hdlSocket)hdlSocket=0ElseintErrNr=wxGetLastErr()strErrMsg=wxGetErrDesc(intErrNr)Message("Listener Error","Cannot close socket.":@LF:intErrNr:@LF:strErrMsg)EndIfEndIf:CANCELExit;----------------------------------------------------------------------------------------------------------------------