MSDN...
<!-- m -->http://www.autohotke...pic.php?t=13829<!-- m -->
<!-- m -->http://www.autohotke...pic.php?t=32455<!-- m -->
<!-- m -->http://www.autohotke...pic.php?t=48616<!-- m -->
but I'm a noob... I try to understand scripts and to have a better knowledge or skill...
At this moment, I ask to you if they are a good way to do this ? Because script use loop or settimer or very hard function that I don't understand (byReff, buffer and other...). I want to know general problem for a TCP connection and winsock ( I have searshed a lot^^ )...
Finnaly my basic script, I think it's a good skeletor, but I don't know if this server can be strong because he use very often OnMessage.
Even if it is bad it has the merit of being compact for a reader and use another aproach...
Tiny Server
IPAddress = 127.0.0.1
Port = 8765
Gui, Add, Edit, w320 r10 vLogEdit ReadOnly,
Gui, Add, Edit, section w200 R1 Limit255 vSendText,
Gui, Add, Button, ys w100 gSendButton, Send
Gui, Show,, Tiny Server
OnExit, ExitSub
VarSetCapacity(wsaData, 32)
DllCall("Ws2_32\WSAStartup", "UShort", 2, "UInt", &wsaData)
socket := DllCall("Ws2_32\socket", "Int", 2, "Int", 1, "Int", 6)
VarSetCapacity(sockaddr, 16)
NumPut(2, sockaddr, 0, "UShort")
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", 16)
DllCall("Ws2_32\listen", "UInt", socket, "UInt", "SOMAXCONN")
socketList:= ","
DllCall("Ws2_32\WSAAsyncSelect", "UInt", socket, "UInt", WinExist("A"), "UInt", 0x5555, "Int", 1|8|10|32)
OnMessage(0x5555, "WinsockMessage")
log := (log := DllCall("Ws2_32\WSAGetLastError")) ? "Winsock error " log : "Server online"
Guicontrol,, LogEdit, %log%
return
WinsockMessage(socket, event)
{
Critical
global log, socketList
if event=1
{
VarSetCapacity(recvdata, 256, 0)
DllCall("Ws2_32\recv", "UInt", socket, "Str", recvdata, "Int", 256, "Int", 0)
log .= "`n" socket ": " recvdata
}
if event=8
{
socket := DllCall("Ws2_32\accept", "UInt", socket, "UInt", 0, "Int", 0)
socketList .= socket ","
log .= "`nClient connected " socket
}
if ( event=658833440 || event=32 )
{
log .= event=32 ? "`nClient offline " socket : "`nClient crashed " socket
StringReplace, socketList, socketList, `,%socket%`,, `,
DllCall("Ws2_32\closesocket" , "UInt", socket)
}
Guicontrol,, LogEdit, %log%
controlsend, edit1, ^{end}, Tiny Server
}
SendButton:
GuiControlGet, SendText
if !SendText
return
if InStr(SendText, "/kick") = 1
{
socket := SubStr(SendText, 7)
log .= DllCall("Ws2_32\closesocket" , "UInt", socket) ? "`nKick error" : "`nClient Kicked " socket
StringReplace, socketList, socketList, `,%socket%`,, `,
}
else
{
Loop, Parse, socketList , `,
{
if A_LoopField <>
DllCall("Ws2_32\send", "UInt", A_LoopField, "Str", SendText, "Int", StrLen(SendText), "Int", 0)
}
log .= "`n-> " SendText
}
Guicontrol,, LogEdit, %log%
controlsend, edit1, ^{end}, Tiny Server
Guicontrol,, SendText,
return
GuiClose:
ExitSub:
DllCall("Ws2_32\WSACleanup")
ExitAppTiny Client
IPAddress = 127.0.0.1
Port = 8765
Gui, Add, Edit, w320 r10 vLogEdit ReadOnly,
Gui, Add, Edit, section w200 R1 Limit255 vSendText,
Gui, Add, Button, ys gSendButton, Send
Gui, Show,, Tiny Client
OnExit, ExitSub
VarSetCapacity(wsaData, 32)
DllCall("Ws2_32\WSAStartup", "UShort", 2, "UInt", &wsaData)
socket := DllCall("Ws2_32\socket", "Int", 2, "Int", 1, "Int", 6)
VarSetCapacity(sockaddr, 16)
NumPut(2, sockaddr, 0, "UShort")
NumPut(DllCall("Ws2_32\htons", "UShort", Port), sockaddr, 2, "UShort")
NumPut(DllCall("Ws2_32\inet_addr", "Str", IPAddress), sockaddr, 4)
DllCall("Ws2_32\connect", "UInt", socket, "UInt", &sockaddr, "Int", 16)
DllCall("Ws2_32\WSAAsyncSelect", "UInt", socket, "UInt", WinExist("A"), "UInt", 0x5555, "Int", 1|32)
OnMessage(0x5555, "WinsockMessage")
log := (log := DllCall("Ws2_32\WSAGetLastError")) ? "Winsock error " log : "Client online " socket
Guicontrol,, LogEdit, %log%
return
WinsockMessage(socket, event)
{
Critical
global log, disconn
if event=1
{
VarSetCapacity(recvdata, 256, 0)
DllCall("Ws2_32\recv", "UInt", socket, "Str", recvdata, "Int", 256, "Int", 0)
log .= "`n" socket ": " recvdata
}
if event=32
{
log .= disconn=1 ? "`nClient offline" : "`nClient kicked"
DllCall("Ws2_32\WSACleanup")
}
if event=658833440
{
log .= "`nServer crashed"
DllCall("Ws2_32\WSACleanup")
}
Guicontrol,, LogEdit, %log%
controlsend, edit1, ^{end}, Tiny Client
}
SendButton:
GuiControlGet, SendText
if !SendText
return
if ( SendText = "/logout" )
{
disconn = 1
DllCall("Ws2_32\shutdown", "UInt", socket, "Int", 2)
}
else
{
DllCall("Ws2_32\send", "UInt", socket, "Str", SendText, "Int", StrLen(SendText), "Int", 0)
log .= "`n-> " SendText
}
Guicontrol,, LogEdit, %log%
controlsend, edit1, ^{end}, Tiny Client
Guicontrol,, SendText,
return
GuiClose:
ExitSub:
DllCall("Ws2_32\WSACleanup")
ExitAppThanks, to share your experience about ahk,winsock,tcp with me
Edit: (Title)




