Page 2 of 3

Re: Socket.ahk

Posted: 20 Jun 2020, 17:19
by geek
What Android app are you trying to connect to, and what firewall settings do you have on your PC?

Re: Socket.ahk

Posted: 21 Jun 2020, 04:46
by PublicBenemy
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.

Re: Socket.ahk

Posted: 21 Jun 2020, 06:02
by PublicBenemy
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.

Re: Socket.ahk

Posted: 21 Jun 2020, 08:32
by BoBo
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.

Re: Socket.ahk

Posted: 21 Jun 2020, 08:50
by PublicBenemy
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!

Re: Socket.ahk

Posted: 21 Jun 2020, 09:44
by geek
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

Re: Socket.ahk

Posted: 21 Jun 2020, 14:26
by PublicBenemy
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.

Re: Socket.ahk

Posted: 21 Jun 2020, 18:27
by geek
Happens to everyone on occasion :) Glad you got it working!

Re: Socket.ahk

Posted: 20 Jul 2020, 11:59
by Chiefkes
Edit: Removed post as I had sockets and websockets mixed up

Re: Socket.ahk

Posted: 20 Jul 2020, 17:11
by Chiefkes
Edit: Removed post as I had sockets and websockets mixed up

Re: Socket.ahk

Posted: 10 Oct 2020, 07:20
by BoBo
@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 ?

Re: Socket.ahk

Posted: 10 Oct 2020, 08:38
by burque505
@BoBo, have a look at this MATRIC.ahk gist. Is that what you're looking for? Found it in a reddit link.
Regards,
burque505

Re: Socket.ahk

Posted: 10 Oct 2020, 10:07
by BoBo
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)?!

Re: Socket.ahk

Posted: 10 Oct 2020, 10:21
by burque505
@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

Re: Socket.ahk

Posted: 17 Oct 2020, 10:34
by TheArkive
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.

Re: Socket.ahk

Posted: 18 Apr 2021, 08:59
by AmigaX86
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.

Re: Socket.ahk

Posted: 30 Jul 2021, 13:19
by xinxilas
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,

Re: Socket.ahk

Posted: 28 Oct 2021, 05:02
by william_ahk
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

Re: Socket.ahk

Posted: 06 Dec 2021, 19:40
by magusneo
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.

Re: Socket.ahk

Posted: 06 Jul 2022, 23:26
by ibieel
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