Socket.ahk - novice help -- ahkv1 side by side with ahkv2 passings strings

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
sashaatx
Posts: 351
Joined: 27 May 2021, 08:27
Contact:

Socket.ahk - novice help -- ahkv1 side by side with ahkv2 passings strings

Post by sashaatx » 21 Apr 2023, 10:00

I'm trying to upgrade my game and stop writing to a log file when unnecessary and use a socket.
I have an application I want to run ahkv1 side by side with ahkv2 and communicate through Socket.ahk found here:https://github.com/TheArkive/Socket_ahk2
I'm new to sockets but I have worked with them before, never passing and handling packets, always in some abstract form.

For this conversation I'll keep it to AHKv2 as Im getting stuck at just replicating the example server//client relationship where I want to send a string from the former to the latter.

Problem: Only getting IP address in return, no string

In the example I have taken test_server() and test_send() functions from : https://github.com/TheArkive/Socket_ahk2/blob/master/_socket_example.ahk
mentioned code:

Code: Select all

	test_server() {
		sock := winsock("server",cb,"IPV4")
	    sock.Bind("0.0.0.0",27015) ; "0.0.0.0",27015
	    sock.Listen()
	}

	test_send() {
	 	sock := winsock("client", cb, "IPV4")
	    If !(r := sock.Connect("127.0.0.1", 27015, true)) { ; connect as blocking, setting param #3 to TRUE
	        msgbox "Could not connect."
	        return ; handle a failed connect properly
	    }
	    
	    msg := "abc"
	    strbuf := Buffer(StrLen(msg) + 1) ; for UTF-8, take strLen() + 1 as the buffer size
	    StrPut(msg, strbuf, "UTF-8")
	    
	    r := sock.Send(strbuf) ; check send result if necessary
	    
	    sock.Close()

	}
and stuck them in socket.ahk for access from two separate ahk scripts:https://github.com/TheArkive/Socket_ahk2/blob/master/_socket.ahk

the first script runs test_server() listening for me to start test_send() back and (my basic goal) is to show a msgbox denoting it has worked, with the string.

Code: Select all

			buf := sock.Recv()
            msgbox("======================`r`n"
                    . "Server recieved from client:`r`n"
                    . Trim(strget(buf,"UTF-8"),"`r`n") "`r`n"
                    . "======================`r`n`r`n")

I can get it working as it is in the github, but it seems if I do anything to change the code Im getting wrong address errrors and "accept" instead of the string msg. but I dont change the code except for one place. I want to retreive that strget(buf) in a global variable. I cannot get this working. I usually only get an "accept" when sending to server, and no string return. so I reduce the above code to:
global return_string := Trim(strget(buf,"UTF-8")

and then I check for return_string values:

Code: Select all

	global return_string := ""

	Settimer(waiter, 250)

	waiter(){
		global return_string
		if return_string != "" {
			msgbox(returnstring)
                        return_string := ""
		}
	}

https://github.com/samfisherirl
? /Easy-Auto-GUI-for-AHK-v2 ? /Useful-AHK-v2-Libraries-and-Classes : /Pulovers-Macro-Creator-for-AHKv2 :

Return to “Ask for Help (v2)”