Because people always ask how to create a TCP client/server, and then (if they get it) don't know how to make it multi client,
I simplified it.
It's event driven, so you don't have to manualy check everything.
Please read this whole post to understand my UDF.
Version 3 of this UDF:
Functions renamed, and everything is event-driven.
_TCP_Client_Create($sIP , $iPort)
_TCP_RegisterEvent($hSocket, $iEvent, $sFunction)
Use _TCP_RegisterEvent() to register an event. These are the possible events to register:
$TCP_SEND - When the other side of the connection is ready to receive (more) data.
$TCP_RECEIVE - If something is received.
$TCP_CONNECT - When you connect to the server. (Client only)
$TCP_DISCONNECT - When a connection is closed.
$TCP_NEWCLIENT - When a new client connects to the server. (Server only)
$hClient = _TCP_Client_Create(@IPAddress1, 88); Create the client. Which will connect to the local ip address on port 88
_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.
; just to keep the program running
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.
ToolTip("CLIENT: Could not connect. Are you sure the server is running?",10,10); ... we aren't.
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)
Func Disconnected($hSocket, $iError); Our disconnect function. Notice that all functions should have an $iError parameter.
ToolTip("CLIENT: Connection closed or lost.", 10,10)
And the server: (which is smaller than the client)
ToolTip("SERVER: Creating server...",10,30)
$hServer = _TCP_Server_Create(88); A server. Tadaa!
_TCP_RegisterEvent($hServer, $TCP_NEWCLIENT, "NewClient"); Whooooo! Now, this function (NewClient) get's called when a new client connects to the server.
_TCP_RegisterEvent($hServer, $TCP_DISCONNECT, "Disconnect"); And this,... this will get called when a client disconnects.
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. (Yes, that's right: $hSocket is the socket of the new client.)
Func Disconnect($hSocket, $iError); Damn, we lost a client. Time of death: @Hour & @Min & @Sec :P
ToolTip("SERVER: Client disconnected.",10,30); Placing a tooltip right under the tooltips of the client.
Now, as you can see, every function has an $iError parameter.
Every funtion that is called can contain two parameters (1st: Socket, 2nd: Error) except for the Received function.
Which should contain 3 parameters. 1:Socket, 2:Data, 3:Error
You can't create a server and a client in the same script.
Well, I think that's all.
Previous number of downloads: 975