Jump to content

TCP UDF, Event driven!


Kip
 Share

Recommended Posts

Does this UDF work with AutoIT v. 3.3.0.0? From the first post I made TCP.au3, server.au3, and client.au3. I launch server.au3, wait a few seconds, then launch client.au3, but it always says "CLIENT: Could not connect. Are you sure the server is running?". I tried changing the port from 88 to some other numbers (same in both scripts) and setting the ipadress to 127.0.0.1.

Any suggestions would be appreciated.

Link to comment
Share on other sites

Does this UDF work with AutoIT v. 3.3.0.0? From the first post I made TCP.au3, server.au3, and client.au3. I launch server.au3, wait a few seconds, then launch client.au3, but it always says "CLIENT: Could not connect. Are you sure the server is running?". I tried changing the port from 88 to some other numbers (same in both scripts) and setting the ipadress to 127.0.0.1.

Any suggestions would be appreciated.

Yes, that UDF works with AutoIT 3.3.0.0

Known problems:

1. dosn't work that algorithm on client: connect to stoped server->get connected when server starts(works only with periodical _ASockConnect call)->disconnect when server stops (works)->connect to started server(dosnot work).

2. client and server examples on the first post dosnot work at all!

server_example.au3

CODE
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****

#AutoIt3Wrapper_UseUpx=n

#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#include "TCP.au3"

ToolTip("SERVER: Creating server...", 10, 30)

Global $hListenSock = _TCP_Server_Create(88); A server. Tadaa!

_TCP_RegisterEvent($hListenSock, $TCP_NEWCLIENT, "NewClient"); Whooooo! Now, this function (NewClient) get's called when a new client connects to the server.

_TCP_RegisterEvent($hListenSock, $TCP_DISCONNECT, "Disconnect"); And this,... this will get called when a client disconnects.

_TCP_RegisterEvent($hListenSock, $TCP_RECEIVE, "Received");

While 1

Sleep(1000)

WEnd

Func NewClient($hSocket, $iError); Yo, check this out! It's a $iError parameter! (In case you didn't noticed: It's in every function)

ToolTip("SERVER: New client connected." & @CRLF & "Sending this: Bleh!", 10, 30)

_TCP_Send($hSocket, "Bleh!"); Sending: "Bleh!" to the new client.

EndFunc ;==>NewClient

Func Disconnect($hSocket, $iError); Damn, we lost a client. Time of death: @Hour & @Min & @Sec :)

ToolTip("SERVER: Client disconnected.", 10, 30); Placing a tooltip right under the tooltips of the client.

EndFunc ;==>Disconnect

Func Received($hSocket, $sData, $iError)

ToolTip("SERVER: Client "&$hSocket&" said." & @CRLF & $sData, 10, 30)

_TCP_Send($hSocket, $sData);

EndFunc ;==>Received

client_example.au3

CODE
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****

#AutoIt3Wrapper_UseUpx=n

#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#include "TCP.au3"

ToolTip("CLIENT: Connecting...", 10, 10)

Global $hClientSoc = _TCP_Client_Create(@IPAddress1, 88); Create the client. Which will connect to the local ip address on port 88

_TCP_RegisterEvent($hClientSoc, $TCP_RECEIVE, "Received"); Function "Received" will get called when something is received

_TCP_RegisterEvent($hClientSoc, $TCP_CONNECT, "Connected"); And func "Connected" will get called when the client is connected.

_TCP_RegisterEvent($hClientSoc, $TCP_DISCONNECT, "Disconnected"); And "Disconnected" will get called when the server disconnects us, or when the connection is lost.

While 1

; just to keep the program running

Sleep(1000)

WEnd

Func Connected($hSocket, $iError); We registered this (you see?), When we're connected (or not) this function will be called.

If Not $iError Then; If there is no error...

ToolTip("CLIENT: Connected!", 10, 10); ... we're connected.

Else; ,else...

ToolTip("CLIENT: Could not connect. Are you sure the server is running?", 10, 10); ... we aren't.

