#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
Opt("GUIOnEventMode", 1) ; Включить режим отслеживания событий GUI.
Opt("TrayIconHide", 1)
Global $IPAddress = "127.0.0.1" ; This IP Address only works for testing on your own computer.
Global $Port = 53555 ; Port used for the connection.
; ====================================================================================================================
; Создаем визуальное окно и объекты в нем.
; ====================================================================================================================
; Создать визуальное окно.
$WINGUI = GUICreate("TEST", 470, 245, -1, -1)
; Информауионные строки.
$lbCLN1 = GUICtrlCreateLabel("DATA_CLIENT1:", 10, 10, 100, 15, $SS_LEFTNOWORDWRAP, $GUI_WS_EX_PARENTDRAG)
$lbCLN2 = GUICtrlCreateLabel("DATA_CLIENT2:", 10, 25, 100, 15, $SS_LEFTNOWORDWRAP, $GUI_WS_EX_PARENTDRAG)
$lbCLN3 = GUICtrlCreateLabel("DATA_CLIENT3:", 10, 40, 100, 15, $SS_LEFTNOWORDWRAP, $GUI_WS_EX_PARENTDRAG)
$lbCLN4 = GUICtrlCreateLabel("DATA_CLIENT4:", 10, 55, 100, 15, $SS_LEFTNOWORDWRAP, $GUI_WS_EX_PARENTDRAG)
$idCLN1 = GUICtrlCreateLabel("0", 105, 10, 65, 15, $SS_LEFTNOWORDWRAP, $GUI_WS_EX_PARENTDRAG)
$idCLN2 = GUICtrlCreateLabel("0", 105, 25, 65, 15, $SS_LEFTNOWORDWRAP, $GUI_WS_EX_PARENTDRAG)
$idCLN3 = GUICtrlCreateLabel("0", 105, 40, 65, 15, $SS_LEFTNOWORDWRAP, $GUI_WS_EX_PARENTDRAG)
$idCLN4 = GUICtrlCreateLabel("0", 105, 55, 65, 15, $SS_LEFTNOWORDWRAP, $GUI_WS_EX_PARENTDRAG)
; Системные события.
GUISetOnEvent($GUI_EVENT_CLOSE, "_CLOSEEvent")
GUISetState(@SW_SHOW, $WINGUI)
Local $STTCP = TCPStartup() ; Запускаем службу TCP.
If $STTCP <> 1 Then
MsgBox(1, "Статус запуска сервера TCP", "Ошибка запуска службы TCP!")
EndIf
While 1 ; Если соединение было разорванно, попробовать снова через 1 секунду.
_STARTSERVER()
Sleep(1000)
WEnd
; ====================================================================================================================
; Сервер TCP.
; ====================================================================================================================
Func _STARTSERVER()
; Assign a Local variable the socket and bind to the IP Address and Port specified with a maximum of 10 pending connexions.
Local $iListenSocket = TCPListen($IPAddress, $Port, 10)
Local $iError = 0
If @error Then
$iError = @error
TCPCloseSocket($iListenSocket)
Return
EndIf
; Assign a Local variable to be used by the Client socket.
Local $iSocket = 0
Do ; Wait for someone to connect (Unlimited).
; Accept incomming connexions if present (Socket to close when finished; one socket per client).
$iSocket = TCPAccept($iListenSocket)
; If an error occurred display the error code and return False.
If @error Then
$iError = @error
TCPCloseSocket($iListenSocket)
Return
EndIf
Until $iSocket <> -1 ;if different from -1 a client is connected.
; Close the Listening socket to allow afterward binds.
TCPCloseSocket($iListenSocket)
While 1
; Assign a Local variable the data received.
Local $sReceived = ""
Do
$sReceived = TCPRecv($iSocket, 2048)
If @error Then
$iError = @error
TCPCloseSocket($iListenSocket)
TCPCloseSocket($iSocket)
Return
EndIf
Until $sReceived <> ""
GUICtrlSetData($idCLN1, $sReceived)
WEnd
EndFunc
; ====================================================================================================================
; Функции системных событий.
; ====================================================================================================================
Func _CLOSEEvent()
TCPShutdown() ; Остановить службу TCP.
Exit
EndFunc