Socket.ahk

Post your working scripts, libraries and tools for AHK v1.1 and older
geek
Posts: 1052
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

Re: Socket.ahk

20 Jun 2020, 17:19

What Android app are you trying to connect to, and what firewall settings do you have on your PC?
PublicBenemy
Posts: 6
Joined: 20 Jun 2020, 16:13
Contact:

Re: Socket.ahk

21 Jun 2020, 04:46

Thanks for your reply, I didn't expect the master himself to be on duty :-D

The app I use for testing is Simple Socket Tester. I also tried to connect to other devices, which I can just telnet in from my desktop computer and see the messages they're sending.

For the firewall, I allowed Autohotkey everything (I think), please see the screenhost attached. I also tried running the script as administrator many times.
Attachments
Anmerkung 2020-06-21 113723.jpg
Anmerkung 2020-06-21 113723.jpg (35.72 KiB) Viewed 7027 times
Last edited by gregster on 21 Jun 2020, 09:04, edited 2 times in total.
Reason: Link fixed.
PublicBenemy
Posts: 6
Joined: 20 Jun 2020, 16:13
Contact:

Re: Socket.ahk

21 Jun 2020, 06:02

When I add the line

Code: Select all

MsgBox, % Verbindung.RecvText()
from thise example
https://autohotkey.com/board/topic/94376-socket-class-%C3%BCberarbeitet/?p=603390

I receive this error message (see attached)

Is "10038" an error code according to this?
https://docs.microsoft.com/en-us/windows/win32/winsock/windows-sockets-error-codes-2

Just also tried

Code: Select all

Verbindung.Connect[("mail.gmx.net", 25)]
instead, same error.
Attachments
Anmerkung 2020-06-21 130202.jpg
Anmerkung 2020-06-21 130202.jpg (44.01 KiB) Viewed 6781 times
Last edited by PublicBenemy on 21 Jun 2020, 08:36, edited 1 time in total.
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Socket.ahk

21 Jun 2020, 08:32

PublicBenemy wrote:
21 Jun 2020, 04:46
... I cannot add a Link to Google Play Store here, as this forum breaks any link "for safety".
Sollten Forum-Moderatoren solche Links finden, fixen wir diese (sofern nicht zu beanstanden). Also einfach den besagten Post (nachträglich) mit Link versehen.
PublicBenemy
Posts: 6
Joined: 20 Jun 2020, 16:13
Contact:

Re: Socket.ahk

21 Jun 2020, 08:50

BoBo wrote:
21 Jun 2020, 08:32
PublicBenemy wrote:
21 Jun 2020, 04:46
... I cannot add a Link to Google Play Store here, as this forum breaks any link "for safety".
Sollten Forum-Moderatoren solche Links finden, fixen wir diese (sofern nicht zu beanstanden). Also einfach den besagten Post (nachträglich) mit Link versehen.
Ah cool, danke, hab ich gemacht!
geek
Posts: 1052
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

Re: Socket.ahk

21 Jun 2020, 09:44

I think the problem may be partially because you have the[] and () reversed in your code.

Code: Select all

Verbindung.Connect[("mail.gmx.net", 25)]
should be

Code: Select all

Verbindung.Connect(["mail.gmx.net", 25])
Please let me know if switching those around resolves your issue or if there are still problems
PublicBenemy
Posts: 6
Joined: 20 Jun 2020, 16:13
Contact:

Re: Socket.ahk

21 Jun 2020, 14:26

GeekDude wrote:
21 Jun 2020, 09:44
I think the problem may be partially because you have the[] and () reversed in your code.

Code: Select all

Verbindung.Connect[("mail.gmx.net", 25)]
should be

Code: Select all

Verbindung.Connect(["mail.gmx.net", 25])
Please let me know if switching those around resolves your issue or if there are still problems
Erm. OMG. I sacrified my afternoon to my inability to put brackets in the right order.
I'm dying now for many reasons.
Thank you so so much.

It works.
geek
Posts: 1052
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

Re: Socket.ahk

21 Jun 2020, 18:27

Happens to everyone on occasion :) Glad you got it working!
Chiefkes
Posts: 30
Joined: 04 May 2020, 20:01

Re: Socket.ahk

20 Jul 2020, 11:59

Edit: Removed post as I had sockets and websockets mixed up
Last edited by Chiefkes on 23 Jul 2020, 06:12, edited 2 times in total.
Chiefkes
Posts: 30
Joined: 04 May 2020, 20:01

