Jump to content

TCP UDF, Event driven!


Kip
 Share

Recommended Posts

Was a while ago I checked this out but I remember that it used dllCall's instead of the inbuilt TCP functions.

So is this better than using the inbuilt or what? (working on a game)

I don't know how the standard functions are build, but my UDF still uses the standard TCPReceive, TCPSend and TCPAccept functions. The only differences are at creating and closing sockets.

Link to comment
Share on other sites

I ran it live on 5 servers on Tuesday night and ran into no issues. Each server was handling about 160 clients with no issue.

Thanks for the fix!

Can you please give more details about your tests?

Did the all servers run on one machine?

How did you simulate 160 X 4 clients?

What machine/s did you use? cpu, memory, etc...

Be Green Now or Never (BGNN)!

Link to comment
Share on other sites

  • 2 weeks later...

I've been using this UDF for a while and its great.

I have been trying to host a server on my computer for the clients to connect to,

From my second computer next to me everything works fine and i can connect,

But when other clients from other places try to connect they cant connect to the server.

Do i need to host my server somewhere or is it my router settings or something?

Link to comment
Share on other sites

I've been using this UDF for a while and its great.

I have been trying to host a server on my computer for the clients to connect to,

From my second computer next to me everything works fine and i can connect,

But when other clients from other places try to connect they cant connect to the server.

Do i need to host my server somewhere or is it my router settings or something?

It could be anything, usually router settings or a firewall.

Link to comment
Share on other sites

Hey there,

First of all: Amazing UDF. I use it now allways when i handle with TCP/IP.

But now ive got a problem:

I cant create a Server and a Client in one Script, is that true?

I just createted a server (on port 9666) and then i tried to connect to an other server (on port 3949) and it faild.

Well, the other Server showed me, that i am connected. But my Client doenst give a callback that i am connected.

#include <TCP.au3>


$hServer = _TCP_Server_Create("9666") ;If i comment this line out, it work pretty well

_TCP_RegisterEvent($hServer,$TCP_NEWCLIENT,"_TCP_Newclient")
_TCP_RegisterEvent($hServer,$TCP_RECEIVE,"_TCP_Recv")


$hClient = _TCP_Client_Create("192.168.0.102",399)

_TCP_RegisterEvent($hClient,$TCP_CONNECT,"_TCP_Connect")
_TCP_RegisterEvent($hClient,$TCP_RECEIVE,"_TCP_Recv")

While 1
    Sleep(100)
WEnd



Func _TCP_Newclient($hSocket,$iError) ;Works
    MsgBox(0,"New Client",$hSocket&"_"&$iError)
EndFunc


Func _TCP_Recv($hSocket,$sData,$iError) ;Works, but only with server
    MsgBox(0,"Recv",$sData)
EndFunc


Func _TCP_Connect($hSocket,$iError);Won't work --> Also doenst give an error or smth like that
    MsgBox(0,"Connected!",$hSocket&"_"&$iError)
EndFunc

Do ya have an idea to fix that?

Greetings

Spider

www.AutoIt.de - Moderator of the German AutoIt Forum

 

Link to comment
Share on other sites

  • 3 weeks later...

Hi there,

i'm wondering if theres a need to unregister events before recreating new ones.

in my test client, i can get disconnected.

By clicking on a button, I can reconnect.

it's calling the 'connect()' function which calls the _TCP_Client_Create function and Registers 3 events (Connect, Disconnect & Receive).

The problem is that the Connect Event is not triggered when I reconnect my client.

Do I have to unregister event in some way ?

Heres is my code (its just a client which purpose is testing my server) :

#include <TCP.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>

GUICreate("Network Test - Client", 500, 200, -1, -1)
Global $GUI_Messages = GUICtrlCreateEdit("en attente d'une action", 120, 10, 370, 180, $ES_AUTOVSCROLL + $WS_VSCROLL + $ES_READONLY)
GUICtrlSetBkColor ($GUI_Messages, 0xffffff)
$GUI_Button_Send1 = GUICtrlCreateButton("Connect", 10, 10, 100, 30)
$GUI_Button_Send2 = GUICtrlCreateButton("Send2", 10, 50, 100, 30)

GUISetState()



Func Connect()
    Global $hclient
    ConsoleWrite("hclient avant TCP_Client_Create=" & $hclient & @CRLF)
    Global $hClient = _TCP_Client_Create(@IPAddress1, 37601); Create the client. Which will connect to the local ip address on port 88
    ConsoleWrite("hclient apres TCP_Client_Create=" & $hclient & @CRLF)
    _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.
    _TCP_RegisterEvent($hClient, $TCP_RECEIVE, "Received"); Function "Received" will get called when something is received
EndFunc

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
        Case $msg = $GUI_Button_Send1
            Connect()
        Case $msg = $GUI_Button_Send2
            ConsoleWrite("envoi au serveur !" & @CRLF)
            GUICtrlSetData($GUI_Messages, @CRLF & "Tentative d'identification", 1)
            If Not TCPSend($hclient,"2569EA0F69A71DD56BD87750|#A01") Then GUICtrlSetData($GUI_Messages, @CRLF & "Echec de l'envoi - wsa error:" & @error, 1)
    EndSelect
