Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

AHKsock - A simple AHK implementation of Winsock (TCP/IP)


  • Please log in to reply
179 replies to this topic
Person93
  • Members
  • 443 posts
  • Last active: Feb 11 2014 12:07 AM
  • Joined: 26 Jan 2012

Hi AHK people!

 

I wrote this simple script as my first step in learning how to use this library.

#Include e:\Documents\AutoHotkey\Lib\AHKsock.ahk
OnExit, CloseAhkSock

sPort := 50000
sFunction := "TheFunc"
ListenErr := AHKsock_Listen(sPort, sFunction)
MsgBox, % "AHKSock_Listen returned " (ListenErr = ""?"nothing":ListenErr) " and Errorlevel = " ErrorLevel
return


CloseAhkSock:
Critical, on
if (!secondExit)
{
	secondExit := 1
	SetTimer, CloseAhkSock, 20
	return
}
else
	AHKsock_Close()
ExitApp

TheFunc(sEvent, iSocket = 0,sName = 0, sAddr = 0, sPort = 0, ByRef bData = 0, bDataLength = 0) {
}

I ran the script and the MsgBox popped up telling me that everything is in order.

 

I ran netstat -a -b and I did find Autohotkey.exe in the results.

 

How do I make a listening port?



jsmain
  • Members
  • 126 posts
  • Last active: Oct 23 2017 06:24 PM
  • Joined: 11 Jul 2005

Forgive me, as I've scoured this forum looking for the AHKsock.ahk source, and have been unable to locate it to include in my AHK LIb.

 

Can someone please point me in the proper direction for this source/download?

 

Thanks in advance.


Jeff Main

jsmain
  • Members
  • 126 posts
  • Last active: Oct 23 2017 06:24 PM
  • Joined: 11 Jul 2005

Spent nearly an hour searching, and finally, after posting the request, I found it with a continued search in only a short few minutes, So, thanks, but never mind the previous request.

 

For the benefit of others, it is located here... https://github.com/j...ter/AHKsock.ahk

 

It was actually very easy had I looked at the download link in the first post. Doh!


Jeff Main

panofish
  • Members
  • 179 posts
  • Last active: Apr 24 2014 03:24 PM
  • Joined: 05 Feb 2007

Suggestion: Update the docs to indicate that this library requires unicode ahk and doesn't work with ansi. :)



hahamed
  • Members
  • 8 posts
  • Last active: Jun 02 2016 02:58 AM
  • Joined: 25 Sep 2013

I want a merged client and server file transfer with AHKSock

can anyone help me?

I want use it for my organization nodes for sending and receiving files.

 

thanks



dangerdogL2121
  • Members
  • 636 posts
  • Last active: Nov 19 2015 03:39 AM
  • Joined: 22 Dec 2012

Hello, forum

I want to use ahksock to allow for communications between computers for videogames.

But, I am having trouble just getting the examples to work.

 

Lets say I'm trying the chatting.ahk example.  When the dialog box opens at the beginning, I put in my IP address.   Then I run it again in a new instance and input my IP address again.  The instances of the script don't connect to each other.

 

I have been having trouble with all of the examples, so maybe something big is wrong.  What should I do to get them to work?

 

Also, does ahksock have the kind of speed needed by videogames?

 

thanks


There is a new AutoHotkey forum here.


Icyhate
  • Members
  • 26 posts
  • Last active: Mar 29 2015 09:21 PM
  • Joined: 06 Aug 2013

sorry everyone but i am simply trying to send a string and recieve a string. 

this library is making me very upset.

can someone please explain to me how to send a string of data i have tried reverse engineering the chat file but it uses terms like highword and lowword. i dont understand i have googled these terms and dont understand them and i also dont understand why tehre would need to be any bitshifting. ugh i feel like this shouldn't be as complicated as i am making it out to be.



philu
  • Members
  • 15 posts
  • Last active: Jan 21 2016 06:34 PM
  • Joined: 28 Aug 2012

I dont even get the very first example running. AHKSock_Listen() fails wih return value=5 and ErrorLevel=-4. According to dllCall(), this means "The specified function could not be found inside the DLL.".

Why? do i have the wrong ws2_32.dll in my system32-folder? what can I do?.. please help me, I'd need this urgently

 

 

And why always me? .___.

 

thanks a lot



dangerdogL2121
  • Members
  • 636 posts
  • Last active: Nov 19 2015 03:39 AM
  • Joined: 22 Dec 2012

That is weird...

 

It sounds like you have a ws2_32.dll in your system folder, but not the right one.

If you haven't checked, search ws2_32.dll in the start menu to make sure you have one.

 

Have you changed anything in AHKsock.ahk or messed with the dlls in your computer?

 

The return I get from AHKSock_Listen is return is blank and ErrorLevel is 0 and AHKSock works for me.

 

Hope it helps


There is a new AutoHotkey forum here.


philu
  • Members
  • 15 posts
  • Last active: Jan 21 2016 06:34 PM
  • Joined: 28 Aug 2012

Thanks a lot dangerdog for your efforts. After some good time... I finally found what was the solution:

 