Re: Socket.ahk

20 Jul 2020, 17:11

Edit: Removed post as I had sockets and websockets mixed up
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Socket.ahk

10 Oct 2020, 07:20

@GeekDude - Hi Philip (that's, by the way, the name of my Grandpa *sigh*) :)
People/users at matricapp.com start using its API more often, so I've asked myself if you would mind providing/posting your matric.ahk script at this forum section (or on GitHub, where I haven't found it too). Well, I've lost the link where I've once downloaded it. So it's hard to refer to it if requested. That'll make even more sense as the developer(s) of Matric have confirmed to set up an AHK-channel at its Discord once people are keen to do trigger matric decks using AHK. Thx for listening. Rgds, Bo 8-)

PS. If it's more convenient for you, I could simply attach matric.ahk to this thread: https://www.autohotkey.com/boards/viewtopic.php?f=22&t=80794 ?
burque505
Posts: 1732
Joined: 22 Jan 2017, 19:37

Re: Socket.ahk

10 Oct 2020, 08:38

@BoBo, have a look at this MATRIC.ahk gist. Is that what you're looking for? Found it in a reddit link.
Regards,
burque505
Last edited by BoBo on 10 Oct 2020, 10:18, edited 1 time in total.
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Socket.ahk

10 Oct 2020, 10:07

burque505 wrote:
10 Oct 2020, 08:38
@BoBo, have a look at this MATRIC.ahk gist (link temporarily disabled, see below). Is that what you're looking for? Found it in a reddit link.
Regards,
burque505
That's it! Thx a lot. You've made my day!! :clap: :thumbup:
* Now we have to get GeekDude's confirmation (well, at least I'd like to get it) bc the linked page is tagged as "Secret" and therefore sh-/couldn't be found using GitHub's search.
@burque505 - I've been at that Reddit page before, but haven't seen a link?! Is it possible that links are only displayed if you're logged in (which I wasn't)?!
burque505
Posts: 1732
Joined: 22 Jan 2017, 19:37

Re: Socket.ahk

10 Oct 2020, 10:21

@BoBo, I can't see that link (or even the particular post it's in) if I log out and refresh, so you must be right.
Regards,
burque505
User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: Socket.ahk

17 Oct 2020, 10:34

Thanks GeekDude, I am VERY interested in this. Tried the old examples a while ago and they seemed semi-clunky. I will definitely give this a spin.
AmigaX86
Posts: 1
Joined: 18 Apr 2021, 08:29

Re: Socket.ahk

18 Apr 2021, 08:59

rIsidoro wrote:
05 Feb 2019, 02:42
Hi all,
I recently began AHK programming. For a small script I made, I used this library so I want to say Thank You to the developer(s).
Also, I would like to contribute a little with a couple of methods I added to the SocketUDP subclass, recvFrom and sendTo, to be used with connectionless protocols like UDP.
They are closely related to recv and send methods. You can add them to the SocketUDP subclass (suggested) or to the main Socket class.
I hope someone may find them useful.

Code: Select all

	RecvFrom(ByRef Buffer, BufSize:=0, Flags:=0, ByRef AddrFrom := 0)
	{
		while (!(Length := this.MsgSize()) && this.Blocking)
			Sleep, this.BlockSleep
		if !Length
			return 0
		if !BufSize
			BufSize := Length
		VarSetCapacity(Buffer, BufSize)
		szAddrFrom := VarSetCapacity(AddrFrom, 16, 0)
		if ((r := DllCall("Ws2_32\recvfrom", "UInt", this.Socket
								, "Ptr", &Buffer, "Int", BufSize
								, "Int", Flags
								, "Ptr", &AddrFrom, "Ptr*", szAddrFrom )) == -1)
			throw Exception("Error calling RecvFrom",, this.GetLastError())
		return r
	}
	
	SendTo(pBuffer, BufSize, Flags:=0, ByRef ToAddr := 0)
	{
		if ((r := DllCall("Ws2_32\sendto", "UInt", this.Socket
						, "Ptr", pBuffer, "Int", BufSize
						, "Int", Flags
						, "Ptr", &ToAddr
						, "Int", 16)) == -1)
			throw Exception("Error calling SendTo",, this.GetLastError())
		return r
	}
Example of use:

Code: Select all

; e.g. in a UDP server, inside a Recv callback function:

