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
JamixZol
  • Members
  • 54 posts
  • Last active: Jan 22 2014 04:35 AM
  • Joined: 26 Mar 2012
@Rathgar2

This lib is very capable of doing what you need. The chat example can handle this as well as the client server example, however I don't think anyone has implemented this yet. I have been working with this for a similar reason (not multiboxing) I did at one time multibox wow though, and after a ton of research landed on a free app called octopus. It's a soft kvm purpose built for multiboxing wow. I found it by searching octopus multiboxing but rather not link it here. In fact, I used it at work for a long time before switching to input director, which may also be worth a look if you need a soft kvm.
My code is built and tested for: AutoHotkey_L
Tools:Notify()Builder Dropiler
Libs:RFN Achieve
sಠಠn

Rathgar2
  • Members
  • 68 posts
  • Last active: Aug 23 2016 11:36 PM
  • Joined: 29 Mar 2012

@Rathgar2

This lib is very capable of doing what you need. The chat example can handle this as well as the client server example, however I don't think anyone has implemented this yet. I have been working with this for a similar reason (not multiboxing) I did at one time multibox wow though, and after a ton of research landed on a free app called octopus. It's a soft kvm purpose built for multiboxing wow. I found it by searching octopus multiboxing but rather not link it here. In fact, I used it at work for a long time before switching to input director, which may also be worth a look if you need a soft kvm.


