<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[Серый форум &mdash; AHK: TCP сервер узнать количество подключенных клиентов]]></title>
		<link>http://forum.script-coding.com/viewtopic.php?id=12367</link>
		<atom:link href="http://forum.script-coding.com/extern.php?action=feed&amp;tid=12367&amp;type=rss" rel="self" type="application/rss+xml" />
		<description><![CDATA[Недавние сообщения в теме «AHK: TCP сервер узнать количество подключенных клиентов».]]></description>
		<lastBuildDate>Thu, 26 Jan 2017 16:02:40 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: AHK: TCP сервер узнать количество подключенных клиентов]]></title>
			<link>http://forum.script-coding.com/viewtopic.php?pid=111575#p111575</link>
			<description><![CDATA[<p>Спасибо.</p>]]></description>
			<author><![CDATA[null@example.com (Dworkin)]]></author>
			<pubDate>Thu, 26 Jan 2017 16:02:40 +0000</pubDate>
			<guid>http://forum.script-coding.com/viewtopic.php?pid=111575#p111575</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: TCP сервер узнать количество подключенных клиентов]]></title>
			<link>http://forum.script-coding.com/viewtopic.php?pid=111573#p111573</link>
			<description><![CDATA[<p>Проверять возвращаемое значение функции send:<br /><a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms740149(v=vs.85).aspx">https://msdn.microsoft.com/en-us/librar … s.85).aspx</a></p>]]></description>
			<author><![CDATA[null@example.com (Malcev)]]></author>
			<pubDate>Thu, 26 Jan 2017 14:10:29 +0000</pubDate>
			<guid>http://forum.script-coding.com/viewtopic.php?pid=111573#p111573</guid>
		</item>
		<item>
			<title><![CDATA[AHK: TCP сервер узнать количество подключенных клиентов]]></title>
			<link>http://forum.script-coding.com/viewtopic.php?pid=111558#p111558</link>
			<description><![CDATA[<p>Подскажите пожалуйста. <br />Переменная ConnectionList(в конце кода) собирает сокеты подключенных клиентов и когда я нажимаю F2 то сервер посылает текст всем кто в этой переменной, НО если кто-то из клиентов отключается то он не удаляется с этой переменной.<br />Как можно узнать количество подключенных клиентов к серверу? Или как узнать кто отключился что бы его удалить из переменной?</p><p>Вот код сервера.<br /></p><div class="codebox"><pre><code>#SingleInstance Force
Network_Port = 27015
Network_Address = 192.168.0.102
 
NewData := false
DataReceived =
Progress, m2 b fs13 zh0 WMn700 X0 Y400, Server is running
Gui 99: show, hide, Slave script ; hidden &quot;message receiver window&quot;

Gosub Connection_Init
return
 
Connection_Init:
OnExit, ExitSub
socket := PrepareForIncomingConnection(Network_Address, Network_Port)
if socket = -1
    ExitApp
 
Process, Exist
DetectHiddenWindows On
ScriptMainWindowId := WinExist(&quot;ahk_class AutoHotkey ahk_pid &quot; . ErrorLevel)
DetectHiddenWindows Off
 
NotificationMsg = 0x5555
OnMessage(NotificationMsg, &quot;ReceiveData&quot;)
 
ExitMsg = 0x6666
OnMessage(ExitMsg, &quot;ExitData&quot;)
 
FD_READ = 1
FD_CLOSE = 32
FD_CONNECT = 20
 
if DllCall(&quot;Ws2_32\WSAAsyncSelect&quot;, &quot;UInt&quot;, socket, &quot;UInt&quot;, ScriptMainWindowId, &quot;UInt&quot;, ExitMsg, &quot;Int&quot;, FD_CLOSE)
{
    msgbox, closed
}
 
if DllCall(&quot;Ws2_32\WSAAsyncSelect&quot;, &quot;UInt&quot;, socket, &quot;UInt&quot;, ScriptMainWindowId, &quot;UInt&quot;, NotificationMsg, &quot;Int&quot;,FD_READ|FD_CONNECT) 
{
    MsgBox % &quot;WSAAsyncSelect() indicated Winsock error &quot;
          . DllCall(&quot;Ws2_32\WSAGetLastError&quot;)
    DllCall(&quot;Ws2_32\WSAAsyncSelect&quot;, &quot;UInt&quot;, socket, &quot;UInt&quot;, ScriptMainWindowId, &quot;UInt&quot;, ExitMsg, &quot;Int&quot;, FD_CLOSE)
    ExitApp
}

SetTimer, NewConnectionCheck, 500
return
 
PrepareForIncomingConnection(IPAddress, Port)
{
    VarSetCapacity(wsaData, 32)
    result := DllCall(&quot;Ws2_32\WSAStartup&quot;, &quot;UShort&quot;, 0x0002, &quot;UInt&quot;, &amp;wsaData)
    if ErrorLevel
    {
        MsgBox % &quot;WSAStartup() could not be called due to error %ErrorLevel%. &quot;
                . &quot;Winsock 2.0 or higher is required.&quot;
        return -1
    }
    if result
    {
        MsgBox % &quot;WSAStartup() indicated Winsock error &quot;
                . DllCall(&quot;Ws2_32\WSAGetLastError&quot;)
        return -1
    }
    AF_INET = 2
    SOCK_STREAM = 1
    IPPROTO_TCP = 6
    socket := DllCall(&quot;Ws2_32\socket&quot;, &quot;Int&quot;, AF_INET, &quot;Int&quot;, SOCK_STREAM, &quot;Int&quot;, IPPROTO_TCP)
    if socket = -1
    {
        MsgBox % &quot;socket() indicated Winsock error &quot;
                . DllCall(&quot;Ws2_32\WSAGetLastError&quot;)
        return -1
    }
    SizeOfSocketAddress = 16
    VarSetCapacity(SocketAddress, SizeOfSocketAddress)
    InsertInteger(2, SocketAddress, 0, AF_INET)
    InsertInteger(DllCall(&quot;Ws2_32\htons&quot;, &quot;UShort&quot;, Port), SocketAddress, 2, 2)
    InsertInteger(DllCall(&quot;Ws2_32\inet_addr&quot;, &quot;Str&quot;, IPAddress),SocketAddress, 4, 4)
    if DllCall(&quot;Ws2_32\bind&quot;, &quot;UInt&quot;, socket, &quot;UInt&quot;, &amp;SocketAddress, &quot;Int&quot;, SizeOfSocketAddress)
    {
        MsgBox % &quot;bind() indicated Winsock error &quot;
                . DllCall(&quot;Ws2_32\WSAGetLastError&quot;) . &quot;?&quot;
        return -1
    }
    if DllCall(&quot;Ws2_32\listen&quot;, &quot;UInt&quot;, socket, &quot;UInt&quot;, &quot;SOMAXCONN&quot;)
    {
        MsgBox % &quot;LISTEN() indicated Winsock error &quot;
                . DllCall(&quot;Ws2_32\WSAGetLastError&quot;) . &quot;?&quot;
        return -1
    }
    return socket
}
 
ReceiveData(wParam, lParam)
{
    global DataReceived
    global NewData
    global mydata
    global ConnectionList
    socket := wParam
    ReceivedDataSize = 4096
    Loop
    {
        VarSetCapacity(ReceivedData, ReceivedDataSize, 0)
        ReceivedDataLength := DllCall(&quot;Ws2_32\recv&quot;, &quot;UInt&quot;, socket, &quot;Str&quot;, ReceivedData, &quot;Int&quot;, ReceivedDataSize, &quot;Int&quot;, 0)
	if ReceivedDataLength = 0
        {   
            StringReplace, ConnectionList, ConnectionList, %socket%`n
            DllCall(&quot;Ws2_32\closesocket&quot;, &quot;UInt&quot;, socket)
        } 
        if ReceivedDataLength = -1
        {
            WinsockError := DllCall(&quot;Ws2_32\WSAGetLastError&quot;)
            if WinsockError = 10035
            {
                DataReceived = %TempDataReceived%
                NewData := true
                return 1
            }
            if WinsockError &lt;&gt; 10054
            {
                MsgBox % &quot;recv() indicated Winsock error &quot; . WinsockError
                StringReplace, ConnectionList, ConnectionList, %socket%`n
                DllCall(&quot;Ws2_32\closesocket&quot;, &quot;UInt&quot;, socket)
            }
        }
        mydata := ReceivedData
        gosub myreceive
	if (A_Index = 1)
            TempDataReceived =
                TempDataReceived = %TempDataReceived%%ReceivedData%
    }
    return 1
}
 
ExitData(wParam, lParam)
{
    global ConnectionList
    socket := wParam
    ReceivedDataSize = 16
    VarSetCapacity(ReceivedData, ReceivedDataSize, 0)
    ReceivedDataLength := DllCall(&quot;Ws2_32\recv&quot;, &quot;UInt&quot;, socket, &quot;Str&quot;, ReceivedData, &quot;Int&quot;, ReceivedDataSize, &quot;Int&quot;, 0)
    StringReplace, ConnectionList, ConnectionList, %socket%`n
    DllCall(&quot;Ws2_32\closesocket&quot;, &quot;UInt&quot;, socket)
    return 1
}
 
SendData(wParam,SendData)
{
    SendDataSize := VarSetCapacity(SendData)
    SendDataSize += 1
    Loop, parse, wParam, `n
    {
        If A_LoopField =
           Continue
        socket := A_LoopField
        sendret := DllCall(&quot;Ws2_32\send&quot;, &quot;UInt&quot;, socket, &quot;Str&quot;, SendData, &quot;Int&quot;, SendDatasize, &quot;Int&quot;, 0)
    }
}
 
 
InsertInteger(pInteger, ByRef pDest, pOffset = 0, pSize = 4)
{
    Loop %pSize%
        DllCall(&quot;RtlFillMemory&quot;, &quot;UInt&quot;, &amp;pDest + pOffset + A_Index-1, &quot;UInt&quot;, 1, &quot;UChar&quot;, pInteger &gt;&gt; 8*(A_Index-1) &amp; 0xFF)
}
 
NewConnectionCheck:
ConnectionCheck := DllCall(&quot;Ws2_32\accept&quot;, &quot;UInt&quot;, socket, &quot;UInt&quot;, &amp;SocketAddress, &quot;Int&quot;, SizeOfSocketAddress)
if ConnectionCheck &gt; 1
    ConnectionList = %ConnectionList%%ConnectionCheck%`n
Return
 
myreceive:
 msgbox, %mydata%
return
 
GuiClose:
ExitSub:
DllCall(&quot;Ws2_32\WSACleanup&quot;)
ExitApp

F2::
If ConnectionList &lt;&gt;
{
    SendText = test
    sendData(ConnectionList,SendText)
}
else
{
 msgbox, Нет клиентов
}
return</code></pre></div>]]></description>
			<author><![CDATA[null@example.com (Dworkin)]]></author>
			<pubDate>Wed, 25 Jan 2017 21:33:28 +0000</pubDate>
			<guid>http://forum.script-coding.com/viewtopic.php?pid=111558#p111558</guid>
		</item>
	</channel>
</rss>
