Jump to content

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

About Winsock and TCP/IP


  • Please log in to reply
36 replies to this topic
Murp-e
  • Members
  • 531 posts
  • Last active: Sep 27 2011 11:44 AM
  • Joined: 12 Jan 2007
Can anyone provide some ideas as to what kind of things this can be used for?

Zaelia
  • Members
  • 754 posts
  • Last active: Jan 17 2015 02:38 AM
  • Joined: 31 Oct 2008
Murp|e, a lot of things... good or not, usefull or not, it's like a "telnet" with the powerfull scripting of AHK...

I'm going on vacation for 5 days, I will to rewrite the entire script with Ipv6 and UrlToIpv4(ToIPv4compatibleV6) for an easy use at my hollidays return.
I did some other tests, it's seems that Ipv6 work well (need more feedbacks), and we need only WS_32.dll for all the functions ( winsock2 or windows2000pro/serv or more )... sounds good!

Murp-e
  • Members
  • 531 posts
  • Last active: Sep 27 2011 11:44 AM
  • Joined: 12 Jan 2007
Zaelia: I'd still appreciate an example or two! I hope you have a wonderful vacation.

tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: Oct 13 2016 01:04 AM
  • Joined: 21 Dec 2007
search the forum for Sparrow for an idea of what one can do
Never lose.
WIN or LEARN.

Zaelia
  • Members
  • 754 posts
  • Last active: Jan 17 2015 02:38 AM
  • Joined: 31 Oct 2008
