How to send a text message via UDP with socket.ahk form github

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
witor
Posts: 4
Joined: 22 Mar 2018, 14:01

How to send a text message via UDP with socket.ahk form github

22 Mar 2018, 16:53

Hello

I need a script that would send this text:

@\r\nparam=MC_UP\r\nplane=Antares18S\r\n

to a given IP address and port, let's say 127.0.0.1 Port=55278

I came across Ws2_32 and I have looked at Socket.ahk on git hub, but I don't know how to use it. As you can tell by now I am not an experienced AHK user. How would i use Socket.ahk to send my message or just "hello world"to 127.0.0.1 port 55278 ?

Is it doable? Is it difficult? If not could someone write a little script, help me out or point me in the right direction?
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: How to send a text message via UDP with socket.ahk form github

23 Mar 2018, 01:02

So your supporters have to search for Socket.ahk on github bc you were to shy to post a link to it? :roll:
https://github.com/G33kDude/Socket.ahk/ ... Socket.ahk
witor
Posts: 4
Joined: 22 Mar 2018, 14:01

Re: How to send a text message via UDP with socket.ahk form github

23 Mar 2018, 04:42

Sorry and thank you for posting the link
Bukan
Posts: 2
Joined: 02 Apr 2018, 05:43

Re: How to send a text message via UDP with socket.ahk form github

02 Apr 2018, 20:51

I was looking for the same thing. Found many related stuff, but nothing easy to understand. Took me a while, but i think i got it. It's my very first experiment with coding, so this is for the most part compilation from other scripts and examples. It is not doing something useful, just send and receive messages to TCP server in another software for now. Trying to use my Saitek Multi Panel without paying $80 for SPAD.Next :roll:

Code: Select all

	#SingleInstance Force
	#Include Socket.ahk
    OnExit, GuiClose
	isConnected := False ;start condition
    
    ;Set up the GUI
    Gui, +Resize +OwnDialogs +AlwaysOnTop +LastFound
    Gui, Add, Edit, r10 w300 vtxtDialog ReadOnly hwndhtxtDialog
    Gui, Add, Edit, xm w250 vtxtInput ReadOnly hwndhtxtInput Limit65535
    Gui, Add, Button, x+5 w45 hp vbtnSend Disabled hwndhbtnSend, Send
	Gui, Add, Text, xm  vIPlbl hwndhIPlbl, IP:
	Gui, Add, Edit, x+1 w90 vIPadress hwndhIPadress, 127.0.0.1
	Gui, Add, Text, x+10  vPortlbl hwndhPortlbl , Port:
	Gui, Add, Edit, x+1 w90 vport hwndhPort, 32167
	Gui, Add, Button, x+8 w65 h21 vbtnConnect hwndhbtnConnect, Connect
	Gui, Add, Text, xm w240 vlblStatus hwndhlblStatus, Disconnected
    Gui, +MinSize
    Gui, Show
	
GuiSize:
    Anchor(htxtDialog, "wh")
    Anchor(htxtInput, "wy")
    Anchor(hbtnSend, "xy")
	Anchor(hbtnConnect, "xy")
    Anchor(hlblStatus, "wy", 1)
	Anchor(hPortlbl, "xy")
	Anchor(hIPadress, "xy")
	Anchor(hPort, "xy")
	Anchor(hIPlbl, "xy")
Return

;Show GUI
Gui, Show
Return

GuiClose:
ExitApp

;this function disable/enable part of Gui depends on connection status.
ToggleGui(isConnected){
	IF (isConnected){
	GuiControl, -ReadOnly, IPadress
	GuiControl, +ReadOnly, txtInput
	GuiControl, -ReadOnly, port
	GuiControl, Disable, btnSend
	GuiControl,, btnConnect, Connect
	} else {
	GuiControl, +ReadOnly, IPadress
	GuiControl, Enable, btnSend
	GuiControl, +ReadOnly, port
	GuiControl, -ReadOnly, txtInput
	GuiControl,, btnConnect, Disconnect
	}
}

;this is actually Connect/Disconnect button
ButtonConnect:

	GuiControlGet, sIP,, IPadress
	GuiControlGet, sPort,, port
	GuiControlGet, sBtn,, btnConnect
	
	IF (isConnected){
		Client.Disconnect() ;disconnecting from the server
		ToggleGui(isConnected)
		GuiControl,, lblStatus, Disconnected
		isConnected := False	
	} else {
		Client := new SocketTCP()	;creating instance of Socket.ahk class
		Client.Connect([sIP, sPort]) ; connecting to server
		Client.onRecv := Func("OnRecieve") ;this was least obvious part for me. There is no implementation of this method in Socket.ahk, so you need to create it by yourself
	
	
	ToggleGui(isConnected)
	GuiControl,, lblStatus, Connected to %sIP% : %sPort%
	isConnected := True
	}

Return



ButtonSend:
    ;Get the text to send
    GuiControlGet, sText,, txtInput
	
	;Sending text
	Client.SendText(sText)
	
	;Data was sent. Add it to the dialog.
    AddDialog(&sText)
    
    ;Clear the Edit control and give focus
    GuiControl,, txtInput
    GuiControl, Focus, txtInput
