Echo server: Difference between revisions

Content added Content deleted
(added factor example)
m (→‎{{header|AutoHotkey}}: format to improve)
Line 6: Line 6:
The implementation must not stop responding to other clients if one client sends a partial line or stops reading responses.
The implementation must not stop responding to other clients if one client sends a partial line or stops reading responses.
=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<tt>echoserver.ahk</tt>, modified from
{{lines_too_long}}
echoserver.ahk, modified from
[http://www.autohotkey.com/forum/topic13829.html script] by zed gecko.
[http://www.autohotkey.com/forum/topic13829.html script] by zed gecko.
<lang AutoHotkey>#SingleInstance Force
<lang AutoHotkey>#SingleInstance Force
Line 42: Line 41:
"UInt", ScriptMainWindowId, "UInt", ExitMsg, "Int", FD_CLOSE)
"UInt", ScriptMainWindowId, "UInt", ExitMsg, "Int", FD_CLOSE)
{
{
msgbox, closed
msgbox, closed
}
}


if DllCall("Ws2_32\WSAAsyncSelect", "UInt", socket,
if DllCall("Ws2_32\WSAAsyncSelect", "UInt", socket,
"UInt", ScriptMainWindowId, "UInt", NotificationMsg, "Int", FD_READ|FD_CONNECT)
"UInt", ScriptMainWindowId, "UInt", NotificationMsg, "Int",
FD_READ|FD_CONNECT)
{
{
MsgBox % "WSAAsyncSelect() indicated Winsock error " . DllCall("Ws2_32\WSAGetLastError")
MsgBox % "WSAAsyncSelect() indicated Winsock error "
DllCall("Ws2_32\WSAAsyncSelect", "UInt", socket,
. DllCall("Ws2_32\WSAGetLastError")
DllCall("Ws2_32\WSAAsyncSelect", "UInt", socket,
"UInt", ScriptMainWindowId, "UInt", ExitMsg, "Int", FD_CLOSE)
"UInt", ScriptMainWindowId, "UInt", ExitMsg, "Int", FD_CLOSE)
ExitApp
ExitApp
}
}
Line 63: Line 64:
if ErrorLevel
if ErrorLevel
{
{
MsgBox WSAStartup() could not be called due to error %ErrorLevel%. Winsock 2.0 or higher is required.
MsgBox % "WSAStartup() could not be called due to error %ErrorLevel%. "
. "Winsock 2.0 or higher is required."
return -1
return -1
}
}
if result
if result
{
{
MsgBox % "WSAStartup() indicated Winsock error " . DllCall("Ws2_32\WSAGetLastError")
MsgBox % "WSAStartup() indicated Winsock error "
. DllCall("Ws2_32\WSAGetLastError")
return -1
return -1
}
}
Line 78: Line 81:
if socket = -1
if socket = -1
{
{
MsgBox % "socket() indicated Winsock error " . DllCall("Ws2_32\WSAGetLastError")
MsgBox % "socket() indicated Winsock error "
. DllCall("Ws2_32\WSAGetLastError")
return -1
return -1
}
}
Line 85: Line 89:
InsertInteger(2, SocketAddress, 0, AF_INET)
InsertInteger(2, SocketAddress, 0, AF_INET)
InsertInteger(DllCall("Ws2_32\htons", "UShort", Port), SocketAddress, 2, 2)
InsertInteger(DllCall("Ws2_32\htons", "UShort", Port), SocketAddress, 2, 2)
InsertInteger(DllCall("Ws2_32\inet_addr", "Str", IPAddress), SocketAddress, 4, 4)
InsertInteger(DllCall("Ws2_32\inet_addr", "Str", IPAddress),
SocketAddress, 4, 4)
if DllCall("Ws2_32\bind", "UInt", socket,
if DllCall("Ws2_32\bind", "UInt", socket,
"UInt", &SocketAddress, "Int", SizeOfSocketAddress)
"UInt", &SocketAddress, "Int", SizeOfSocketAddress)
{
{
MsgBox % "bind() indicated Winsock error " . DllCall("Ws2_32\WSAGetLastError") . "?"
MsgBox % "bind() indicated Winsock error "
. DllCall("Ws2_32\WSAGetLastError") . "?"
return -1
return -1
}
}
if DllCall("Ws2_32\listen", "UInt", socket, "UInt", "SOMAXCONN")
if DllCall("Ws2_32\listen", "UInt", socket, "UInt", "SOMAXCONN")
{
{
MsgBox % "LISTEN() indicated Winsock error " . DllCall("Ws2_32\WSAGetLastError") . "?"
MsgBox % "LISTEN() indicated Winsock error "
. DllCall("Ws2_32\WSAGetLastError") . "?"
return -1
return -1
}
}
Line 201: Line 208:
DllCall("Ws2_32\WSACleanup")
DllCall("Ws2_32\WSACleanup")
ExitApp</lang>
ExitApp</lang>
echoclient.ahk
<tt>echoclient.ahk</tt>
<lang AutoHotkey>#SingleInstance OFF
<lang AutoHotkey>#SingleInstance OFF
Network_Port = 12321
Network_Port = 12321
Line 236: Line 243:
FD_CLOSE = 32
FD_CLOSE = 32
if DllCall("Ws2_32\WSAAsyncSelect", "UInt", socket,
if DllCall("Ws2_32\WSAAsyncSelect", "UInt", socket,
"UInt", ScriptMainWindowId, "UInt", NotificationMsg, "Int", FD_READ|FD_CLOSE)
"UInt", ScriptMainWindowId, "UInt", NotificationMsg, "Int",
FD_READ|FD_CLOSE)
{
{
MsgBox % "WSAAsyncSelect() indicated Winsock error " . DllCall("Ws2_32\WSAGetLastError")
MsgBox % "WSAAsyncSelect() indicated Winsock error "
. DllCall("Ws2_32\WSAGetLastError")
ExitApp
ExitApp
}
}
Line 251: Line 260:
if ErrorLevel
if ErrorLevel
{
{
MsgBox WSAStartup() could not be called due to error %ErrorLevel%. Winsock 2.0 or higher is required.
MsgBox % "WSAStartup() could not be called due to error %ErrorLevel%. "
. "Winsock 2.0 or higher is required."
return -1
return -1
}
}
if result
if result
{
{
MsgBox % "WSAStartup() indicated Winsock error " . DllCall("Ws2_32\WSAGetLastError")
MsgBox % "WSAStartup() indicated Winsock error "
. DllCall("Ws2_32\WSAGetLastError")
return -1
return -1
}
}
Line 266: Line 277:
if socket = -1
if socket = -1
{
{
MsgBox % "socket() indicated Winsock error " . DllCall("Ws2_32\WSAGetLastError")
MsgBox % "socket() indicated Winsock error "
. DllCall("Ws2_32\WSAGetLastError")
return -1
return -1
}
}
Line 273: Line 285:
InsertInteger(2, SocketAddress, 0, AF_INET)
InsertInteger(2, SocketAddress, 0, AF_INET)
InsertInteger(DllCall("Ws2_32\htons", "UShort", Port), SocketAddress, 2, 2)
InsertInteger(DllCall("Ws2_32\htons", "UShort", Port), SocketAddress, 2, 2)
InsertInteger(DllCall("Ws2_32\inet_addr", "Str", IPAddress), SocketAddress, 4, 4)
InsertInteger(DllCall("Ws2_32\inet_addr", "Str", IPAddress),
SocketAddress, 4, 4)
if DllCall("Ws2_32\connect", "UInt", socket,
if DllCall("Ws2_32\connect", "UInt", socket,
"UInt", &SocketAddress, "Int", SizeOfSocketAddress)
"UInt", &SocketAddress, "Int", SizeOfSocketAddress)
{
{
MsgBox % "connect() indicated Winsock error " . DllCall("Ws2_32\WSAGetLastError") . "?"
MsgBox % "connect() indicated Winsock error "
. DllCall("Ws2_32\WSAGetLastError") . "?"
return -1
return -1
}
}