I have a small problem, I can't use freeaddrinfo ? (who delete dynamic variable ) doesn't work well :(

IPAddress = www.google.com
Port = 80 ; it's an option

; ... WSAStartUp ...

VarSetCapacity(mask, 256, 0) ; it's an option, you select ipv4 or ipv6 for return of numeric address
NumPut(2, mask, 4, "Short") ; 2 = ipv4  23 = ipv6
VarSetCapacity(Result, 256, 0)
msgbox % DllCall("Ws2_32\getaddrinfo", "Str", IPAddress, "Str", Port, "UInt", &mask, "UInt", &Result) " GetAddrInfo "

msgbox % DllCall("Ws2_32\WSAGetLastError") " Error "

; ----> My problem is here ! <----
;msgbox % DllCall("Ws2_32\freeaddrinfo", "UInt *", Result)
;msgbox % DllCall("Ws2_32\freeaddrinfo", "UInt", Result)
;msgbox % DllCall("Ws2_32\freeaddrinfo", "UInt", &Result)

loop 256 ; just for check Value, they parse value of structure in other structure ( ref table at bottom )
msgbox % NumGet(NumGet(Result, 0, "UInt")+a_index-1, 0, "UChar") " AddrInfo Result " a_index-1

; 0 -> 4 = allready an numeric address
; 4 -> 2 = ipv4 , 23 = ipv6
; 16 - > len (v4=16 v6=28)
; 24 to 39> ???? canon name ? and other, never mind ^^
; 40 -> begin of sockaddr: 2 = ipv4 , 23 = ipv6
; 42 to 44 -> port
; 44 to 47 -> numeric address returned (if ipv4)
; 48 to 63 -> numeric address returned (if ipv6)

Becarefull:

The getaddrinfo function with support for the IPv6 protocol is available on Windows XP and later. The IPv6 protocol is also supported on Windows 2000 when the IPv6 Technology Preview for Windows 2000 is installed.



Murp-e
  • Members
  • 531 posts
  • Last active: Sep 27 2011 11:44 AM
  • Joined: 12 Jan 2007
Tank: My understanding is that sparrow gives AHK the same type of scripting capabilities as ASP and PHP, namely generating and serving HTML pages on-the-fly. I think this script is similar to the underlying client-server technology that Sparrow uses, is that about right?

tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: Oct 13 2016 01:04 AM
  • Joined: 21 Dec 2007
No it allows you to use and execute ahk remotely
Never lose.
WIN or LEARN.

Murp-e
  • Members
  • 531 posts
  • Last active: Sep 27 2011 11:44 AM
  • Joined: 12 Jan 2007
Oh I see.... why didn't you just say so!

tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: Oct 13 2016 01:04 AM
  • Joined: 21 Dec 2007
in addition to redering documents uppon request
Never lose.
WIN or LEARN.

Zaelia
  • Members
  • 754 posts
  • Last active: Jan 17 2015 02:38 AM
  • Joined: 31 Oct 2008
If you want to know more:
<!-- m -->http://www.autohotke...ic.php?p=355775<!-- m -->
TheGood did this during my holydays, I do not know if I will continue...

tomoe_uehara
  • Members
  • 2166 posts
  • Last active: Jun 11 2015 05:33 PM
  • Joined: 05 Sep 2009
Please continue with your own project Zaelia, maybe we can see the difference between your script and TheGood's

Zaelia
  • Members
  • 754 posts
  • Last active: Jan 17 2015 02:38 AM
  • Joined: 31 Oct 2008
Perhaps I will continue with tcp/udp/ipv4/ipv6... When I find a solve I find a new problem ...

Now, a tiny UDP/IPV4 protocol script... but I have some problem with address and recvfrom()... However UDP is perfect for real time and fast/small message

IPAddress = 127.0.0.1
Port = 8765
SendToIPAddress = 127.0.0.1
SendToPort = 8766

Gui, Add, Edit, w320 r10 vLogEdit ReadOnly,
Gui, Add, Edit, section w200 R1 Limit255 vSendText,
Gui, Add, Button, ys gSendButton, Send
Gui, Show,, Tiny UDP
OnExit, ExitSub

VarSetCapacity(wsaData, 32)
DllCall("Ws2_32\WSAStartup", "UShort", 2, "UInt", &wsaData)
socket := DllCall("Ws2_32\socket", "Int", 2, "Int", 2, "Int", 17)

VarSetCapacity(sockaddr, 32)
NumPut(2, sockaddr, 0, "Short")
NumPut(DllCall("Ws2_32\htons", "UShort", Port), sockaddr, 2, "UShort")
NumPut(DllCall("Ws2_32\inet_addr", "Str", IPAddress), sockaddr, 4)
DllCall("Ws2_32\bind", "UInt", socket, "UInt", &sockaddr, "Int", 32) ; to receiv an UDP

DllCall("Ws2_32\WSAAsyncSelect", "UInt", socket, "UInt", WinExist("A"), "UInt", 0x5555, "Int", 1)
OnMessage(0x5555, "WinsockMessage")

log := (log := DllCall("Ws2_32\WSAGetLastError")) ? "Winsock error " log : "Tiny UDP ready !"
Guicontrol,, LogEdit, %log%
return

WinsockMessage(socket, event)
{
Critical
global log

VarSetCapacity(RecvData, 256, 0)
;VarSetCapacity(recvaddr, 32, 0) ; recvfrom() don't work, I don't know why
;DllCall("Ws2_32\recvfrom", "UInt", socket, "Str", recvdata, "Int", 256, "Int", 0, "UInt", &recvaddr, "Int", 32)
DllCall("Ws2_32\recv", "UInt", socket, "Str", recvdata, "Int", 256, "Int", 0)
log .= RecvData = "" ? "`n -> failure" : "`nRecv: " RecvData

Guicontrol,, LogEdit, %log%
controlsend, edit1, ^{end}, Tiny UDP
} 

SendButton:
GuiControlGet, SendText

VarSetCapacity(sendaddr, 32)
NumPut(2, sendaddr, 0, "Short")
NumPut(DllCall("Ws2_32\htons", "UShort", SendToPort), sendaddr, 2, "UShort")
NumPut(DllCall("Ws2_32\inet_addr", "Str", SendToIPAddress), sendaddr, 4)
DllCall("Ws2_32\sendto", "UInt", socket, "Str", SendText, "Int", StrLen(SendText), "Int", 0 , "UInt", &sendaddr, "Int", 32)
log .= "`nSend: " SendText

Guicontrol,, LogEdit, %log%
controlsend, edit1, ^{end}, Tiny UDP
Guicontrol,, SendText,
return

GuiClose:
ExitSub:
DllCall("Ws2_32\WSACleanup")
ExitApp


TLM
  • Administrators
  • 3864 posts
  • Last active:
  • Joined: 21 Aug 2006
ghod mode :shock: !

+1 Please do continue the Winsock and TCP/IP project.
Your code is greatly appreciated.

Posted Image

don't duplicate, iterate!


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

Here are a few suggestions to help you out:
1. Instead of using the log variable, it might be easier to use OutputDebug. Check the docs of the command for more info.
2. You HAVE to handle errors. This means actually checking the return value of the DllCall against what MSDN says it should return and outputting WSAGetLastError() when an error occurred. Otherwise, you won't know what's wrong!

For example, for your recvfrom() call, do something like:

r := DllCall("Ws2_32\recvfrom", "UInt", socket, "Str", recvdata, "Int", 256, "Int", 0, "UInt", &recvaddr, "Int", 32)
If (r = -1) or ErrorLevel {
    Outputdebug, % "An error occurred! r = " r ". ErrorLevel = " ErrorLevel ". WSAGetLastError = " DllCall("Ws2_32\WSAGetLastError")
}
I have never used recvfrom, so I can't tell you what's wrong. But looking at what WSAGetLastError tells you should put you on the right track! However, I did notice that you set the recvaddr to 32 bytes, when I *think* it should be 28 (sockaddr_in6 without the in6_addr is 12 bytes, and in6_addr is 16 bytes). Not sure if that's the only thing wrong though.

Z_Gecko
  • Guests
  • Last active:
  • Joined: --
take a look at http://de.autohotkey.../topic4544.html to see a working udp-script.