WEnd


Func Connected($hSocket, $iError); We registered this (you see?), When we're connected (or not) this function will be called.
ConsoleWrite("appel de la fonction 'connected()'" & @crlf)
    If not $iError Then; If there is no error...
        GUICtrlSetData($GUI_Messages, @CRLF & "Connected to Server", 1); ... we're connected.
    Else; ,else...
        GUICtrlSetData($GUI_Messages, @CRLF & "Could not Connect", 1); ... we aren't.
    EndIf
EndFunc


Func Received($hSocket, $sReceived, $iError); And we also registered this! Our homemade do-it-yourself function gets called when something is received.
    GUICtrlSetData($GUI_Messages, @CRLF & $sReceived, 1) ;(and we'll display it)
    ConsoleWrite("appel de la fonction Received()" & @CRLF)
EndFunc

Func Disconnected($hSocket, $iError); Our disconnect function. Notice that all functions should have an $iError parameter.
    GUICtrlSetData($GUI_Messages, @CRLF & "Connection Lost", 1)
EndFunc
Link to comment
Share on other sites

  • 2 weeks later...
  • 5 weeks later...

The problem is that the Connect Event is not triggered when I reconnect my client.

I've just started "playing" with Kips code and I can't get the "$TCP_CONNECT" to trigger ANYTIME.

When I look at the code I'm wondering what "connect" actually does.

When _TCP_Client_Create is called, watching the messages from the server and client it appears as if the connection is made then.

Before "$TCP_CONNECT" is registered.

IF I shut the server down and restart there is no "auto-reconnect" so no need for a "$TCP_CONNECT".

When I reconnect (as you do) again the _TCP_Client_Create does the connection before the new "$TCP_CONNECT" is registered.

The only time I've seen it trigger is when the server "DIS-connects"???

John Morrison

aka

Storm-E

Link to comment
Share on other sites

I've just started "playing" with Kips code and I can't get the "$TCP_CONNECT" to trigger ANYTIME.

When I look at the code I'm wondering what "connect" actually does.

When _TCP_Client_Create is called, watching the messages from the server and client it appears as if the connection is made then.

Before "$TCP_CONNECT" is registered.

IF I shut the server down and restart there is no "auto-reconnect" so no need for a "$TCP_CONNECT".

When I reconnect (as you do) again the _TCP_Client_Create does the connection before the new "$TCP_CONNECT" is registered.

The only time I've seen it trigger is when the server "DIS-connects"???

John Morrison

aka

Storm-E

You can see it triggered if you will use valid IP address on your network but the server part should be down.

I am also can't understand the behavior. Regards.

The

Be Green Now or Never (BGNN)!

Link to comment
Share on other sites

Both my first post and the UDF itself say that $TCP_CONNECT can only be used within a client script.

Run the client example that can be downloaded from my first post. Do NOT run the server script. Does it say it can't connect? If so, the $TCP_CONNECT is perfectly fine, but your code isn't.

Edited by Kip
Link to comment
Share on other sites

Both my first post and the UDF itself say that $TCP_CONNECT can only be used within a client script.

Run the client example that can be downloaded from my first post. Do NOT run the server script. Does it say it can't connect? If so, the $TCP_CONNECT is perfectly fine, but your code isn't.

Thanks Kip

Now it makes more sense!

I missed the "CLIENT is Connected." tooltip as it was being overwritten by the immeditate message recieved from the server.

Then I jusy got confused somehow thinking the "_TCP_Client_Create" was doing the conneciton.

I went back over my code remove a far bit of useless error checking adn hay presto it works as you siad it would.

Another DOH! moment brought to you by

John Morrison

AKA

Storm-E

Link to comment
Share on other sites

Hi,

I have a LAN wireless network set to public at home.

I shout down the server and ran the client (client and server examples from the first post).

I used the IP Address "1.1.1.1" in _TCP_Client_Create("1.1.1.1", $88).

The result of this test is that the Connected function reported success connecting to the server.

Any idea why?

Edited by lsakizada

Be Green Now or Never (BGNN)!

Link to comment
Share on other sites

I see the question of whether you can pass an array into _TCP_Send data (2nd parameter) was addressed early on, in this thread. But it looks like things have changed enough that the original discussion no longer applies. Should I be able to do something like this?

_TCP_Send($hClient, ProcessList())

And on the other side, in the associate $TCP_RECEIVE event, deal with the variable (2nd parameter) as an array?

$sReceived[0][0]

Thanks,

aisc

Link to comment
Share on other sites

Did you run that straight trough an online translator?

No, you can't pass arrays to _TCP_Send.

When you're busting on someone's grammar, you might want to make sure you spell the word "through" correctly. :mellow:

And thanks.

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...