# AHKsock

作者: TheGood 最近更新时间: 20100824

AHKsock is a high-level wrapper which I have written to facilitate the use of the Winsock APIs in AHK. It will allow you to create clients and servers that can communicate with each other, purely written in AHK! The most important functions are:
o Listen - Starts listening on a port.
o Connect - Connects to a server.
o Send - Sends data to a connected socket.
o Close - Closes a connection.
o GetAddrInfo - Retrieves IP addresses from a hostname.
o GetNameInfo - Retrieves a hostname from an IP address.

AHKsock_AsyncSelect(wParam, lParam)
AHKsock_Close(iSocket = -1, iTimeout = 5000)
AHKsock_Connect(sName, sPort, sFunction)
AHKsock_ErrorHandler(sFunction = """")
AHKsock_ForceSend(iSocket, ptrData, iLength)
AHKsock_GetAddrInfo(sHostName, ByRef sIPList, bOne = False)
AHKsock_GetNameInfo(sIP, ByRef sHostName, sPort = 0, ByRef sService = "")
AHKsock_LastError()
AHKsock_Listen(sPort, sFunction = False)
AHKsock_RaiseError(1)
AHKsock_RegisterAsyncSelect(iSocket, fFlags = 43, sFunction = "AHKsock_AsyncSelect", iMsg = 0)
AHKsock_Send(iSocket, ptrData, iLength)
AHKsock_Settings(sSetting, sValue = "")
AHKsock_ShutdownSocket(AHKsock_Sockets("GetSocketFromIndex", A_Index)
AHKsock_Sockets("SetFunction", sktListen, sFunction)
AHKsock_SockOpt(iSocket, sOption, iValue = -1)
AHKsock_Startup(2)

关于函数的参数和返回值, 请参阅其源码或 此文档.

#### 备注

The demo file is "Example 4 - Hostname & IP Lookup" for AHK and AHK_L x86 ANSI by TheGood.

关于此函数(集)的更新细节和注意事项, 请参见 AutoHotkey 论坛: http://www.autohotkey.com/forum/viewtopic.php?t=58183

#### 许可

不存在

#### 示例

    /*! TheGood
        AHKsock - A simple AHK implementation of Winsock.
        AHKsock Example 4 - Hostname & IP Lookups
        http://www.autohotkey.com/forum/viewtopic.php?p=355775
        Last updated: August 24th, 2010

        This is just a very simple example that demonstrates the use of two inverse functions: AHKsock_GetAddrInfo and
        AHKsock_GetNameInfo. The code should be simple enough to follow.
    */
        ;We'll need to allow more than one instance to test it on the same machine
        #SingleInstance, Off

        ;Needed if AHKsock isn't in one of your lib folders
        ;#Include %A_ScriptDir%\AHKsock.ahk

        ;Set up an OnExit routine
        OnExit, GuiClose

        ;Make the GUI
        Gui, Add, Text,, What would you like to do?
        Gui, Add, Button, wp gbtnGAI, Hostname to IP(s)
        Gui, Add, Button, wp gbtnGNI, IP to Hostname
        Gui, Show
    Return

    GuiClose:
    GuiEscape:
        AHKsock_Close() ;No sockets to actually close here. We just do it to cleanup WinSock.
    ExitApp

    btnGAI:
        Gui, +OwnDialogs

        ;Random examples for the default value of the InputBox
        sEx1 := "www.google.com"
        sEx2 := "localhost"
        sEx3 := A_ComputerName
        Random, Default, 1, 3

        ;Ask for the hostname
        InputBox, sName, Hostname to IP(s), Please enter the hostname to look up:,,, 120,,,,, % sEx%Default%
        If ErrorLevel
            Return

        ;Get the IPs
        If (i := AHKsock_GetAddrInfo(sName, sIPList)) {
            MsgBox 0x10, Error, % "AHKsock_GetAddrInfo failed.`nReturn value = " i ".`nErrorLevel = " ErrorLevel
            Return
        }

        ;Display
        MsgBox 0x40, Results, % "Hostname:`n" sName "`n`nIP addresses found:`n" sIPList

    Return

    btnGNI:
        Gui, +OwnDialogs

        ;Random service examples for the default value of the InputBox
        sEx1 := 7  ;echo
        sEx2 := 21 ;ftp
        sEx3 := 25 ;SMTP
        sEx4 := 80 ;http
        Random, Default, 1, 4

        ;Ask for the IP
        InputBox, sIPandPort, IP to Hostname, Please enter the IP address (and optionally the port) to look up:,,, 120,,,,, % "127.0.0.1:" sEx%Default%
        If ErrorLevel
            Return

        ;Separate the IP and the port
        If Not (i := InStr(sIPandPort, ":"))
            sIP := sIPandPort, sPort := 0
        Else sIP := SubStr(sIPandPort, 1, i - 1), sPort := SubStr(sIPandPort, i + 1)

        ;Get the hostname
        If (i := AHKsock_GetNameInfo(sIP, sName, sPort, sService)) {
            MsgBox 0x10, Error, % "AHKsock_GetNameInfo failed.`nReturn value = " i ".`nErrorLevel = " ErrorLevel
            Return
        }

        ;Display
        MsgBox 0x40, Results, % "IP address: " sIP (sPort ? "`nPort: " sPort : "") "`n`nHostname: " sName (sPort ? "`nService: " sService : "")

    Return