Jump to content

Recommended Posts

Posted (edited)

Kips TCP UDF

I'm a bit of a newbie to autoit, I've had a go at modifying the example script which is given and I'm trying to get a function to TCP_Send from the Server to the Client but cant seem to get the Function call when you press a button (I've tried many different combos) I'm sure its something simple I'm overlooking. The whole custom params thing using a UDF confuses me if theres any good tutorial/read up please share!

Heres the Server script:

#include <TCP.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
  
GUICreate("Network Test - Server", 500, 200, -1, -1)
$GUI_Messages = GUICtrlCreateEdit("Server Starting...", 120, 10, 370, 180, $ES_AUTOVSCROLL + $WS_VSCROLL + $ES_READONLY)
GUICtrlSetBkColor ($GUI_Messages, 0xffffff)
$GUI_Button_Send1 = GUICtrlCreateButton("Broadcast", 10, 10, 100, 30)
$GUI_Button_Send2 = GUICtrlCreateButton("Clients", 10, 50, 100, 30)
$GUI_Button_Send3 = GUICtrlCreateButton("Send Message", 10, 90, 100, 30)

GUISetState()
  
$hServer = _TCP_Server_Create(1008); A server. Tadaa!
GUICtrlSetData($GUI_Messages, @CRLF & "Server Running" , 1)
_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.
_TCP_RegisterEvent($hServer, $TCP_SEND, "SendMessage")
  
Func NewClient($hSocket, $iError); Yo, check this out! It's a $iError parameter! (In case you didn't noticed: It's in every function)
    GUICtrlSetData($GUI_Messages, @CRLF & "Client Connected", 1)
    _TCP_Send($hSocket, "Bleh!"); Sending: "Bleh!" to the new client. (Yes, that's right: $hSocket is the socket of the new client.)
EndFunc
  
Func Disconnect($hSocket, $iError); Damn, we lost a client. Time of death: @Hour & @Min & @Sec :P
    GUICtrlSetData($GUI_Messages, @CRLF & "Client Disconnected", 1)
EndFunc

Func BroadCast1($hSocket, $iError)
    $clients = _TCP_Server_ClientList()
    GUICtrlSetData($GUI_Messages, $clients, 1)
    MsgBox( 1, "asd", $clients)
EndFunc

Func SendMessage($hSocket, $iError)
    GUICtrlSetData($GUI_Messages, @CRLF & "Sent Message: Jebus", 1)
    _TCP_Send($hSocket, "Bleh!")
    MsgBox( 1, "asd", "Message Sent")
EndFunc

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
        Case $msg = $GUI_Button_Send1
        Case $msg = $GUI_Button_Send2
            _TCP_RegisterEvent($hServer, $TCP_SEND, "SendMessage")
        Case $msg = $GUI_Button_Send3
    EndSelect
WEnd

Client script:

#include <TCP.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
     
GUICreate("Network Test - Client", 500, 200, -1, -1)
$GUI_Messages = GUICtrlCreateEdit("Connecting...", 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()
    $hClient = _TCP_Client_Create(@IPAddress1, 1008); 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.
EndFunc

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
        Case $msg = $GUI_Button_Send1
            Call("Connect")
        Case $msg = $GUI_Button_Send2
    EndSelect
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...
        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)
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

I've also posted on the UDF topic but thought it would be best to post here instead, any help appreciated!

Edited by Valley
Posted (edited)

Made a basic one last night without the UDF, but that doesnt help because it doesnt allow multiple clients :)

Edited by Valley

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
×
×
  • Create New...