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
Blue60
  • Guests
  • Last active:
  • Joined: --
Thank you, you are King. That works! Jay

franzk
  • Guests
  • Last active:
  • Joined: --
Could someone post a quick example of streaming data.
ie: telnet into a site and refresh a gui with the stream it produces !

cheers :?

Mr Elmo
  • Members
  • 18 posts
  • Last active: Jun 18 2010 05:46 PM
  • Joined: 25 Oct 2006
Franzk: See Example #3's StreamProcessor function. Read through that entire example for a good understanding of frames. (I think one of the best things about this library/examples is that it actually includes a good course on Winsocks/TCP framing in the comments!)

TheGood: Great Stuff! I can see this being really useful for AHK users for as long as AHK is still used.

I have one suggestion, and in the same stream as Franzk's question. I'd be lost without the StreamProcessor you included in your Example #3. Would you consider including a general version of that function in the library? I believe this would greatly ease script-to-script communication because strings are the native variable for all AHK scripts.

If I may suggest a possible syntax...
R := AHKsock_StreamProcessor(Socket, bNewData, bNewDataLength)
if (R = "") {
	; Stream is still processing, no string to output yet
} else {
	Msgbox Received complete string: %R%
}
To keep it straight with other sockets, SP would need to use %Socket% in the name of its static variables
(Also maybe include AHKsock_StreamProcessor(Socket) in the recv() 'disconnect' routine to automatically clear the buffer for that socket?)

And complementing it with a new function for the sender's side...

AHKsock_StreamSend(Socket, StringVar) {
	; Send StringLength Frame
	; Send String
}

Whaddaya think? I can put in some work on these later on if you think these features would jive well with your ideas for the library.
%MemorablePhrase%

franzk
  • Guests
  • Last active:
  • Joined: --
When I try example 3 I get an error

Call to non-existent function
Specifically: Anchor(htxtDialog,"wh")
Line 105.
?? any ideas ?

franzk
  • Guests
  • Last active:
  • Joined: --
All I want to do is watch my mail server port 25 and if key words appear in the streaming output perform an action.

eg: HELO mail.microsoft.com -> popup msgbox..

Thanks :?

Mr Elmo
  • Members
  • 18 posts
  • Last active: Jun 18 2010 05:46 PM
  • Joined: 25 Oct 2006
To answer you Anchor problem, you need to include the stdlib file Anchor.ahk in your Autohotkey/Lib folder. See this topic for details on that.

Whether or not AHKsock is the answer to your specific problem with your mailbox, I don't know.
%MemorablePhrase%

franzk
  • Guests
  • Last active:
  • Joined: --
@Mr Elmo - Thanks that sorted the Anchor.

If anyone can help I'd be very grateful 8)

TheGood
  • Members
  • 589 posts
  • Last active: Mar 22 2014 03:22 PM
  • Joined: 30 Jul 2007

All I want to do is watch my mail server port 25 and if key words appear in the streaming output perform an action.

eg: HELO mail.microsoft.com -> popup msgbox..

Thanks :?

Hey franzk. This is much more complicated than that. Like I said before, AHKsock only takes care of interacting with the transport layer and under. But the application layer is all up to you. AHKsock does not "speak" any protocol in that layer (like HTTP, FTP, and TelNet). It is up to you to take care of the application layer protocol, either by making up your own, or by abiding to a well-known protocol (which is hard since you would need to have deep knowledge of the protocol). For example, in Example 3, I made up my own kind of protocol (which uses different types of frames). Look into it for more info.

I hope this helps! :)

I have one suggestion, and in the same stream as Franzk's question. I'd be lost without the StreamProcessor you included in your Example #3. Would you consider including a general version of that function in the library? I believe this would greatly ease script-to-script communication because strings are the native variable for all AHK scripts.

Hi Mr Elmo,
Why not just copy my StreamProcessor() function into your script and adapt it to your needs? That's what I built it for! :)

In reality, I did think about doing what you suggested, but decided against it simply because it is not something that all applications need. As you can see, Example 1 and Example 2 do not use any stream processing function (although Example 2 does have a simplified version of a stream processor).

