Jump to content
Sign in to follow this  

Tcp event

Recommended Posts


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 :

#include <TCPEvent.au3>

Func Example()

    Local $sIPAddress = ""
    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

    Local $iSocket = 0
        $iSocket = TCPAccept($iListenSocket)

        If @error Then
            $iError = @error
            MsgBox(16, "Server", "Could not accept the incoming connection, Error code: " & $iError)
            Return False
    Until $iSocket <> -1

    MsgBox(32, "Server", "Client Connected")

    Local $EvSock
    While 1
        $EvSock = TCPEvent($iSocket)
        Switch $EvSock
            Case $NetworkEvent_Disconnect
                MsgBox(32, "Server", "Connection closed")
            Case $NetworkEvent_Data
                MsgBox(32, "Server", "Reciev Data : " & TCPRecv($iSocket, 1000))
                TCPSend($iSocket, "Test Ok")
                MsgBox(32, "Server", "Exit")

EndFunc   ;==>Example





#include <TCPEvent.au3>

Func Exemple()

    Local $sIPAddress = ""
    Local $iPort = 65432

    Local $iSocket = TCPConnect($sIPAddress, $iPort)

    If @error Then
        Local $iError = @error
        MsgBox(16, "Client", "Could not connect, Error code: " & $iError)
        MsgBox(32, "Client", "Connection successful")
        TCPSend($iSocket, "Test")
        Local $EvSock
        While 1
            $EvSock = TCPEvent($iSocket)
            Switch $EvSock
                Case $TCPEvent_Disconnect
                    MsgBox(32, "Client", "Connection closed")
                Case $TCPEvent_Data
                    MsgBox(32, "Client", "Reciev Data : " & TCPRecv($iSocket, 1000))

    ; Close the socket.
EndFunc   ;==>Exemple





  • Thanks 1

Share this post

Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Similar Content

    • arcker
      By arcker

      Arck System.
      Version 0.1 Beta

      alpha version ( proof of concept ).
      Version 0.1 Beta *
      What is it ?

      Au3 FastCGI is a CGI handler that will run constantly to get request from webservers
      The process will send back html code to the webserver.
      It’s the same as php or other language that acts as cgi.
      FastCGI is powerful since CGI spawn a process on each request. FastCGI works with TCP connection, so it’s always started to handle request.
      Some reads : http://www.fastcgi.com/drupal/

      Features :

      This udf enables FastCGI to be used with autoit.
      FastCGI is used by a lot of webservers ( mainly to use php ) to call functions written in other languages. Its main purpose is too use sockets on existants processes instead of stdios to send/receive result and spawn process.
      With is, you can spawn your fastcgi “listener” then let it run. A service is in preparation to do so.
      You can use any webserver that support FastCGI. Lighttp, apache, and more, make a total third part architecture. The webserver with your firewall can run in linux and Au3_fastcgi_Server can run on a dedicated server.
      Multiple instances can run, so many processes can run, to avoid contention and make a bigger webserver.

      This UDF uses
      TCP_Event by kip to boost performances. ( avoid loop ) Bininbinary by ward to fastcheck only needed informations. ( avoid uses of multiple structure, can gain 1-2 ms ) ( will be in proper version ) ( not used in FastCGI_Lite ) For now, this udf lets you make webservers in autoit with easy integration. By detaching webserver and fastcgi, you can easily update your webserver.I’ve attached a script that you rerun your script in scite to test. Simply right-click on autoit icon when running, then click restart, and it will close and tells cite to reload it. Be careful, scite will run active tab.
      To use it, you have to think this way :
      The main process will handle all of your process code. You have to let the engine function, then add your functions to test it.
      When you call http://localhost/test.au3, the script will call “test” function.
      If the function doesn’t exists, a custom 404 error is thrown. For now, you can’t set subpages ( like /somepages/test/index.au3 ) only “root” can be called.
      The Fast CGI Lite script is my paperboard. You can execute it and test some pages, but it will changed often in the future. Please test your script in an external file.
      HOW TO RUN

      First of all, start the ServerControl.exe, then click start, and reduces it if everything ok. If another process is listening to port 80, close it, or change port in ligttpd config files. Open Au3_FastCGI_Lite.au3 in Scite and run it Open your favorite webbrower and type http://localhost/index.au3
      You should see a 404 custom page Now go to http://localhost/_all.au3 TO DO LIST

      Add SubPages Support Add GZip support ( should be on lighttp size, don’t know) Add ability to send/receive files and process them Add Multiple process example Add Session ID or something like that Benchmark TCP Event <> TCP Loop

      Your code must contain engine code. All of your pages must be functions ( for now, I plan to make something different with an array soon ) All au3 code must be added in. You can btw use external files to make more. AutoIt can’t compile code on the fly (as php does ), so your “listener” must be fully stopped and restarted to test changes. Doesn’t support session id ( for now ) DOWNLOAD