Return

OnRecieve(){
	global Client ;calling for our instanse of Socket.ahk
	inText := Client.RecvText() ;recieving actual message
	ackn1 := "Recieved" ;just for debugging
	AddDialog(&ackn1, bYou = False) ;just for debugging
	AddDialog(&inText, bYou = False) ; show inc message in Gui
return
}

AddDialog(ptrText, bYou = True) {
    Global htxtDialog
    
    ;Append the interlocutor
    sAppend := bYou ? "You > " : "Server > "
    InsertText(htxtDialog, &sAppend)
    
    ;Append the new text
    InsertText(htxtDialog, ptrText)
    
    ;Append a new line
    sAppend := "`r`n"
    InsertText(htxtDialog, &sAppend)
    
    ;Scroll to bottom
    SendMessage, 0x0115, 7, 0,, ahk_id %htxtDialog% ;WM_VSCROLL
}



/*! TheGood
    Append text to an Edit control
    http://www.autohotkey.com/forum/viewtopic.php?t=56717
*/
InsertText(hEdit, ptrText, iPos = -1) {
    
    If (iPos = -1) {
        SendMessage, 0x000E, 0, 0,, ahk_id %hEdit% ;WM_GETTEXTLENGTH
        iPos := ErrorLevel
    }
    
    SendMessage, 0x00B1, iPos, iPos,, ahk_id %hEdit% ;EM_SETSEL
    SendMessage, 0x00C2, False, ptrText,, ahk_id %hEdit% ;EM_REPLACESEL
}

;Anchor by Titan, adapted by TheGood
;http://www.autohotkey.com/forum/viewtopic.php?p=377395#377395
Anchor(i, a = "", r = false) {
	static c, cs = 12, cx = 255, cl = 0, g, gs = 8, gl = 0, gpi, gw, gh, z = 0, k = 0xffff, ptr
	If z = 0
		VarSetCapacity(g, gs * 99, 0), VarSetCapacity(c, cs * cx, 0), ptr := A_PtrSize ? "Ptr" : "UInt", z := true
	If (!WinExist("ahk_id" . i)) {
		GuiControlGet, t, Hwnd, %i%
		If ErrorLevel = 0
			i := t
		Else ControlGet, i, Hwnd, , %i%
	}
	VarSetCapacity(gi, 68, 0), DllCall("GetWindowInfo", "UInt", gp := DllCall("GetParent", "UInt", i), ptr, &gi)
		, giw := NumGet(gi, 28, "Int") - NumGet(gi, 20, "Int"), gih := NumGet(gi, 32, "Int") - NumGet(gi, 24, "Int")
	If (gp != gpi) {
		gpi := gp
		Loop, %gl%
			If (NumGet(g, cb := gs * (A_Index - 1)) == gp, "UInt") {
				gw := NumGet(g, cb + 4, "Short"), gh := NumGet(g, cb + 6, "Short"), gf := 1
				Break
			}
		If (!gf)
			NumPut(gp, g, gl, "UInt"), NumPut(gw := giw, g, gl + 4, "Short"), NumPut(gh := gih, g, gl + 6, "Short"), gl += gs
	}
	ControlGetPos, dx, dy, dw, dh, , ahk_id %i%
	Loop, %cl%
		If (NumGet(c, cb := cs * (A_Index - 1), "UInt") == i) {
			If a =
			{
				cf = 1
				Break
			}
			giw -= gw, gih -= gh, as := 1, dx := NumGet(c, cb + 4, "Short"), dy := NumGet(c, cb + 6, "Short")
				, cw := dw, dw := NumGet(c, cb + 8, "Short"), ch := dh, dh := NumGet(c, cb + 10, "Short")
			Loop, Parse, a, xywh
				If A_Index > 1
					av := SubStr(a, as, 1), as += 1 + StrLen(A_LoopField)
						, d%av% += (InStr("yh", av) ? gih : giw) * (A_LoopField + 0 ? A_LoopField : 1)
			DllCall("SetWindowPos", "UInt", i, "UInt", 0, "Int", dx, "Int", dy
				, "Int", InStr(a, "w") ? dw : cw, "Int", InStr(a, "h") ? dh : ch, "Int", 4)
			If r != 0
				DllCall("RedrawWindow", "UInt", i, "UInt", 0, "UInt", 0, "UInt", 0x0101) ; RDW_UPDATENOW | RDW_INVALIDATE
			Return
		}
	If cf != 1
		cb := cl, cl += cs
	bx := NumGet(gi, 48, "UInt"), by := NumGet(gi, 16, "Int") - NumGet(gi, 8, "Int") - gih - NumGet(gi, 52, "UInt")
	If cf = 1
		dw -= giw - gw, dh -= gih - gh
	NumPut(i, c, cb, "UInt"), NumPut(dx - bx, c, cb + 4, "Short"), NumPut(dy - by, c, cb + 6, "Short")
		, NumPut(dw, c, cb + 8, "Short"), NumPut(dh, c, cb + 10, "Short")
	Return, true
}
P.S. Sorry about my English.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Joey5 and 226 guests