EndIf

EndFunc ;==>Connected

Func Received($hSocket, $sData, $iError); And we also registered this! Our homemade do-it-yourself function gets called when something is received.

ToolTip("CLIENT: We received this: " & $sData, 10, 10); (and we'll display it)

EndFunc ;==>Received

Func Disconnected($hSocket, $iError); Our disconnect function. Notice that all functions should have an $iError parameter.

ToolTip("CLIENT: Connection closed or lost.", 10, 10)

EndFunc ;==>Disconnected

Link to comment
Share on other sites

The OP has an error in the client code; the port and IP address are swopped around...

This

CODE
$hClient = _TCP_Client_Create(88, @IPAddress1); Create the client. Which will connect to the local ip address on port 88

should be

CODE
$hClient = _TCP_Client_Create(@IPAddress1, 88); Create the client. Which will connect to the local ip address on port 88

fix that and it will work

Link to comment
Share on other sites

@Kip

The latest UDF uploaded has lots of errors coming up

C:\_\Apps\AutoIT3\UDF's\TCP\AsyncSocket\TCP.au3(170,6) : ERROR: syntax error
    For UBound
    ~~~~^
C:\_\Apps\AutoIT3\UDF's\TCP\AsyncSocket\TCP.au3(68,29) : ERROR: _ASocket(): undefined function.
    $hListenSocket = _ASocket( )
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\_\Apps\AutoIT3\UDF's\TCP\AsyncSocket\TCP.au3(71,65) : ERROR: _ASockSelect(): undefined function.
    _ASockSelect( $hListenSocket, $__TCP_WINDOW, 0x0400, $FD_ACCEPT)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\_\Apps\AutoIT3\UDF's\TCP\AsyncSocket\TCP.au3(75,45) : ERROR: _ASockListen(): undefined function.
    _ASockListen( $hListenSocket, $sIP, $iPort )
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\_\Apps\AutoIT3\UDF's\TCP\AsyncSocket\TCP.au3(88,35) : ERROR: _HiWord(): undefined function.
    Local $iError = _HiWord( $LParam )
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\_\Apps\AutoIT3\UDF's\TCP\AsyncSocket\TCP.au3(89,35) : ERROR: _LoWord(): undefined function.
    Local $iEvent = _LoWord( $LParam )
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\_\Apps\AutoIT3\UDF's\TCP\AsyncSocket\TCP.au3(104,71) : ERROR: __TCP_Server_OnSocketEvent(): undefined function.
                GUIRegisterMsg(0x0400 + $uBound - 1, "__TCP_Server_OnSocketEvent" )
                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\_\Apps\AutoIT3\UDF's\TCP\AsyncSocket\TCP.au3(143,46) : ERROR: _ASockShutdown(): undefined function.
        _ASockShutdown($__TCP_SOCKETS[$iElement][0])
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\_\Apps\AutoIT3\UDF's\TCP\AsyncSocket\TCP.au3(146,43) : ERROR: ___ArrayDelete(): undefined function.
        ___ArrayDelete($__TCP_SOCKETS, $iElement)
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\_\Apps\AutoIT3\UDF's\TCP\AsyncSocket\TCP.au3 - 9 error(s), 0 warning(s)

Can someone upload a good working version

thanks

ptrex

Link to comment
Share on other sites

I used examples from the 1-st post and fixed them working with tcp.au3 from the 1-st post.

As for error in TCP.au3: I suppose that errors are still present even in a last (fixed) post...

At least of 3 days i'm trying to make tcp windows service (tcp.au3 + service.au3). It is working :)

The main problem I've got with this: many-many errors in udfs, even in standart au3.3.0.0!

Link to comment
Share on other sites

Link to comment
Share on other sites

While applying tcp.au3 to my tcp service project i've found no func like _tcp_shutdown().

We have thise funcs:

_TCP_Server_Stop() - stops server socket $__TCP_SOCKETS[0][0]

and two identical funcs, that closes socket by handle

_TCP_Server_DisconnectClient($hSocket)

_TCP_Client_Stop($hSocket)

Its not a good idea to stop tcp.au3 based project with not all sockets closed!

I suggest:

1. to fix it with func _tcp_shutdown(), which closes all opened sockets and maybe makes tcpshutdown()

2. combine _TCP_Server_DisconnectClient($hSocket) and _TCP_Client_Stop($hSocket) in one like _TCP_Disconnect($hSocket)

There are no any init func. Its a bad idea too... I used 2 days to understand why my service dosnot stop correctly.

Than, why there are too many undeclared variables in code?

I suggest to make something like _tcp_startup(ByRef $whdlGui, $tcpstartup = 1) func to initialize that very usefull socket udf.

Am i right?

Thanx a lot to @Kip and all of U

Link to comment
Share on other sites

i have a question.... i want tht my client try to connect until my server is online.... but it didn't work....

this is my try script:

Client:

#include "TCP.au3"

    Global $hClient=""
    TryConn()
    
    Func TryConn()
    ToolTip("CLIENT: Connecting...",10,10)
    $hClient = _TCP_Client_Create(@IPAddress1, 88); Create the client. Which will connect to the local ip address on port 88
    EndFunc
    
    
    _TCP_RegisterEvent($hClient, $TCP_RECEIVE, "Received"); Function "Received" will get called when something is received
    _TCP_RegisterEvent($hClient, $TCP_CONNECT, "Connected"); And func "Connected" will get called when the client is connected.
    _TCP_RegisterEvent($hClient, $TCP_DISCONNECT, "Disconnected"); And "Disconnected" will get called when the server disconnects us, or when the connection is lost.
    
    While 1
; just to keep the program running
    WEnd
    
    Func Connected($hSocket, $iError); We registered this (you see?), When we're connected (or not) this function will be called.
        
        If not $iError Then; If there is no error...
            ToolTip("CLIENT: Connected!",10,10); ... we're connected.
        Else; ,else...
            ToolTip("CLIENT: Could not connect. Are you sure the server is running?",10,10); ... we aren't.
    ;Changed HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    sleep (500)
    TryConn()
        EndIf
        
    EndFunc
    
    
    Func Received($hSocket, $sReceived, $iError); And we also registered this! Our homemade do-it-yourself function gets called when something is received.
        ToolTip("CLIENT: We received this: "& $sReceived, 10,10); (and we'll display it)
    EndFunc
    
    Func Disconnected($hSocket, $iError); Our disconnect function. Notice that all functions should have an $iError parameter.
        ToolTip("CLIENT: Connection closed or lost.", 10,10)
    EndFunc

what's wrong?!?!?!?

Edited by IvanDream
Link to comment
Share on other sites

i have a question.... i want tht my client try to connect until my server is online.... but it didn't work....

this is my try script:

Your test script is nothing more than an example script. Please, take an a working example and try.

Take a look on your script and check:

- U have no sleep delay in your while loop.

- U have to place your tryconn() func in the different place - its stops executing client after tryconn() - is that what u supposed to?

Link to comment
Share on other sites

yes my scrips is the example with a modification....

i want tht my client try to connect to the server and if fail after some time retry to connect but it didn't work

I see. Try this:

CODE
#include "TCP.au3"

#Include <Timers.au3>

Global $tmrConnect = 0

Global $hClientSoc = _TCP_Client_Create(@IPAddress1, 88); Create the client. Which will connect to the local ip address on port 88

_TCP_RegisterEvent($hClientSoc, $TCP_RECEIVE, "Received"); Function "Received" will get called when something is received

_TCP_RegisterEvent($hClientSoc, $TCP_CONNECT, "Connected"); And func "Connected" will get called when the client is connected.

_TCP_RegisterEvent($hClientSoc, $TCP_DISCONNECT, "Disconnected"); And "Disconnected" will get called when the server disconnects us, or when the connection is lost.

While 1

Sleep (1000); just to keep the program running

WEnd

Exit

Func TryToConnect($hWnd, $Msg, $iIDTimer, $dwTime)

_ASockConnect($hClientSoc, @IPAddress1, 88)

EndFunc

Func SetTimerConnect()

If Not $tmrConnect Then $tmrConnect = _Timer_SetTimer($__TCP_WINDOW,5000,"TryToConnect")

EndFunc

Func StopTimerConnect()

If $tmrConnect Then

_Timer_KillTimer($__TCP_WINDOW, $tmrConnect)

$tmrConnect=0

EndIf

EndFunc

Func Connected($hSocket, $iError); We registered this (you see?), When we're connected (or not) this function will be called.

If not $iError Then; If there is no error...

ToolTip("CLIENT: Connected!",10,10); ... we're connected.

Else; ,else...

ToolTip("CLIENT: Could not connect. Are you sure the server is running?",10,10); ... we aren't.

;Changed HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

;~ sleep (500)

;~ TryConn()

SetTimerConnect()

EndIf

EndFunc

Func Received($hSocket, $sReceived, $iError); And we also registered this! Our homemade do-it-yourself function gets called when something is received.

ToolTip("CLIENT: We received this: "& $sReceived, 10,10); (and we'll display it)

EndFunc

Func Disconnected($hSocket, $iError); Our disconnect function. Notice that all functions should have an $iError parameter.

ToolTip("CLIENT: Connection closed or lost.", 10,10)

EndFunc

Link to comment
Share on other sites

I see. Try this:

CODE
#include "TCP.au3"

#Include <Timers.au3>

Global $tmrConnect = 0

Global $hClientSoc = _TCP_Client_Create(@IPAddress1, 88); Create the client. Which will connect to the local ip address on port 88

_TCP_RegisterEvent($hClientSoc, $TCP_RECEIVE, "Received"); Function "Received" will get called when something is received

_TCP_RegisterEvent($hClientSoc, $TCP_CONNECT, "Connected"); And func "Connected" will get called when the client is connected.

_TCP_RegisterEvent($hClientSoc, $TCP_DISCONNECT, "Disconnected"); And "Disconnected" will get called when the server disconnects us, or when the connection is lost.

While 1

Sleep (1000); just to keep the program running

WEnd

Exit

Func TryToConnect($hWnd, $Msg, $iIDTimer, $dwTime)

_ASockConnect($hClientSoc, @IPAddress1, 88)

EndFunc

Func SetTimerConnect()

If Not $tmrConnect Then $tmrConnect = _Timer_SetTimer($__TCP_WINDOW,5000,"TryToConnect")

EndFunc

Func StopTimerConnect()

If $tmrConnect Then

_Timer_KillTimer($__TCP_WINDOW, $tmrConnect)

$tmrConnect=0

EndIf

EndFunc

Func Connected($hSocket, $iError); We registered this (you see?), When we're connected (or not) this function will be called.

If not $iError Then; If there is no error...

ToolTip("CLIENT: Connected!",10,10); ... we're connected.

Else; ,else...

ToolTip("CLIENT: Could not connect. Are you sure the server is running?",10,10); ... we aren't.

;Changed HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

;~ sleep (500)

;~ TryConn()

SetTimerConnect()

EndIf

EndFunc

Func Received($hSocket, $sReceived, $iError); And we also registered this! Our homemade do-it-yourself function gets called when something is received.

ToolTip("CLIENT: We received this: "& $sReceived, 10,10); (and we'll display it)

EndFunc

Func Disconnected($hSocket, $iError); Our disconnect function. Notice that all functions should have an $iError parameter.

ToolTip("CLIENT: Connection closed or lost.", 10,10)

EndFunc

ok thx i understand now :)

Link to comment
Share on other sites

Downloaded and have been playing with it for a while. Works great for me!

Thanks very much, saves me having to learn some stuff. :)

Funny how I have an online FPS now... which you cant even shoot on yet. :lmao:

Thanks very much.

Still learning...I love autoit. :)

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...