On Windows7, there are two "ws_2.32.dll"s:

  • @ System32 (the folder for 64-bit-files (total mind****)) which is 291 KB large, and
  • @ winsxs, maybe @ SysWOW64, the folder for 32-bit-files (WTF). Here its size 202 KB.

The latter does not include all of the needed functions. This means, when you're using AHK32bit, the smaller one will be opened which is not enough.

 

Long story cut short: SCRIPT NEEDS AUTOHOTKEY 64-BIT



philu
  • Members
  • 15 posts
  • Last active: Jan 21 2016 06:34 PM
  • Joined: 28 Aug 2012

ok so I had a closer look now, I like the documentation and functionality, well done

 

but do I understand right that I cannot send messages to and fro from server to client and vice versa? I have to use AHKsock_Connect() first on BOTH sides, making both server AND client?? this looks so weird to me :S

please confirm :D



dangerdogL2121
  • Members
  • 636 posts
  • Last active: Nov 19 2015 03:39 AM
  • Joined: 22 Dec 2012

I thought you didn't have to.

If the server has AHKsock_Listen() and then a client does AHKsock_Connect() they should be connected and be able to send messages then.

 

It would be good if someone more knowledgeable than myself tried to answer these questions.

 

Hope it helps


There is a new AutoHotkey forum here.


philu
  • Members
  • 15 posts
  • Last active: Jan 21 2016 06:34 PM
  • Joined: 28 Aug 2012

okay, you are right: sending messages back and forth IS possible. I just made the mistake to include a message box @ the "ACCEPTED" sEvent. this delays the connecting process and therefore prevents the "SEND" command from being sent.

hope this will help someone one day.

 

thank you again danger



Stefan_AC
  • New members
  • 1 posts
  • Last active: Sep 17 2015 08:51 AM
  • Joined: 31 Jul 2015

first: thx for your great work :-)

 

in short:

I try to write a Adobe Lightroom (LR) plugin to send keystrokes with AHK via sockets

I can connect, sending Data gives the correct number of sent Bytes,
but Lightroom doesnt get a receive Event :-(
with telnet it works

longer:
an innofficial? and a little buggy documentation and example from LR is here:
http://lightroomsolu...s/LrSocket.html
LR uses a bind function, it is a server, LR is 64 Bit
(with minor fixing) I can send Data with this example with Telnet (Win 8.1) and with netcat http://joncraton.org...at-for-windows/

but with the AHK script from this post
and with http://www.autohotke...tet/?hl= socket
and with https://msdn.microso...1(v=vs.85).aspx
its the same: Data seems to be sent but not received,
any ideas???

here you find a corrected LR plugin (with the other ahk library, but this makes no difference)
http://sk-webserver....vplugin.0.9.zip

 



SpoolDigity
  • Members
  • 1 posts
  • Last active: Oct 29 2015 08:06 PM
  • Joined: 22 Oct 2015

I am working on a chatting program using AHKsock and I have run into a problem I am hoping someone can shed some light on. My project is based off of example 3 - Messaging. After I have connected and sent a message, I want to disconnect and resume listening.

 

 What I have tried with no success is posted below. I am using AHKsock_close() and AHKsock_Listen() functions to try to disconnect and then resume listening. The program disconnects but apparently does not resume listening, because it is unresponsive to attempts from the script running on other machines to connect.

btnSend:
 
	sName = 172.29.112.98
    
    MsgBox, % AHKsock_Connect(sName, 27015, "Peer")
	
	Sleep, 1000
    
    /*! This GUI event will be called everytime the user clicks on the Send button, or when he presses the Enter key after
    typing text in the Edit control. The logic is rather straight-forward. We first send a string length frame to tell the
    peer how long the string coming will be, and we then send the string. See the StreamProcessor function for more
    information on frames.
    */
    
    ;Check if we're connected
    If (iPeerSocket = -1)
        Return
    
    ;Get the text to send
    GuiControlGet, sText,, txtInput
    
    ;Make sure we even have something to send
    If Not sText
        Return
    
    ;Get text length
    iTextLength := StrLen(sText) * 2
    
    ;First send a string length frame: high-word is 2, low-word is the length of the string
    VarSetCapacity(iFrame, 4, 0), NumPut((2 << 16) + iTextLength, iFrame, 0, "UInt")
    If (i := AHKsock_ForceSend(iPeerSocket, &iFrame, 4)) {
        OutputDebug, % "AHKsock_ForceSend failed with return value = " i " and error code = " ErrorLevel " at line " A_LineNumber
        ExitApp
    }
    
    ;Send the actual string now, excluding the null terminator
    If (i := AHKsock_ForceSend(iPeerSocket, &sText, iTextLength)) {
        OutputDebug, % "AHKsock_ForceSend failed with return value = " i " and error code = " ErrorLevel " at line " A_LineNumber
        ExitApp
    }
    
    ;Data was sent. Add it to the dialog.
    AddDialog(&sText)
    
    ;Clear the Edit control and give focus
    GuiControl,, txtInput
    GuiControl, Focus, txtInput
	
	Sleep, 1000
	
	AHKsock_Close()

        Sleep, 1000
	
	AHKsock_Listen(27015, "Peer")
Return