Jump to content

TCP UDF, Event driven!


Kip
 Share

Recommended Posts

Oh, sorry. I was reading your post wrong. You DO can register a disconnect event.

_TCP_RegisterEvent($TCP_DISCONNECT, "Function")

Func Function($iError)

Msgbox(0,"Server or Client","(Client: We lost our connection to the server) or (Server: A client has closed his connection)")

EndFunc

Edited by Kip
Link to comment
Share on other sites

no offence but i think that alek's ideea(to pass a client number instead of the socket) is easyer and better :)

Only two things are infinite, the universe and human stupidity, and i'm not sure about the former -Alber EinsteinPractice makes perfect! but nobody's perfect so why practice at all?http://forum.ambrozie.ro

Link to comment
Share on other sites

Ok, new UDF uploaded. It's not much simpler. I hope you can still understand the new version.

This is really event driven. Using the UDF created by Zatorg.

New function names, so you'll have to rewrite some stuff.

many thanks for you UDF, very usefull.

Unfotunately, i understand sample Server.au3 and Client.au3 but with your new version, there is new name and new function, and i not understand how to use them.

you don't write so much comment in your code ;-)

I'm a bit lost with _TCP_RegisterEvent for example

very big thanks again for your job

Laurent

Link to comment
Share on other sites

Unfotunately, i understand sample Server.au3 and Client.au3 but with your new version, there is new name and new function, and i not understand how to use them.

you don't write so much comment in your code ;-)

I'm a bit lost with _TCP_RegisterEvent for example

very big thanks again for your job

Laurent

I'll try to add comments again.

no offence but i think that alek's ideea(to pass a client number instead of the socket) is easyer and better

I don't see the reason why...?

Edit: 900th post :)

Edited by Kip
Link to comment
Share on other sites

I added comments to the examples.

ok thanks, so i have make a mistake. i have look a example in download file in Pages 3, not in YOUR example in Page 1.

In page1 it's enough clear

one more time, many thanks you save me many days with your code

Link to comment
Share on other sites

yeh this is pretty nice..

But has problems..

Tip:

Why dont you make a real udf.. (Have a look here: http://www.autoitscript.com/autoit3/udfs/UDF_Standards.htm)

Because you have the functions but it is not clear what the exact error or succes returns are.. and more..

it also would be handy to see what every function does... (Thats what a good udf should have..)

And then my qeustion..

I was trying to make a simple chat system..

here is my server:

#include "TCP.au3"
#include <INet.au3>
_TCP_Server_Create(88); A server. Tadaa!

_TCP_RegisterEvent($TCP_NEWCLIENT, "NewClient"); Whooooo! Now, this function (NewClient) get's called when a new client connects to the server.
_TCP_RegisterEvent($TCP_DISCONNECT, "Disconnect"); And this,... this will get called when a client disconnects.
_TCP_RegisterEvent($TCP_RECEIVE, "Received")

While 1
    
WEnd

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


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

And here is my client:

#include "TCP.au3"
#include <INet.au3>
#include <ButtonConstants.au3>
#include <GUIConstants.au3>
#include <EditConstants.au3>
_TCP_Client_Create(88, @IPAddress1); Create the client. Which will connect to the local ip address on port 88
_TCP_RegisterEvent($TCP_RECEIVE, "Received"); Function "Received" will get called when something is received
_TCP_RegisterEvent($TCP_CONNECT, "Connected"); And func "Connected" will get called when the client is connected.
;_TCP_RegisterEvent($TCP_DISCONNECT, "Disconnected"); And "Disconnected" will get called when the server disconnects us, or when the connection is lost.
$username='ludocus'
$Form1 = GUICreate("Begin of chat", 633, 447, 190, 123)
$Button1 = GUICtrlCreateButton("Send", 560, 16, 57, 25, $BS_DEFPUSHBUTTON)
GUICtrlSetFont(-1, 10, 800, 0, "Arial")
$Input1 = GUICtrlCreateInput("", 392, 16, 161, 24)
GUICtrlSetFont(-1, 10, 800, 0, "Arial")
$List1 = GUICtrlCreateList("", 392, 80, 217, 344)
$Label1 = GUICtrlCreateLabel("Users:", 392, 56, 34, 17)
$Edit1 = GUICtrlCreateEdit("", 8, 8, 377, 433)
GUICtrlSetFont(-1, 10, 800, 0, "Arial")
GUISetState(@SW_SHOW)
_TCP_Client_Send($username&' has logged in')
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
            
        Case $Button1
            _TCP_Client_Send(GUICtrlRead($Input1))

    EndSwitch
    
WEnd

Func Connected($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

Func Received($iError, $sReceived)
    If StringLeft($sReceived, 9) = '||| CONN:' Then
        $nString = StringReplace($sReceived, '||| CONN:', '')
        $sReceived = $nString&' has connected..'
    EndIf
    $tkst = GUICtrlRead($Edit1)
    if $tkst = '' then
        GUICtrlSetData($Edit1, $sReceived)
    Else
        GUICtrlSetData($Edit1, $tkst&@CRLF&$sReceived)
    EndIf
EndFunc

Ok you said in your 1st post: No more messing with multiple clients..

Well, Your thingie can only support 1 client.. Or am I making mistakes?

cause when I open multiple windows of my client and 1 server.. It only supports its own client..

(For example: I run the server.. I run the client 2 times.. I send at the first client: Hello Client2. But then I look at the second client and there is no line of text added saying: hello)

Link to comment
Share on other sites

Great job kip i had already made my multi user server for my ORPG but since this is event driven and as such more powerful(im not going with it cause of the simplicity) im going to start using it instead thanks again!

@ludocus

That "MAKE A REAL UDF" statement is kinda true but rude at the same time im sure someone that needs to can figure out very easily what error when't on and as for your code the server is receiving it but not sending to the other client(s) it looks like its sending back to the received client try going through the list of connected clients if its not the one it received from send it to that one so all clients but the one that sent it receive the same thing.

[quote name='PsaltyDS' post='635433' date='Jan 27 2009, 07:04 AM']Larry is a mass murderer?! It's always the quiet, clean cut, bald guys... [/quote]

Link to comment
Share on other sites

Ok so I went on with my script and now I have a sweet chat system and computer control in one..

look:

client.au3

Server.au3

so.. but the thing is it only works at @IPAddress1 so only local..

And when I try using _GetIP() it doesn't work..

does somebody know how to make the server listen to the whole world?

and that the client connects to my ip adress not only @IPAddress1..?

Please I really need help with this..

Link to comment
Share on other sites

Ok so I went on with my script and now I have a sweet chat system and computer control in one..

look:

client.au3

Server.au3

so.. but the thing is it only works at @IPAddress1 so only local..

And when I try using _GetIP() it doesn't work..

does somebody know how to make the server listen to the whole world?

and that the client connects to my ip adress not only @IPAddress1..?

Please I really need help with this..

use 0.0.0.0

[font="Impact"]Never fear, I is here.[/font]

Link to comment
Share on other sites

Alek is correct, but you may also have to fix whatever firewall you have to port forward to the proper computer.

0.0.0.0 is the INADDR_ANY value, which tells the networking code to listen for packets destined for any address that your program happens to get. Using a specific IP will limit what address the packets can be destined to (so, if you use your public IP there, no one on the lan can talk to it, and, as you've seen, if you use your lan IP, no one on the outside can talk to it either).

As far as static vs dynamic IP's, you can use a service like DynDNS, and just point people/clients at your dns name. It's a good thing to do usually any way, for it makes updating your IP a lot easier, and your clients can stay the exact same no matter how many times your IP changes.

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