Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.
Keywords: WinSock Socket IPC Interprocess Communication Communicate TCP IP
Note: This Winsock IPC (interprocess communication) script is dependant on the QOTD port (17) being enabled. By default on Windows 7 this port is NOT enabled. To enable this port you must go to the Control Panel|Programs|Turn Windows Features On/Off|Check the box next to Simple TCPIP Services.
Reference: http://www.windowsnetworking.com/articles_tutorials/windows-7-simple-tcpip-services-what-how.html
;****************************************************************** ;* Server executable * ;****************************************************************** #DefineFunction PopupMenu(choices) user32=StrCat(DirWindows(1),"User32.DLL") MF_STRING = 0 TPM_RETURNCMD = 256 TPM_LEFTBUTTON = 0 hwnd=DllHwnd("") ;create menu hmenu = DllCall(user32,long:"CreatePopupMenu") choicecount=ItemCount(choices,"|") For xx=1 To choicecount thischoice=ItemExtract(xx,choices,"|") DllCall(user32,long:"AppendMenuA",long:hmenu, long:MF_STRING, long:xx, lpstr:thischoice) Next DllCall(user32,long:"SetForegroundWindow",long:hwnd) mc = MouseInfo(3) mcx=ItemExtract(1,mc," ") mcy=ItemExtract(2,mc," ") wFlags=TPM_RETURNCMD|TPM_LEFTBUTTON s=DllCall(user32,long:"TrackPopupMenu",long:hmenu, long:wFlags, long:mcx, long:mcy, long:0, long:hwnd, lpnull) ;Destroy Menu DllCall(user32,long:"DestroyMenu",long:hmenu) Return(s) #EndFunction IntControl(12, 5, 0, 0, 0) IntControl (1008, 1, 0, 0, 0) IntControl(1007, 1, 1, `CommandExecute`, `Pic_CommandExecute.ico|0`) DirChange(DirScript()) Home = DirScript() ListenerStatus = `` Ini = StrCat(Home,`Ini_CommandExecute.ini`) Log = StrCat(Home,`Log_CommandExecute.log`) AddExtender ("WWWSK44I.DLL") ; 32-bit 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 GoSub PopulateMenu hdlSocketData = sAccept(hdlSocket, @FALSE) ; Do not block for a connection. If hdlSocketData Then Break intErrNr = wxGetLastErr() strErrMsg = wxGetErrDesc(intErrNr) TimeDelay(1) GoSub Tray EndWhile Command2Perform = sRecvLine(hdlSocketData, 513) ; Parameter MaxSize must be set to one byte more than maximal expected string length. If Command2Perform == `*Stop*` Then blnListenerDown = @TRUE If !blnListenerDown Then oldvalue=IntControl(92, "Disable", 0, 0, 0) ; disable file resirection RunShell(Environment(`COMSPEC`),StrCat(`/c `,Command2Perform),Home,@HIDDEN,@NOWAIT) IntControl(92, "revert", oldvalue, 0, 0) ; restore previous file redirection setting strResponse = `The command was started.` If !sSendLine(hdlSocketData, strResponse) intErrNr = wxGetLastErr() strErrMsg = wxGetErrDesc(intErrNr) IntControl(1007, 4, 1, "Cannot send service response." : @LF : intErrNr : @LF : strErrMsg, `Pic_ErrCommandExecute.ico|0`) EndIf intCountServed = intCountServed + 1 ListenerStatus = `Last execution:` ListenerStatus = ListenerStatus : @CRLF : "Served = " : intCountServed ListenerStatus = ListenerStatus : @CRLF : "Request = " : Command2Perform ListenerStatus = ListenerStatus : @CRLF : "Response = " : strResponse ListenerStatus = ListenerStatus : @CRLF : "IP local = " : wxGetInfo (1, hdlSocketData) ListenerStatus = ListenerStatus : @CRLF : "IP remote = " : wxGetInfo (2, hdlSocketData) ErrorMode(@OFF) handle = FileOpen(Log, "Append") If handle == 0 Then TimeDelay(3) handle = FileOpen(Log, "Append") EndIf If handle <> 0 Then If intCountServed == 1 Then FileWrite(handle, `**************************************************************************`) If intCountServed > 1 Then FileWrite(handle, ``) FileWrite(handle, StrCat(`[`,TimeYmdHms(),`]`)) FileWrite(handle, ListenerStatus) FileClose(handle) EndIf ErrorMode(@CANCEL) If sClose(hdlSocketData) hdlSocketData = 0 Else intErrNr = wxGetLastErr() strErrMsg = wxGetErrDesc(intErrNr) Break EndIf Else For intI = 2 To 1 By -1 strMsgText = "*** Stop service requested, down in " : intI : " sec. ***" TimeDelay(1) Next IntControl(1007, 2,0, "", "") strResponse = `The service was stopped.` If !sSendLine(hdlSocketData, strResponse) intErrNr = wxGetLastErr() strErrMsg = wxGetErrDesc(intErrNr) IntControl(1007, 4, 1, "Cannot send service response." : @LF : intErrNr : @LF : strErrMsg, `Pic_ErrCommandExecute.ico|0`) EndIf intCountServed = intCountServed + 1 ListenerStatus = `Last execution:` ListenerStatus = ListenerStatus : @CRLF : "Served = " : intCountServed ListenerStatus = ListenerStatus : @CRLF : "Request = " : Command2Perform ListenerStatus = ListenerStatus : @CRLF : "Response = " : strResponse ListenerStatus = ListenerStatus : @CRLF : "IP local = " : wxGetInfo (1, hdlSocketData) ListenerStatus = ListenerStatus : @CRLF : "IP remote = " : wxGetInfo (2, hdlSocketData) ErrorMode(@OFF) handle = FileOpen(Log, "Append") If handle == 0 Then TimeDelay(3) handle = FileOpen(Log, "Append") EndIf If handle <> 0 Then If intCountServed > 1 Then FileWrite(handle, ``) FileWrite(handle, StrCat(`[`,TimeYmdHms(),`]`)) FileWrite(handle, ListenerStatus) FileClose(handle) EndIf ErrorMode(@CANCEL) Break EndIf EndWhile :CleanUp If IsDefined(hdlSocketData) If sClose(hdlSocketData) hdlSocketData = 0 Else intErrNr = wxGetLastErr() strErrMsg = wxGetErrDesc(intErrNr) EndIf EndIf If IsDefined(hdlSocket) If sClose(hdlSocket) hdlSocket = 0 Else intErrNr = wxGetLastErr() strErrMsg = wxGetErrDesc(intErrNr) EndIf EndIf :CANCEL IntControl(1007, 2,0, "", "") Exit :PopulateMenu all_section=IniItemizePvt("",Ini) nb_section = ItemCount(all_section, @TAB) liste_prog="" last=0 For j=1 To nb_section Program = ItemExtract(j, all_section, @TAB) Exe = IniReadPvt(Program, "prog", "", Ini) If( Exe != "") Then liste_prog=StrCat(liste_prog,Program,"|") last=last+1 EndIf Next liste_prog=StrCat(liste_prog,"Exit") last=last+1 Return :Tray Button=IntControl(1007, 0, 0, "", "") If Button == 2 Then ;Only display menu on right-click :Tray1 choice=PopupMenu(liste_prog) Program = ItemExtract(choice, all_section, @TAB) AppSelected=IniReadPvt(Program, "Prog", "", Ini) If (choice == last) Then IntControl(1007, 2,0, "", "") ;clear icon from tray Exit EndIf If Program == `About` Then GoSub AboutInfo ;About If Program <> `` && Program <> `About` && choice < last Then Application = ItemExtract(1,AppSelected,` `) Params = `` For k = 2 To ItemCount(AppSelected,` `) Params = StrCat(Params,` `,ItemExtract(k,AppSelected,` `)) Next If Application <> `` Then ShellExecute(Application,Params,``,@NORMAL,``) EndIf EndIf Return :AboutInfo AboutInfo = StrCat(`This script waits for commands to execute from a remote client commponent, carries them out, then waits for another command.`,@CRLF,@CRLF,`This script also presents any number of shortcuts via the ini file for the local user to consume.`,@CRLF,@CRLF,ListenerStatus) MyDialogFormat=`WWWDLGED,6.1` MyDialogCaption=`About StopScreenSaver` MyDialogX=182 MyDialogY=196 MyDialogWidth=342 MyDialogHeight=111 MyDialogNumControls=003 MyDialogProcedure=`DEFAULT` MyDialogFont=`DEFAULT` MyDialogTextColor=`DEFAULT` MyDialogBackground=`DEFAULT,255|255|255` MyDialogConfig=0 MyDialog001=`151,093,036,012,PUSHBUTTON,DEFAULT,"OK",1,1,32,DEFAULT,DEFAULT,DEFAULT` MyDialog002=StrCat(`003,005,050,060,PICTURE,DEFAULT,DEFAULT,DEFAULT,100,DEFAULT,DEFAULT,DEFAULT,"`,Home,`Pic_CommandExecute.bmp"`) MyDialog003=`057,005,278,060,MULTILINEBOX,AboutInfo,DEFAULT,DEFAULT,6,8,DEFAULT,DEFAULT,DEFAULT` ButtonPushed=Dialog("MyDialog") Return
;****************************************************************** ;* Client executable * ;****************************************************************** Home = DirScript() SystemDrive = Environment(`SystemDrive`) SystemRoot = Environment(`SystemRoot`) Download = @FALSE File2Download = `` FileName2Download = `` Option_Flag = 5 ReturnToMenu = @FALSE ServerName = `Default_ComputerName` DirChange(DirScript()) WinIconize("") AddExtender ("WWWSK44I.DLL") While @TRUE ErrorDisplayed = @FALSE blnCleanup = @FALSE :Menu ReturnToMenu = @FALSE MyDialogFormat=`WWWDLGED,6.1` MyDialogCaption=`Remote Execution client` MyDialogX=223 MyDialogY=173 MyDialogWidth=340 MyDialogHeight=135 MyDialogNumControls=025 MyDialogProcedure=`DEFAULT` MyDialogFont=`DEFAULT` MyDialogTextColor=`DEFAULT` MyDialogBackground=`DEFAULT,DEFAULT` MyDialogConfig=0 MyDialog001=`003,115,036,012,PUSHBUTTON,DEFAULT,"OK",1,20,32,DEFAULT,DEFAULT,DEFAULT` MyDialog002=`299,115,036,012,PUSHBUTTON,DEFAULT,"Cancel",0,21,DEFAULT,DEFAULT,DEFAULT,DEFAULT` MyDialog003=`001,005,060,008,STATICTEXT,DEFAULT,"Server:",DEFAULT,100,DEFAULT,DEFAULT,DEFAULT,DEFAULT` MyDialog004=`065,003,094,012,EDITBOX,ServerName,DEFAULT,DEFAULT,1,DEFAULT,DEFAULT,DEFAULT,DEFAULT` MyDialog005=`001,099,060,008,STATICTEXT,DEFAULT,"Command to carry out:",DEFAULT,101,DEFAULT,DEFAULT,DEFAULT,DEFAULT` MyDialog006=`065,097,270,012,EDITBOX,Command2Perform,DEFAULT,DEFAULT,10,DEFAULT,DEFAULT,DEFAULT,DEFAULT` MyDialog007=`005,029,008,008,RADIOBUTTON,Option_Flag,DEFAULT,1,129,DEFAULT,DEFAULT,DEFAULT,DEFAULT` MyDialog008=`015,027,074,012,PUSHBUTTON,DEFAULT,"Start iTunes",2,3,DEFAULT,DEFAULT,DEFAULT,DEFAULT` MyDialog009=`005,041,008,008,RADIOBUTTON,Option_Flag,DEFAULT,2,130,DEFAULT,DEFAULT,DEFAULT,DEFAULT` MyDialog010=`015,039,074,012,PUSHBUTTON,DEFAULT,"Start AutoHotkey",3,4,DEFAULT,DEFAULT,DEFAULT,DEFAULT` MyDialog011=`005,053,008,008,RADIOBUTTON,Option_Flag,DEFAULT,3,131,DEFAULT,DEFAULT,DEFAULT,DEFAULT` MyDialog012=`015,051,074,012,PUSHBUTTON,DEFAULT,"Start TouchControl",4,5,DEFAULT,DEFAULT,DEFAULT,DEFAULT` MyDialog013=`001,017,334,074,GROUPBOX,DEFAULT,"Quick Command Choices",DEFAULT,2,DEFAULT,DEFAULT,DEFAULT,DEFAULT` MyDialog014=`005,065,008,008,RADIOBUTTON,Option_Flag,DEFAULT,4,132,DEFAULT,DEFAULT,DEFAULT,DEFAULT` MyDialog015=`015,063,074,012,PUSHBUTTON,DEFAULT,"Start Media Center",5,6,DEFAULT,DEFAULT,DEFAULT,DEFAULT` MyDialog016=`005,077,008,008,RADIOBUTTON,Option_Flag,DEFAULT,5,133,DEFAULT,DEFAULT,DEFAULT,DEFAULT` MyDialog017=`015,075,074,012,PUSHBUTTON,DEFAULT,"Use Manual command",6,7,DEFAULT,DEFAULT,DEFAULT,DEFAULT` MyDialog018=`097,029,008,008,RADIOBUTTON,Option_Flag,DEFAULT,6,164,DEFAULT,DEFAULT,DEFAULT,DEFAULT` MyDialog019=`107,027,074,012,PUSHBUTTON,DEFAULT,"Stop the service",7,8,DEFAULT,DEFAULT,DEFAULT,DEFAULT` MyDialog020=`097,041,008,008,RADIOBUTTON,Option_Flag,DEFAULT,7,165,DEFAULT,DEFAULT,DEFAULT,DEFAULT` MyDialog021=`107,039,074,012,PUSHBUTTON,DEFAULT,"Start MediaBrowser Service",8,9,DEFAULT,DEFAULT,DEFAULT,DEFAULT` MyDialog022=`097,053,008,008,RADIOBUTTON,Option_Flag,DEFAULT,8,166,DEFAULT,DEFAULT,DEFAULT,DEFAULT` MyDialog023=`107,051,074,012,PUSHBUTTON,DEFAULT,"Shutdown the workstation",9,10,DEFAULT,DEFAULT,DEFAULT,DEFAULT` MyDialog024=`097,065,008,008,RADIOBUTTON,Option_Flag,DEFAULT,9,167,DEFAULT,DEFAULT,DEFAULT,DEFAULT` MyDialog025=`107,063,074,012,PUSHBUTTON,DEFAULT,"Hibernate the workstation",10,11,DEFAULT,DEFAULT,DEFAULT,DEFAULT` ButtonPushed=Dialog("MyDialog") Select ButtonPushed Case 2 Command2Perform = `start "iTunes" /MIN "%%ProgramFiles(x86)%%\iTunes\iTunes.exe"` Option_Flag = 1 ReturnToMenu = @TRUE Break Case 3 Command2Perform = `start "AutoHotkey" /MIN "C:\Program Files (x86)\AutoHotkey\AutoHotkey.exe"` Option_Flag = 2 ReturnToMenu = @TRUE Break Case 4 Command2Perform = `start "TouchControl" /MIN "D:\Program Files (x86)\Touch-IR\Touch-IRServer\Touch-IR.exe"` Option_Flag = 3 ReturnToMenu = @TRUE Break Case 5 Command2Perform = `start "Media Center" /MIN "%%WinDir%%\ehome\ehshell.exe"` Option_Flag = 4 ReturnToMenu = @TRUE Break Case 6 Command2Perform = `` Option_Flag = 5 ReturnToMenu = @TRUE Break Case 7 If AskYesNo(`Are you sure?`,`This will prevent you from sending anything else to the machine, are you sure you want to terminate the service?`) Then Command2Perform = `*Stop*` Option_Flag = 6 EndIf ReturnToMenu = @TRUE Break Case 8 Command2Perform = `start "MediaBrowserService" /MIN "D:\Program Files (x86)\MediaBrowser\MediaBrowser\MediaBrowserService.exe"` Option_Flag = 7 ReturnToMenu = @TRUE Break Case 9 If AskYesNo(`Are you sure?`,`This will prevent you from sending anything else to the machine, are you sure you want to shutdown the machine?`) Then Command2Perform = `Shutdown /s /t 10 /c "This system is shutting down due to a remote command"` Option_Flag = 8 EndIf ReturnToMenu = @TRUE Break Case 10 If AskYesNo(`Are you sure?`,`This will prevent you from sending anything else to the machine, are you sure you want to hibernate the machine?`) Then Command2Perform = `Shutdown /h /f` Option_Flag = 9 EndIf ReturnToMenu = @TRUE Break EndSelect If ReturnToMenu Then Goto Menu hdlSocket = sOpen () If !hdlSocket intErrNr = wxGetLastErr () strErrMsg = wxGetErrDesc (intErrNr) Message ("Connecter Error", "Cannot open socket." : @LF : intErrNr : @LF : strErrMsg) blnCleanup = @TRUE ErrorDisplayed = @TRUE EndIf ; If !sConnect (hdlSocket, "alpha.mike-r.com", "qotd") ; Some currently operational QOTD server. If !ErrorDisplayed && !sConnect (hdlSocket, ServerName, "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 ErrorDisplayed = @TRUE EndIf If !ErrorDisplayed && !sSendLine (hdlSocket, Command2Perform) intErrNr = wxGetLastErr () strErrMsg = wxGetErrDesc (intErrNr) Message ("Connecter Error", "Cannot send service request." : @LF : intErrNr : @LF : strErrMsg) blnCleanup = @TRUE ErrorDisplayed = @TRUE 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) If !ErrorDisplayed Then Message ("Connecter Error", "Cannot close socket." : @LF : intErrNr : @LF : strErrMsg) blnCleanup = @TRUE ErrorDisplayed = @TRUE EndIf If !ErrorDisplayed Then Message (Command2Perform, strResponse) ; Disable this line for test loop. If Option_Flag == 6 || Option_Flag == 8 || Option_Flag == 9 Then Break 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
[About] prog = "About" [iTunes] prog = cmd /c start "iTunes" /MIN "%ProgramFiles(x86)%\iTunes\iTunes.exe" [AutoHotkey] prog = cmd /c start "AutoHotkey" /MIN "%ProgramFiles(x86)%\AutoHotkey\AutoHotkey.exe" [TouchControl] prog = cmd /c start "TouchControl" /MIN "D:\Program Files (x86)\Touch-IR\Touch-IRServer\Touch-IR.exe" [Media Center] prog = cmd /c start "Media Center" /MIN "%WinDir%\ehome\ehshell.exe" [MediaBrowser Service] prog = cmd /c start "MediaBrowserService" /MIN "D:\Program Files (x86)\MediaBrowser\MediaBrowser\MediaBrowserService.exe" [MediaBrowser Configurator] prog = cmd /c start "MediaBrowserService" /MIN "D:\Program Files (x86)\MediaBrowser\MediaBrowser\Configurator.exe" [Netflix] prog = Iexplore https://signup.netflix.com/Login [Hulu] prog = Iexplore http://www.hulu.com/
Article ID: W17627
Filename: WinSock Interprocess Communication Sample.txt
File Created: 2012:03:26:11:28:02
Last Updated: 2012:03:26:11:28:02