@JamixZol
Wooooooooooooo Dogies!!! I had No Luck finding Octopus (it appears to have been 404'ed everywhere), but Input Director I did find and it works exactly the way I need it to!!!! I had to record a Macro inside Input Director to trigger my AHK scripts on my Slave machines but it totally works and it really only took a night of tinkering to get it up and running!! Yaup for Joy! Woooooooooooooo! Thanx ever so much for taking the time to reply and your advice was perfect!

Xymmer
  • Members
  • 2 posts
  • Last active: Apr 19 2012 02:53 PM
  • Joined: 19 Apr 2012

Would anybody here be able to give a headstart on how to use AHKSock to control Firefox using MozRepl (MozRepl GitHub page)?

Formerly, I used FF.ahk (FF.ahk forum thread), but since I've been using AHK_L, the WS2 library, which FF.ahk uses, doesn't work for me anymore.
All I need is to have FF.ahk to work with AHKSock instead of WS2, but I myself do not understand enough of the working principles to do it myself.


Finally solved this one. AHK_L stores strings internally as Unicode.
AHKSock sends strings as unicode (2 bytes each).
Mozrepl wants ascii.

Had to add the following to AHKSock_Send to force it to send ASCII:

strData := StrGet(ptrData, iLength)  ; get the string using the pointer sent
      strAsciiDataLength := StrPut(strData, "CP0")   ; get the string length as ascii (seems like it's always iLength + 1)
      VarSetCapacity(ptrAsciiData, strAsciiDataLength)    ; set a var with that length
      StrPut(strData, &ptrAsciiData, "CP0")   ; put the string as ascii into the new memory location
      ptrData := &ptrAsciiData   ; assign the ascii string pointer to the original ptrData var

Also:
- if you took the chat example as a starting point, you must not send a string length frame, mozrepl doesn't seem to like that
- Because ascii is 1 byte / char and unicode is 2 bytes / char), you must remove the "* 2" in the iTextLength example when sending
iTextLength := StrLen(sText) * 2


The attach zip has a modified ahksock that allows you to send a ascii/true boolean when using _send or _forcesend.
e.g.
bSendAsAscii := true
AHKsock_ForceSend(iPeerSocket, &sText, iTextLength, bSendAsAscii)


weapy
  • Members
  • 8 posts
  • Last active: Feb 11 2009 11:30 PM
  • Joined: 12 Jan 2009
Hello, can anyone help me with this AHKsock.ahk, i cant make it to work on any example scripts made.
i have AutoHotkey ANSI, downloaded the AHKsock.ahk for ANSI and few example files like AHKsock Example 1 - Simple Scenario - Client.ahk - AHKsock Example 1 - Simple Scenario - Server.ahk
edited each file to include ahksock
;Needed if AHKsock isn't in one of your lib folders
    #Include %A_ScriptDir%\AHKsock.ahk
But i get an error from AHKsock.ahk on line 456 from Ws2_32\GetAddrInfo dll-function call while the script try to AHKsock_Listen on server script.
Return 5 ;The getaddrinfo() call failed. The error is in ErrorLevel.
Why i cant make this to work? Where i do wrong? Thank you for your time

GG4000
  • Members
  • 4 posts
  • Last active: May 13 2012 05:23 PM
  • Joined: 13 May 2012
I was able to run the example after deleting a few lines that caused error messages.
Not sure what It's doing.
I'm trying to wrie a simple server with a port number to listen on.
Then the client Android app will send a message to it.
Are there any examples like that out there?

Help

Thank You

Crossfire
  • Guests
  • Last active:
  • Joined: --
Hey, Great script you have there. Im having a little trouble tho getting it to work multiple times. For example once it connect it just sends the initial data and then it stops. What i want to be able to do it send lets say 1 message to the server and the client gets a pop up. But i want that to be repeated if a hot key is pressed again. Basically i want something like your chat program where you can send data between the 2 clients. Unfortunately the chat program is a little to hard for me to understand. I stared at it for a couple days now and cant really grasp it. Maybe someone has or can guide me a to a simple script where when the programs start they connect to eachother but dont actually send data just yet. I want it to wait for me to hit a hotkey and send a string. Then the server will get that string and display it in a popup message. Once message is displayed i want it to be able to accept another one if the client sends it. I want this to work the other way around to where the server can send data to client and then pop up message happens.

I know im asking alot but im really ripping my hair out here. I know this shouldnt be too hard but i guess im just not grasping it.

Ill post my code now which is basically the examples with minor things tweaked.

Client
    #Include %A_ScriptDir%\AHKsock.ahk
    #SingleInstance, Off
	
    sServer := "localhost"
    OnExit, CloseAHKsock
    Menu, Tray, Add
    Menu, Tray, Add, Exit Gracefully, CloseAHKsock
	top:
    AHKsock_ErrorHandler("AHKsockErrors")
    If (i := AHKsock_Connect(sServer, 27015, "Recv")) {
        OutputDebug, % "AHKsock_Connect() failed with return value = " i " and ErrorLevel = " ErrorLevel
        ;ExitApp
    }
return

CloseAHKsock:
    AHKsock_Close()
ExitApp

Recv(sEvent, iSocket = 0, sName = 0, sAddr = 0, sPort = 0, ByRef bData = 0, iLength = 0) {
    
    If (sEvent = "CONNECTED") {
        If (iSocket = -1) {
            OutputDebug, % "Client - AHKsock_Connect() failed. Exiting..."
            ;ExitApp
        } Else OutputDebug, % "Client - AHKsock_Connect() successfully connected!"
        
    } Else If (sEvent = "DISCONNECTED") {
        
        OutputDebug, % "Client - The server closed the connection. Exiting..."
        ;ExitApp
        
    } Else If (sEvent = "RECEIVED") {
        OutputDebug, % "Client - We received " iLength " bytes."
		MsgBox,%bData%
		
    }
	return
}

AHKsockErrors(iError, iSocket) {
    OutputDebug, % "Client - Error " iError " with error code = " ErrorLevel ((iSocket <> -1) ? " on socket " iSocket : "")
}


Server
#Include %A_ScriptDir%\AHKsock.ahk
    OnExit, CloseAHKsock
    Menu, Tray, Add
    Menu, Tray, Add, Exit Gracefully, CloseAHKsock
    AHKsock_ErrorHandler("AHKsockErrors")
   ; VarSetCapacity(bData, bDataLength := 10, 0xFF)
	bData:="Message Test"
    bDataLength := StrLen(bData)
    ;Listen on port 27015

    If (i := AHKsock_Listen(27015, "Send")) {
        OutputDebug, % "AHKsock_Listen() failed with return value = " i " and ErrorLevel = " ErrorLevel
        ExitApp
    }
Return
CloseAHKsock:
    AHKsock_Close() 
ExitApp

Send(sEvent, iSocket = 0, sName = 0, sAddr = 0, sPort = 0, ByRef bRecvData = 0, bRecvDataLength = 0) {
    Global bData, bDataLength
    Static bDataSent, bConnected
    
    If (sEvent = "ACCEPTED") {
        OutputDebug, % "Server - A client connected!"

        If bConnected {
            OutputDebug, % "Server - Disconnected new client because we are still serving a previous client."
            AHKsock_Close(iSocket)
            
        } Else { ;If we weren't connected before, we are now!
            bConnected := True
            bDataSent := 0 ;Reset bDataSent for the new client
        }
        
    } Else If (sEvent = "DISCONNECTED") {
        OutputDebug, % "Server - The client disconnected. Going back to listening..."
        
    } Else If (sEvent = "SEND") {    
        Loop {
            
            ;Try to send the data
            If ((i := AHKsock_Send(iSocket, &bData + bDataSent, bDataLength - bDataSent)) < 0) {
                
                ;Check if we received WSAEWOULDBLOCK.
                If (i = -2) {
                    ;That's ok. We can leave and we'll keep sending from
                    ;where we left off the next time we get the SEND event.
                    Return
                    
                ;Something bad has happened with AHKsock_Send
                } Else OutputDebug, % "Server - AHKsock_Send failed with return value = " i " and ErrorLevel = " ErrorLevel
                
            ;We were able to send bytes!
            } Else OutputDebug, % "Server - Sent " i " bytes!"
            
            ;Check if everything was sent
            If (i < bDataLength - bDataSent)
                bDataSent += i ;Advance the offset so that at the next iteration, we'll start sending from where we left off
            Else Break ;We're done
        }
        bDataSent := 0
        
        OutputDebug, % "Server - Closing the client connection now..."
        If (i := AHKsock_Close(iSocket))
            OutputDebug, % "Server - The shutdown() call failed. ErrorLevel = " ErrorLevel
        bConnected := False
    }
}

;We're not actually handling errors here. This is here just to make us aware of errors if any do come up.
AHKsockErrors(iError, iSocket) {
    OutputDebug, % "Server - Error " iError " with error code = " ErrorLevel ((iSocket <> -1) ? " on socket " iSocket : "")
}


skyline7349
  • Members
  • 14 posts
  • Last active: Jun 05 2012 10:21 PM
  • Joined: 05 Jun 2012
Can someone please point me to a simple script like the chat one except dummied down? Ive been working on it for days but cant figure it out. I know it cant be hard i just dont understand AHKsock. I just want 1 script that can be opened twice and act as a server and client. They connect to eachother then using Hot keys can send and receive strings. For now i just want the strings to pop up in a MSGbox.

skyline7349
  • Members
  • 14 posts
  • Last active: Jun 05 2012 10:21 PM
  • Joined: 05 Jun 2012
Well since i guess noone knows i finally figured it out. Now my next question, is there a way to have 1 server(listening) with multiple connections? so if i send a string it sends to both computers?

Zaelia
  • Members
  • 754 posts
  • Last active: Jan 17 2015 02:38 AM
  • Joined: 31 Oct 2008
Yes, of course, it's possible but I can't help a lot, just some extra info because no answers...
Maybe try this too for understand without a library <!-- l --><a class="postlink-local" href="http://www.autohotkey.com/community/viewtopic.php?t=57306">viewtopic.php?t=57306</a><!-- l -->
You have to work like a chat server, any clients connected to one server, a client send a message to server and this server resend the message to other clients (or not, or just a targeted client if private message, or other features...) .
Posted Image
"You annoy me, therefore I exist."

skyline7349
  • Members
  • 14 posts
  • Last active: Jun 05 2012 10:21 PM
  • Joined: 05 Jun 2012
Thanks man i appreciate it. I think i have a better understanding of this now. I almost have up on this forum lol people arent that active.

Zaelia
  • Members
  • 754 posts
  • Last active: Jan 17 2015 02:38 AM
  • Joined: 31 Oct 2008
Bad luck :( Forum is into a period of change and AHKSock author (TheGood) seems gone at this moment... However this forum is a gold mine, you will need time but a lot of server/client or other network tool are already aviable. In my previous link I think you will find what you want for ahk basic without library, I spent a lot of time to investigate too.
"You annoy me, therefore I exist."

GetReal
  • Members
  • 6 posts
  • Last active: Dec 01 2017 11:23 PM
  • Joined: 13 Sep 2011
Does anyone have a copy of this library or one that would be able to replace it?

EDIT: Is this any good?

fincs
  • Moderators
  • 1662 posts
  • Last active:
  • Joined: 05 May 2007
It appears Google has cached the Unicode x86/x64 version of this script, but not the ANSI one.

GetReal
  • Members
  • 6 posts
  • Last active: Dec 01 2017 11:23 PM
  • Joined: 13 Sep 2011
@fincs: Thanks. While it may not be of use for some others, it is to me because that's the one I wanted. Again, thanks.

suoliweng
  • Members
  • 5 posts
  • Last active: Feb 14 2013 01:54 PM
  • Joined: 20 Mar 2012
how to auto connect to the server after the server restart?
i use a loop to keep connecting,but AHKsock_Connect() always get the error 1(AHKsock_Connect is still processing a connection attempt.)
i use the AHKsock_Send() to know that the connect has broken,and then set connect_state=0
keep_connect(){       ;check connect_state every 2 seconds
global connect_state  ;0 means disconnected,1 means connected
sServer := "127.0.0.1"
port=1235
Loop{
    ;ToolTip, in loop connect_state=%connect_state%
    if(connect_state=1){
        Sleep,2000
        continue
        }

    try{  ; Attempts to execute code.
        ;~ re:=AHKsock_Close()
        re2:=AHKsock_Connect(sServer, port, "start_s")
        ToolTip, try to connect %re2%  %ErrorLevel%
       }
    Sleep,2000
    }
}