Search the Community
Showing results for tags 'onevent'.
Found 3 results
Hi guys/girls! I'm gonna share this UDF I made today. It allows you to easily create TCP servers and set actions depending on three events: OnConnect, OnDisconnect and OnReceive. It is also multi client (you can set the clients limit) and you can also bind a Console-based executable to the socket (similar to -e parameter in NetCat). This feature is useful if you want to use some Console UDF to create your TCP server and don't want to mix it with the TCP functions. Also, as it runs on background just firing events, it won't pause your script while listening/receiving, so you can do anything else (stop and restart the server, allow the user to click buttons or just wait on an infinite loop) that your callbacks will be called once the event is fired. It's also very easy to use. See this examples: Example #1: A basic server By running this (then connecting to the server using Netcat), you will receive a message box telling you when some user connects or disconnects (the socket ID and his IP address is passed as parameter to your callback function) and also when the user sends something over the TCP socket (the data sent is passed as parameter). #cs Download netcat at https://eternallybored.org/misc/netcat/ Execute this script Run in CMD: nc -vv 127.0.0.1 8081 #ce #include "TCPServer.au3" ; First we set the callback functions for the three events (none of them is mandatory) _TCPServer_OnConnect("connected") _TCPServer_OnDisconnect("disconnect") _TCPServer_OnReceive("received") ; And some parameters _TCPServer_DebugMode(True) _TCPServer_SetMaxClients(10) ; Finally we start the server at port 8081 at any interface _TCPServer_Start(8081) Func connected($iSocket, $sIP) MsgBox(0, "Client connected", "Client " & $sIP & " connected!") _TCPServer_Broadcast('new client connected guys', $iSocket) _TCPServer_Send($iSocket, "Hey! Write something ;)" & @CRLF) _TCPServer_SetParam($iSocket, "will write") EndFunc ;==>connected Func disconnect($iSocket, $sIP) MsgBox(0, "Client disconnected", "Client " & $sIP & " disconnected from socket " & $iSocket) EndFunc ;==>disconnect Func received($iSocket, $sIP, $sData, $sPar) MsgBox(0, "Data received from " & $sIP, $sData & @CRLF & "Parameter: " & $sPar) _TCPServer_Send($iSocket, "You wrote: " & $sData) _TCPServer_SetParam($iSocket, 'will write again') EndFunc ;==>received While 1 Sleep(100) WEnd Example #2: A basic HTTP server (just one page, as it is just an example) In this example, we run this code and point our browser to the address mentioned on the comments. A basic "It works!" page is show. #cs Run this script Point your browser to http://localhost:8081/ #ce #include "TCPServer.au3" _TCPServer_OnReceive("received") _TCPServer_DebugMode(True) _TCPServer_SetMaxClients(10) _TCPServer_Start(8081) Func received($iSocket, $sIP, $sData, $sParam) _TCPServer_Send($iSocket, "HTTP/1.0 200 OK" & @CRLF & _ "Content-Type: text/html" & @CRLF & @CRLF & _ "<h1>It works!</h1>" & @CRLF & _ "<p>This is the default web page for this server.</p>" & @CRLF & _ "<p>However this server is just a 26-lines example.</p>") _TCPServer_Close($iSocket) EndFunc ;==>received While 1 Sleep(100) WEnd Example #3: A telnet-like server (Command Prompt bound to the socket after password requesting) By running this example and connecting with Netcat, we will be asked for a password, which is 12345 as we set on the script. If the password is correct, we will see the Command Prompt live-updated (try running a ping to some server, for example). #cs Download netcat at https://eternallybored.org/misc/netcat/ Execute this script Run in CMD: nc -vv 127.0.0.1 8081 #ce #include "TCPServer.au3" Global $sPassword = "12345" ; input server password here _TCPServer_OnConnect("connected") _TCPServer_OnDisconnect("disconnect") _TCPServer_OnReceive("received") _TCPServer_DebugMode(True) _TCPServer_SetMaxClients(10) _TCPServer_Start(8081) Func connected($iSocket, $sIP) _TCPServer_Send($iSocket, "Welcome! Please input password: ") _TCPServer_SetParam($iSocket, 'login') EndFunc ;==>connected Func disconnect($iSocket, $sIP) MsgBox(0, "Client disconnected", "Client " & $sIP & " disconnected from socket " & $iSocket) EndFunc ;==>disconnect Func received($iSocket, $sIP, $sData, $sParam) If $sParam = "login" Then If $sData <> $sPassword Then _TCPServer_Send($iSocket, "Wrong password. Try again: ") Return Else _TCPServer_SetParam($iSocket, 'command') _TCPServer_BindAppToSocket($iSocket, 'cmd.exe') EndIf ElseIf $sParam = "command" Then _TCPServer_SendToBound($iSocket, $sData) EndIf EndFunc ;==>received While 1 Sleep(100) WEnd The limit is your imagination? Well, no sure. We have this limit: You can't create more than one server with this UDF in the same script. However, you can pause and resume (read 'stop and start again') your server at any time in your script, without having to reset the server settings. And of course you can have many clients (or just one, it's your choice!) in the same server. Or run multiple instances.Functions list: _TCPServer_Start _TCPServer_Stop _TCPServer_Close _TCPServer_Send _TCPServer_Broadcast _TCPServer_SetParam _TCPServer_BindAppToSocket _TCPServer_SendToBound _TCPServer_UnBindAppToSocket _TCPServer_GetMaxClients _TCPServer_IsServerActive _TCPServer_ListClients _TCPServer_OnConnect _TCPServer_OnDisconnect _TCPServer_OnReceive _TCPServer_SetMaxClients _TCPServer_DebugMode _TCPServer_AutoTrim _TCPServer_SocketToIP _TCPServer_SocketToConnID _TCPServer_ConnIDToSocket Help file and more examples included! Latest version: 18.104.22.168 Download: TCPServer UDF.rar Changelog 1.0 - First release - 18/04/2022.214.171.124 - Bug fix __TCPServer_Accept internal function / help file recompiled - 26/04/2015Perhaps you will need to uncompress the file first, so the help file will work. Fork this on Github: http://github.com/jesobreira/TCPServerUDF TCPServer UDF.rar
Hi, I have a quite complex script and now I would like to define the initial parameters and directories via a GUI. The first question is: Which GUI mode is best for my purposes? I don't understand in detail what the difference is. I tend to OnEvent mode as I only need the GUI at the beginning. Edit: Ok, only in loop mode the file selection works. One question solved. But how do I pause my script and start it after I am done with the GUI? This is my code so far: #include <MsgBoxConstants.au3> #include <WindowsConstants.au3> #include <File.au3> #include <FileConstants.au3> #include <StringConstants.au3> #include <Date.au3> #include <Array.au3> #include <GUIConstantsEx.au3> Local $AutoIt_GUI = GUICreate("ATCOR4 - Initial Settings", 500, 400) ; title, width and height Local $idLabel0 = GUICtrlCreateLabel("Wellcome to this ATCOR4 automation. Please choose your preferences.", 30, 10) ; text and position (left, top) Local $idButton1 = GUICtrlCreateButton("*.meta file", 30, 50, 60) Local $idLabel1 = GUICtrlCreateLabel("select the meta file with start and end time", 120, 55, 300, 60) Local $idButton2 = GUICtrlCreateButton("*.pos file", 30, 80, 60); name, position and width Local $idLabel2 = GUICtrlCreateLabel("select the pos file with flight geometry", 120, 85, 300, 60) Local $idCombo1 = GUICtrlCreateCombo("Operation Type", 30, 150, 100) GUICtrlSetData($idCombo1, "GUI|.inn file", "Operation Type") Global $OperationMode = '"' & GUICtrlRead($idCombo1) & '"' GUISetState(@SW_SHOW) ; display this GUI Local $idMsg = 0 ; In this message loop we use variables to keep track of changes to the GUI controls While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop Exit ;Case $GUI_EVENT_MINIMIZE ;MsgBox($MB_SYSTEMMODAL, "", "Dialog minimized", 2) ;Case $GUI_EVENT_MAXIMIZE ;MsgBox($MB_SYSTEMMODAL, "", "Dialog restored", 2) Case $idButton1 Local $MetaOpenDialog = FileOpenDialog("Select the meta file...", @ScriptDir & "\", "Meta (*.meta)") $metaPath = $MetaOpenDialog GUICtrlSetData($idLabel1, $MetaOpenDialog) Case $idButton2 Local $PosOpenDialog = FileOpenDialog("Select the pos file...", @ScriptDir & "\", "Pos (*.pos)") $posPath = $PosOpenDialog GUICtrlSetData($idLabel2, $PosOpenDialog) EndSwitch WEnd Thanks in advance for any hints and suggestions (the shorter the code the better).
Hey everyone, I searched some hours for a way to use the function WaitForSingleObject not as a function the script has to wait for, but a function which calls a callback function when the event is signaled. I found a way with RegisterWaitForSingleObject but no AutoIt implemention, so I made one on my own, and now I want to share it with you guys! The only limitation by this implemention is that you are not able to use more than one callback at once. to avoid this, you could write a static array in the function which fills with the selfmade callback structs. If you find another way, let me know! Example: Hope you like it and find use for it! Greetz, Spider RegisterWaitForSingleObject.zip