VarSetCapacity(pktIN, DGRAMSIZE, 0)
Sock.RecvFrom(pktIN, DGRAMSIZE, 0, addrFrom)
IPfrom := DllCall( "Ws2_32.dll\inet_ntoa","UInt",NumGet(addrFrom,4,"UInt"), "AStr" ) ; IPFrom will contain the IP of the client in string format

; then, after preparing the answer in pktOUT, send it back to the client
Sock.SendTo(&pktOUT, DGRAMSIZE, 0, addrFrom)
Again Thank You G33kDude and Bentschi for the great library.
Bye



Thank you rIsidoro, great, crucial functions for UDP. Thank you again, was surprised that there was no reaction to your post.
xinxilas
Posts: 30
Joined: 18 Nov 2015, 16:54

Re: Socket.ahk

30 Jul 2021, 13:19

Server := new SocketTCP()
Server.OnAccept := Func("OpenFolder")
Server.Bind(["127.0.0.1", 8000])
Server.Listen()

OpenFolder(Server)
{
Sock := Server.Accept()
Request := StrSplit(Sock.RecvText(), " ")
Sock.SendText("HTTP/1.0 501 OK Content-Type: text/plainrnrnNot Implemented")
Sock.Disconnect()
}

F1::
MsgBox, "OK"
return


Sock isnt disconnected correctly if i use http://localhost to call my tcpsocket
It stays looped here:

Recv(ByRef Buffer, BufSize:=0, Flags:=0)
{
while (!(Length := this.MsgSize()) && this.Blocking) ;---------HERE
Sleep, this.BlockSleep ;---------HERE
if !Length
return 0
if !BufSize
BufSize := Length
VarSetCapacity(Buffer, BufSize)
if ((r := DllCall("Ws2_32\recv", "UInt", this.Socket, "Ptr", &Buffer, "Int", BufSize, "Int", Flags)) == -1)
throw Exception("Error calling recv",, this.GetLastError())
return r
}



Then i cant use F1 hotkey
But i can call http://localhost again

If i use http://127.0.0.1
It works fine,
william_ahk
Posts: 486
Joined: 03 Dec 2018, 20:02

Re: Socket.ahk

28 Oct 2021, 05:02

Noob question, what does blocking do? The server was sometimes unresponsive after some requests and setting Blocking to false solved the issue.

Code: Select all

static Blocking := true, BlockSleep := 50
magusneo
Posts: 45
Joined: 30 Sep 2013, 06:34

Re: Socket.ahk

06 Dec 2021, 19:40

How can I get "Basic HTTP server" example working?
tried codes like

Code: Select all

Server := new SocketTCP()
Server.OnAccept := Func("OnAccept")
Server.Bind(["127.0.0.1", 1337])
Server.Listen()

MsgBox, Serving on port 1337`nClose to ExitApp
ExitApp
But browser(Internet Explorer) can't open "http://127.0.0.1:1337"

edited:It's be solved on another windows7 system instead of my old windowsXP.
User avatar
ibieel
Posts: 216
Joined: 17 Oct 2021, 23:30

Re: Socket.ahk

06 Jul 2022, 23:26

I managed to send a text as follows:
1- I opened port 8000 on my router
2- I opened port 8000 on my Windows10 computer's firewall (inbound and outbound)

however I had a problem, after a few seconds sending texts via socket the connection error script.
when i restart the script it works for a few more seconds and the connection error again.
I couldn't diagnose the problem, the script is working as follows:

Server Script:

Code: Select all

#NoEnv
SetBatchLines, -1

#Include ..\Socket.ahk

global DC_SERV := True
global DC_CLI := False

Server := new SocketTCP()
Server.OnAccept := Func("OnAccept")
Server.Bind(["0.0.0.0", 8000])
Server.Listen()

OnAccept(Server) {
GLOBAL
	Sock := Server.Accept()
	ToolTip, % Sock.RecvText()
	if (DC_SERV)
		Sock.Disconnect()
}

Client Script:

Code: Select all

#NoEnv
SetBatchLines, -1

#Include ..\Socket.ahk

global DC_SERV := True
global DC_CLI := False


Loop
{
	x := new SocketTCP()
	x.Connect(["187.46.742.551", 8000])	;187.46.742.551 = my global IP
	x.SendText(A_Index)
	Sleep, 10
	if (DC_CLI)
		x.Disconnect()
}
return

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 73 guests