AHKSock Implementation (TCP for AHK)

Post your working scripts, libraries and tools
User avatar
goldeneyesrj
Posts: 37
Joined: 18 Jan 2016, 22:09
Location: Rio de Janeiro / Brazil

AHKSock Implementation (TCP for AHK)

05 Apr 2016, 13:24

My example for implementation of AHK_Sock.
EDIT: I make some modifications in TCPRECEIVE function to have more precision.
EDIT 2: I update the code (Server and Client) with more layer to check errors. Maybe you like a variable only for control. A fixed string to determine if the inbound is valid. For example a acess password for connect... Any sugestion?
EDIT 3: Maybe you prefer to use another separator for variables... For example: ¤ or ¥ (i use this). Change all | by another character.

See:
https://autohotkey.com/board/topic/5382 ... ock-tcpip/
Download AHKsock.ahk from top of first post (TheGood's) and paste in your \Documents\AutoHotkey\Lib folder.
PS.: the lib need AHK Unicode! Tested in Windows 10 Home SL, Home and Pro 1511 and 1607 x86 and x64.

Sorry for my bad english please.

Server:

Code: Select all

;SORRY MY BAD ENGLISH

#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%
#SingleInstance Force ; ONLY ONE SERVER INSTANCE

;GLOBAL VARS FOR PERSIST IN FUNCTIONS
global sClient, TEXT1, TEXT2, TEXT3, TEXT4, TEXT5, TEXT6, ParcialTCP

;ON EXIT CLOSE SOCKETS
OnExit, Exit

;REFRESH FOR SEND AND RECEIVE
SetTimer, RefreshSend, 5000
SetTimer, RefreshReceive, 100
gosub RefreshSend

;MY EXAMPLE GUI
GUI:
Gui, add, edit, w300 vTEXT1,
Gui, add, edit, w300 vTEXT2,
Gui, add, edit, w300 vTEXT3,
Gui, add, text, w300 vTEXT4,
Gui, add, text, w300 vTEXT5,
Gui, add, text, w300 vTEXT6,
Gui, add, text, w300 vsClient,
Gui, Show,, Server
return

;REFRESH THE RECEIVE AND GUI
RefreshReceive:
if sClient
	AHKsock_Connect(sClient, 27014, "TCPRECEIVE")
GuiControlGet, Text1,, Text1 ;GET TEXT IN EDITBOX 1
GuiControlGet, Text2,, Text2 ;GET TEXT IN EDITBOX 2
GuiControlGet, Text3,, Text3 ;GET TEXT IN EDITBOX 3
GuiControl,, Text4, %Text4% ;WRITE TEXT IN TEXT 1
GuiControl,, Text5, %Text5% ;WRITE TEXT IN TEXT 2
GuiControl,, Text6, %Text6% ;WRITE TEXT IN TEXT 3
GuiControl,, sClient, %sClient% ;WRITE CLIENT IP IN TEXT 4 (RETRIVED IN ACCEPTED EVENT)
return

;REFRESH SEND (NOT NECESSARY TO RUN AWAYS (ONLY ONE), BUT DISCONECT AND RECONECT SOCKETS)
RefreshSend:
AHKsock_Close() ;CLOSE ALL CONECTIONS TWICE (REDUNDANCE)
AHKsock_Close()
ParcialTCP= ;NULL RECEIVER PARTIAL VAR
AHKsock_Listen(27016, "TCPSEND") ;START LISTEN...
return

;FUNCTION CALLED BY AHK_SOCK FOR SEND DATA
;SEND VARS SEPARATED BY | AND ALL VARS TERMINATOR IS `n
TCPSEND(sEvent, iSocket = 0, sName = 0, sAddr = 0, sPort = 0)
{
	if (sEvent = "ACCEPTED") ;sEvent ACCEPTED
		sClient= %sAddr% ;GETTING IPADRESS OF CLIENT TO RefreshReceive
	If (sEvent = "SEND") ;sEvent SEND START TRANSMISSION OF VARS
	{
		SendText= %TEXT1%|%TEXT2%|%TEXT3%|`n ;SEPARATED BY | AND TERMINATED BY `n
		iLength:= StrLen(SendText) * 2 ;THE LENGHT OF STRING * 2
		AHKsock_Send(iSocket, &SendText, iLength) ;SEND
		AHKsock_Close(iSocket) ;CLOSE SOCKET
	}
	return
}

;FUNCTION CALLED BY AHK_SOCK FOR RECEIVE DATA
;PARSE bData TWO TIMES TO DETECT | (DELIMIT VARS) AND `n (MARK END OF ALL VARS)
TCPRECEIVE(sEvent, iSocket = 0, sName = 0, sAddr = 0, sPort = 0, ByRef bData = 0, iLength = 0)
{
	If (sEvent = "RECEIVED") ;sEvent RECEIVED: THE VARS ARE RECEIVED
	{
		ParcialTCP= %ParcialTCP%%bData% ;ADD bData AT END OF PARTIAL VAR
		Loop, Parse, ParcialTCP,| ;PARSE ParcialTCP TO SEARCH FOR TERMINATOS `n
		{
			if A_Index= 4 ;IF NUMBER OF VARS + 1 (`n POSITION)
			{
				if (A_LoopField = "`n") ;FOUND `n AFTER LAST VAR (REMEMBER HAVE 3 VARS)
				{
					Loop, Parse, ParcialTCP,| ;PARSE PARTIAL VAR TO SEPARATE RECEIVED VARS (TEXT4, TEXT5, TEXT6)
					{
						if A_Index= 1
							TEXT4= %A_LoopField%
						if A_Index= 2
							TEXT5= %A_LoopField%
						if A_Index= 3
							TEXT6= %A_LoopField%
					}
				}
			}
		}
		Loop, Parse, bData,`n
		{
			if A_Index= 2 ;IF HAVE ANY AFTER TERMINATOR `n, ADD TO PARTIAL VAR (AND RESET ParcialTCP)
				ParcialTCP= %A_LoopField%
		}
	}
	return
}

;EXIT CLOSE ALL SOCKETS
GuiClose:
Exit:
AHKsock_Close() ;REDUNDANT
AHKsock_Close()
ExitApp
Client: (the only diference is manual input of server address)

Code: Select all

;SORRY MY BAD ENGLISH

#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%
#SingleInstance Off ;MORE THAN ONE CLIENT INSTANCE

;GLOBAL VARS FOR PERSIST IN FUNCTIONS
global sServer, TEXT1, TEXT2, TEXT3, TEXT4, TEXT5, TEXT6, ParcialTCP

;ON EXIT CLOSE SOCKETS
OnExit, Exit

;INPUT FOR SERVER IP
InputBox, sServer, SERVER IP,,,,,,,,, %A_IPAddress1%

;REFRESH FOR SEND AND RECEIVE
SetTimer, RefreshSend, 5000
SetTimer, RefreshReceive, 100
gosub RefreshSend

;MY EXAMPLE GUI
GUI:
Gui, add, edit, w300 vTEXT1,
Gui, add, edit, w300 vTEXT2,
Gui, add, edit, w300 vTEXT3,
Gui, add, text, w300 vTEXT4,
Gui, add, text, w300 vTEXT5,
Gui, add, text, w300 vTEXT6,
Gui, add, text, w300 vsServer,
Gui, Show,, Client
return

;REFRESH THE RECEIVE AND GUI
RefreshReceive:
if sServer
	AHKsock_Connect(sServer, 27016, "TCPRECEIVE") ;CONNECT...
GuiControlGet, Text1,, Text1 ;GET TEXT IN EDITBOX 1
GuiControlGet, Text2,, Text2 ;GET TEXT IN EDITBOX 2
GuiControlGet, Text3,, Text3 ;GET TEXT IN EDITBOX 3
GuiControl,, Text4, %Text4% ;WRITE TEXT IN TEXT 1
GuiControl,, Text5, %Text5% ;WRITE TEXT IN TEXT 2
GuiControl,, Text6, %Text6% ;WRITE TEXT IN TEXT 3
GuiControl,, sServer, %sServer% ;WRITE SERVER IP IN TEXT 4
return

;REFRESH SEND (NOT NECESSARY TO RUN AWAYS (ONLY ONE), BUT DISCONECT AND RECONECT SOCKETS)
RefreshSend:
AHKsock_Close() ;CLOSE ALL CONECTIONS TWICE (REDUNDANCE)
AHKsock_Close()
ParcialTCP= ;NULL RECEIVER PARTIAL VAR
AHKsock_Listen(27014, "TCPSEND") ;START LISTEN...
return

;FUNCTION CALLED BY AHK_SOCK FOR SEND DATA
;SEND VARS SEPARATED BY | AND ALL VARS TERMINATOR IS `n
TCPSEND(sEvent, iSocket = 0, sName = 0, sAddr = 0, sPort = 0)
{
If (sEvent = "SEND") ;sEvent SEND START TRANSMISSION OF VARS
{
SendText= %TEXT1%|%TEXT2%|%TEXT3%|`n ;SEPARATED BY | AND TERMINATED BY `n
iLength:= StrLen(SendText) * 2 ;THE LENGHT OF STRING * 2
AHKsock_Send(iSocket, &SendText, iLength) ;SEND
AHKsock_Close(iSocket) ;CLOSE SOCKET
}
return
}

;FUNCTION CALLED BY AHK_SOCK FOR RECEIVE DATA
;PARSE bData TWO TIMES TO DETECT | (DELIMIT VARS) AND `n (MARK END OF ALL VARS)
TCPRECEIVE(sEvent, iSocket = 0, sName = 0, sAddr = 0, sPort = 0, ByRef bData = 0, iLength = 0)
{
	If (sEvent = "RECEIVED") ;sEvent RECEIVED: THE VARS ARE RECEIVED
	{
		ParcialTCP= %ParcialTCP%%bData% ;ADD bData AT END OF PARTIAL VAR
		Loop, Parse, ParcialTCP,| ;PARSE ParcialTCP TO SEARCH FOR TERMINATOS `n
		{
			if A_Index= 4 ;IF NUMBER OF VARS + 1 (`n POSITION)
			{
				if (A_LoopField = "`n") ;FOUND `n AFTER LAST VAR (REMEMBER HAVE 3 VARS)
				{
					Loop, Parse, ParcialTCP,| ;PARSE PARTIAL VAR TO SEPARATE RECEIVED VARS (TEXT4, TEXT5, TEXT6)
					{
						if A_Index= 1
							TEXT4= %A_LoopField%
						if A_Index= 2
							TEXT5= %A_LoopField%
						if A_Index= 3
							TEXT6= %A_LoopField%
					}
				}
			}
		}
		Loop, Parse, bData,`n
		{
			if A_Index= 2 ;IF HAVE ANY AFTER TERMINATOR `n, ADD TO PARTIAL VAR (AND RESET ParcialTCP)
				ParcialTCP= %A_LoopField%
		}
	}
    return
}

;EXIT CLOSE ALL SOCKETS
GuiClose:
Exit:
AHKsock_Close() ;REDUNDANT
AHKsock_Close()
ExitApp
Last edited by goldeneyesrj on 06 Jan 2017, 02:42, edited 2 times in total.
User avatar
goldeneyesrj
Posts: 37
Joined: 18 Jan 2016, 22:09
Location: Rio de Janeiro / Brazil

Re: AHKSock Implementation (TCP for AHK)

31 Dec 2016, 15:31

I change the TCP Receive function.
This is the version i use in my scripts.

Atention in this line (server and Client):

Code: Select all

if A_Index= 4 ;IF NUMBER OF VARS + 1 (`n POSITION)
You have to put a number of vars transmited for other part plus one!
In the example, 3 vars + 1 is 4

Code: Select all

if A_Index= 1
	TEXT4= %A_LoopField%
if A_Index= 2
	TEXT5= %A_LoopField%
if A_Index= 3
	TEXT6= %A_LoopField%
Sorry my bad english.
DanielToward13
Posts: 71
Joined: 18 May 2017, 10:56

Re: AHKSock Implementation (TCP for AHK)

30 Jul 2017, 17:28

Does it work over the internet?
is there a way to create a P2P connection without changing the firewall and routing settings?
User avatar
goldeneyesrj
Posts: 37
Joined: 18 Jan 2016, 22:09
Location: Rio de Janeiro / Brazil

Re: AHKSock Implementation (TCP for AHK)

23 Sep 2017, 09:55

DanielToward13 wrote:Does it work over the internet?
is there a way to create a P2P connection without changing the firewall and routing settings?
I never tested over internet, but maybe you need to create a desmilitarized zone in your router to expose your computer to internet, same way dvrs security cameras. First test in diferents subnets... Sorry my bad english.

Return to “Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 23 guests