It would also be restraining if the user uses delimiting (ie. consider all the data between two delimiters as one frame) instead of length-prefixing (as used in the SP function) to "create" frames.

More generally, applications that do not communicate in a frame-like manner could dispense of the StreamProcessor() for maximum efficiency. It makes more sense to have it part of the user script, because it will always need to be adapted to fit the application's requirements.

Hope this helps!

Edit: I also added Anchor() into Example 3.

HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008
Great work, it looks really promising :)

Any plans to add Unicode support for AutoHotkey_L?

TheGood
  • Members
  • 589 posts
  • Last active: Mar 22 2014 03:22 PM
  • Joined: 30 Jul 2007

Great work, it looks really promising :)

Thanks!

Any plans to add Unicode support for AutoHotkey_L?

Working on it as we speak! :)
Almost done in fact.

HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008
Cool 8)

TheGood
  • Members
  • 589 posts
  • Last active: Mar 22 2014 03:22 PM
  • Joined: 30 Jul 2007
Added AHK_L x86/x64 Unicode support!

HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008

Added AHK_L x86/x64 Unicode support!

Thank you very much ;)

JoeSchmoe
  • Members
  • 304 posts
  • Last active: Feb 28 2013 05:39 PM
  • Joined: 17 Feb 2008
Just discovered this library. Truly amazing! Many thanks for all of your great work. It is truly appreciated. I am fairly new to DLLCall and using the Windows API in general, so your tutorials are very helpful. I've learned a lot so far by slowly working through your examples.

As I describe in this thread, I'm very interested in getting an AHK client to be able to talk to a server running PHP. I chose PHP because it is very similar to AHK, incredibly easy to use and learn, and has a well supported built in library for Berkeley sockets. In particular, I suspect that the functions available in the PHP Socket Functions library are probably close analogues to the functions available in the Winsock library or other Berkeley Sockets based libraries. Plus, it's quite easy to download PHP for windows or even XAMPP,

Any tips on how I might go about making a PHP server to talk with your AHK library? This is probably a long shot, but any chance that you might be willing to write up some example pseudo code, just describing which functions I should call and what sort of inputs I should send to those functions?

TheGood
  • Members
  • 589 posts
  • Last active: Mar 22 2014 03:22 PM
  • Joined: 30 Jul 2007

Just discovered this library. [...]

Thanks! I'm happy to know you got something out of it. :)

As I describe in this thread, I'm very interested in getting an AHK client to be able to talk to a server running PHP.

Sure. There's nothing stopping you from writing the server in PHP and the client in AHK. If you're the one making both, then it's just a matter of making sure you're using the same protocol (which you will have to invent).

For example, in my chatting scripts, my "protocol" was to use typing update frames and string length frames to communicate. As long as this basic system is obeyed, the client/server could have been written in any language supporting WinSock.

Any tips on how I might go about making a PHP server to talk with your AHK library?

This is probably a long shot, but any chance that you might be willing to write up some example pseudo code, just describing which functions I should call and what sort of inputs I should send to those functions?

Unfortunately, I do not have any experience with PHP, but it should be pretty straight-forward. You would use AHKsock_Connect to connect to the PHP server, and then, assuming it's the PHP server that starts sending data, you would intercept the RECEIVED event and treat it as necessary. In the opposite case, if the AHK client is the one that starts sending data, you would send data either in the SEND event using AHKsock_Send or outside the funciton using AHKsock_ForceSend.

AHKsock_Connect(MyPHPServer, Port, "Function")
Return

Function(sEvent, iSocket = 0, sName = 0, sAddr = 0, sPort = 0, ByRef bData = 0, bDataLength = 0) {
    If (sEvent = "RECEIVED")
        ;You received data from the PHP server. Do what you need to do, i.e. save it to a file, display information, etc...
    Else If (sEvent = "SEND")
        ;You are clear for sending data to the PHP server, if any.
        AHKsock_Send(iSocket, &MyData, MyDataLength)
        ;You can also use AHKsock_ForceSend() instead outside of the function. (See docs and examples...)
}

I know this is very general, but all you really need to do is simply to follow the same protocol on both sides.