188 posts in this topic
TCPServer UDF (multi client, event-based, able to bind console app to socket)
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:
Help file and more examples included!
Latest version: 18.104.22.168
Download: TCPServer UDF.rar
1.0 - First release - 18/04/2015 22.214.171.124 - Bug fix __TCPServer_Accept internal function / help file recompiled - 26/04/2015 Perhaps you will need to uncompress the file first, so the help file will work.
Fork this on Github: http://github.com/jesobreira/TCPServerUDF
CUI console mouseevent
Can someone guide me to the right direction with this question?
I need a simple example for getting the mouse coordinates and other input events (using api calls, not mousegetpos) on a console window (return the x and y cell)
UDF link where I get the code from:
A c++ example that does exactly what i'm looking for:
The code in the attachments helps a lot but I don't quite understand it,
what code of it is necessary and is there a way more 'simple/less code' way to do this (like the c++ example)
Get monitor where mouse is on screen
I was searching for a while now and didn't find anything regarding this:
Is there a function that returns the "number" of the monitor where the mouse is currently on screen? The same number that appears when you open windows-resolution and click on "identify" (or something). I want to trigger an event when the number changes. I wrote something that recognizes leaving the main monitor. But I couldn't get any further than that (maybe with _WinAPI_EnumDisplayMonitors/Devices/Settings ?) and I wouldn't catch an event, if someone has 3 monitors (aligned 1--2--3 with 1 being main monitor) and changes from 2 to 3.
Here is my code:
The GUI was just for testing. Maybe you can give me some hints :)?
IE: Trigger event on _IEFormElementOptio
nSelect to display hidden div.
Hello, and thank you for your input. I've been searching around to try to find out how to trigger a change when selecting an option from a dropdown using _IEFormElementOptionSelect, but haven't found anything in the forums or via google.
Here is my sample code:
;Select the lead type $LeadType = _IEGetObjByName($oIE, "data[leads_types]") $LeadType.focus _IEFormElementOptionSelect($LeadType, "Expired", 1, "byText") This works to select the value, but it doesn't trigger a date range div that is set with display:none. When done manually there is an event trigger where jquery changes the display setting.
The HTML for the dropdown:
<select name="data[leads_types]" class="selectbox leadselectbox" style="width:140px;" id="lead_options"> <option value="">Lead Type</option> <optgroup label="Leads"> <option value="1">Divorce</option> <option value="27">Empty Nesters</option> <option value="24">Estate Sales</option> <option value="18">Evictions</option> <option value="2">Expired</option> </optgroup> <optgroup label="CRM"> <option value="11">Cold Lead</option> <option value="20">Dead</option> <option value="13">Follow Up Lead</option> <option value="12">Hot Lead</option> </optgroup> </select>The Html for the div that gets displayed:
<div id="date_selection" style="display: none;float: left;margin-left: 5px; width:390px; "> <div style="float: left;font-size:15px"> From: <input class="inputTxt3 dtpicker hasDatepicker" style="width:80px;" size="25" name="offer_from_date" id="offer_from_date" value="08/26/2015" type="text"> To: <input class="inputTxt3 dtpicker hasDatepicker" style="width:80px;" size="25" name="offer_to_date" id="offer_to_date" value="09/10/2015" type="text"> </div> <div style="display: block; float: left;margin-left: 5px;font-size:13px" id="searchTypeDiv"> <span title="Search for Recorded or Filed Dates">Record Date</span> <input type="radio" name="searchDateType" value="r"> <br> <span title="Search for the date RG found the record">Insert Date </span> <input type="radio" name="searchDateType" value="i" checked=""> </div> </div>I've tried changing the values when it isn't displayed, but that doesn't work. I need to find out how to trigger that event. Can someone point me in the right direction?
AUInfo Reading info under mouse on 2560x1440 screen wrong
I'm trying to run a script to fire when a pixel at a certain location on the screen changes to a particular colour, however auinfo is unable to measure the colour where my cursor is due to my high resolution - 2560x1440 on a 13.3" laptop. Does anyone have a workaround or a fix for this?
If I move my mouse to the bottom-right corner of the screen auinfo reports the mouse position as about 1280x720, 1/2 my actual resolution.