Celtic88 Posted March 12, 2018 Share Posted March 12, 2018 Hi all, The code allows to checks if an event happened on one of the tcp connection , call return : event : $TCPEvent_Disconnect = Connection is dead $TCPEvent_None = Noting.. $TCPEvent_Data = data arrived #by celtic88 Global Const $TCPEvent_Disconnect = -1 ;Connection is dead Global Const $TCPEvent_None = 0 ;Noting.. Global Const $TCPEvent_Data = 1 ;data available Func TCPSocketEvent($hSocket) Local $timeval = DllStructCreate("int tv_sec;int tv_usec") Local $fd_set = DllStructCreate("int fd_count;UINT fd_array[64]") Local $ValLong = DllStructCreate("long length") DllStructSetData($fd_set, "fd_count", 1) DllStructSetData($fd_set, "fd_array", $hSocket, 1) Local $result = DllCall("Ws2_32.dll", "int", "select", "int", $hSocket + 1, "struct*", $fd_set, "ptr", 0, "ptr", 0, "struct*", $timeval) If @error Then Return $TCPEvent_Disconnect If $result[0] < 0 Then Return $TCPEvent_Disconnect ; Seems to be an error Local $result2 = DllCall("Ws2_32.dll", "int", "__WSAFDIsSet", "UINT", $hSocket, "struct*", $fd_set) If @error Then Return $TCPEvent_Disconnect If $result[0] = 0 Or $result2[0] = 0 Then Return $TCPEvent_None ; No data available Local $FIONREAD = 0x4004667F Local $aRet = DllCall("Ws2_32.dll", "int", "ioctlsocket", "uint", $hSocket, "long", $FIONREAD, "struct*", $ValLong) If @error Then Return $TCPEvent_Disconnect If $aRet[0] <> 0 Then Return $TCPEvent_Disconnect Local $length = DllStructGetData($ValLong, "length") If $length = 0 Then Return $TCPEvent_Disconnect ;~ Return $length Return $TCPEvent_Data EndFunc ;==>TCPSocketIsAlive exemple : Server : expandcollapse popup#include <TCPEvent.au3> Func Example() TCPStartup() Local $sIPAddress = "0.0.0.0" Local $iPort = 65432 Local $iListenSocket = TCPListen($sIPAddress, $iPort, 100) Local $iError = 0 If @error Then $iError = @error MsgBox(16, "Server", "Could not listen, Error code: " & $iError) Return False EndIf Local $iSocket = 0 Do $iSocket = TCPAccept($iListenSocket) If @error Then $iError = @error MsgBox(16, "Server", "Could not accept the incoming connection, Error code: " & $iError) Return False EndIf Until $iSocket <> -1 MsgBox(32, "Server", "Client Connected") Local $EvSock While 1 Sleep(100) $EvSock = TCPEvent($iSocket) Switch $EvSock Case $NetworkEvent_Disconnect MsgBox(32, "Server", "Connection closed") ExitLoop Case $NetworkEvent_Data MsgBox(32, "Server", "Reciev Data : " & TCPRecv($iSocket, 1000)) TCPSend($iSocket, "Test Ok") MsgBox(32, "Server", "Exit") ExitLoop EndSwitch WEnd TCPCloseSocket($iSocket) EndFunc ;==>Example Example() client expandcollapse popup#include <TCPEvent.au3> Func Exemple() TCPStartup() Local $sIPAddress = "127.0.0.1" Local $iPort = 65432 Local $iSocket = TCPConnect($sIPAddress, $iPort) If @error Then Local $iError = @error MsgBox(16, "Client", "Could not connect, Error code: " & $iError) Else MsgBox(32, "Client", "Connection successful") TCPSend($iSocket, "Test") Local $EvSock While 1 Sleep(100) $EvSock = TCPEvent($iSocket) Switch $EvSock Case $TCPEvent_Disconnect MsgBox(32, "Client", "Connection closed") ExitLoop Case $TCPEvent_Data MsgBox(32, "Client", "Reciev Data : " & TCPRecv($iSocket, 1000)) EndSwitch WEnd EndIf ; Close the socket. TCPCloseSocket($iSocket) EndFunc ;==>Exemple Exemple() coffeeturtle 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now