Jump to content
matwachich

[Class] Simple TCP with OOP

Recommended Posts

matwachich

Hi everybody!

Here is my new super tool!

It's an AutoItObject TCP Class! Very simple to use, with example and complet decumentation (Made with NaturalDocs)

There are two classes: Client, and Server.

- The data are encrypted!

- The server can handle multiple clients

- Data buffering, if you send a big amount of data, it will received as it was sent, not as many little parts of it

- OOP programing! (very simple!)

PS: You don't need to download AutoItObject, everything you need is in the zip.

Update: 03/11/2011

+: Added, -: Deleted/Deprecated, *: Modified, !: Bug corrected
=== 1.1c === (30/10/2011)
Server:
+: ClientGetBufferLen method
+: Completed doc of ClientPropertyGet and ClientPropertySet methods (Return values)
*: Now, when calling .Shutdown, the Callback_LostClient is called for each client.
!: Corrected bug: Script error when calling .DisconnectAll befor calling at least one
    time .Startup (Main socket array was not dimensioned)

UDF_TCPClass.rar

Edited by matwachich
  • Like 1

Share this post


Link to post
Share on other sites
lgvlgv

this is fantastic :graduated:

i love it!!!!!!

it works like a charm.

now i just need to figure out how to send and recive files with this, i very impressed how stable it is.

Edited by lgvlgv

Share this post


Link to post
Share on other sites
Beege

matwachich nice job! :graduated: Im surprised you havent got more comments. Very good use of autoitobject. And documentation is cool too. I like the html. One thing about the examples, you should add a check for window close in your loop. Since you have the tray icon hidden I dont know how to get out of it.

edit: and maybe add the examples to your zip so the user dosent have to copy and create from the help file to test it.

Edited by Beege

Share this post


Link to post
Share on other sites
happytc

Hi everybody!

Here is my new super tool!

It's an AutoItObject TCP Class! Very simple to use, with example and complet decumentation (Made with NaturalDocs)

There are two classes: Client, and Server.

- The data are encrypted!

- The server can handle multiple clients

- Data buffering, if you send a big amount of data, it will received as it was sent, not as many little parts of it

- OOP programing! (very simple!)

PS: You don't need to download AutoItObject, everything you need is in the zip.

Here is the zip, enjoy!

So good work!

One question: how do you compile AutoItObject.au3 to DLL file?

(In the Function of _File_AutoItObject_dll(), how do you get $string?)

Share this post


Link to post
Share on other sites
Beege

One question: how do you compile AutoItObject.au3 to DLL file?

(In the Function of _File_AutoItObject_dll(), how do you get $string?)

You dont. The Autoitobject UDF automatically extracts the dll for use at runtime.

Share this post


Link to post
Share on other sites
matwachich

Update 1.1b: Bug correction and new examples

Change log:

=== 1.1b === (30/09/2011)
Server:
!: Corrected bug in Number of client's extended properties
*: Documentation corrected (added 0-based for ClientPropertyGet/Set)
+: New Method: NbrClients
*: Property NbrClients is now private
Client:
!: Changed return value of Connect method (see documentation)

PS: If you want to send a file, just read it's content with binary mode (flag 16 in FileOpen) and send it! The receiver will receive binary data, and all you have to do is write it on a file with binary mode too (flag 18 in FileOpen)

For archive download, see first post

Share this post


Link to post
Share on other sites
lgvlgv

Update 1.1b: Bug correction and new examples

Change log:

=== 1.1b === (30/09/2011)
Server:
!: Corrected bug in Number of client's extended properties
*: Documentation corrected (added 0-based for ClientPropertyGet/Set)
+: New Method: NbrClients
*: Property NbrClients is now private
Client:
!: Changed return value of Connect method (see documentation)

PS: If you want to send a file, just read it's content with binary mode (flag 16 in FileOpen) and send it! The receiver will receive binary data, and all you have to do is write it on a file with binary mode too (flag 18 in FileOpen)

For archive download, see first post

i will try it a.s.a.p.

PS.

I love ur work :graduated:

DS

Share this post


Link to post
Share on other sites
lgvlgv

I love the new isconnected, was missing that. will try that to.

Share this post


Link to post
Share on other sites
lgvlgv

i get one error every time i start the app i maid on client side, think it has nothing to do with ur scripts and i think i allways had it.

Yet everything works as it should and cant see any errors in anything else then exept when i start my client (think i implemented ur code a litte wrong?)

Do u have any idea where i should start to look?

in ur client.au3, line 6 is

Global $__TCPClass_oMyErr = ObjEvent("AutoIt.Error", "_TCPClass_ErrFunc")

when i do this.

ConsoleWrite("test")

#include "Client.au3"

ConsoleWrite("test1")

i can see it must be the client.au3

>Running:(3.3.6.1):C:\Program\AutoIt3\autoit3.exe "H:\Jobb\scripts\Dev\ChargeDeamon\utveckling\Ny\CH-Agent\CH-Agent.au3" -d  
testA COM Error has occured!
err.description is:
err.windescription:
err.number is:  00000002
err.lastdllerror is:  0
err.scriptline is:  6
err.source is:
err.helpfile is:
err.helpcontext is:
test1@@(266) :(42:37)Updateing client settnigs...
@@ (500) :(42:37) _Getbiosinfo()
@@(266) :(42:37)Updateing webserver...
Edited by lgvlgv

Share this post


Link to post
Share on other sites
lgvlgv

Update 1.1b: Bug correction and new examples Change log:

=== 1.1b === (30/09/2011) Server: !: Corrected bug in Number of client's extended properties *: Documentation corrected (added 0-based for ClientPropertyGet/Set) +: New Method: NbrClients *: Property NbrClients is now private Client: !: Changed return value of Connect method (see documentation)
PS: If you want to send a file, just read it's content with binary mode (flag 16 in FileOpen) and send it! The receiver will receive binary data, and all you have to do is write it on a file with binary mode too (flag 18 in FileOpen) For archive download, see first post

What im i doing wrong?

server sends:
Case $ButtonViewDesktop
Local $sock1 = _GUICtrlListView_GetItemTextArray(GUICtrlGetHandle($ListViewClients)) ;gets the socket
SendPacketToServer($sock1[7], "~~GetScreenShot")

client do:
Case $data[0] = "~~GetScreenShot"
SetClientStatus(1, "Sending Screenshot")
_ScreenCapture_SetJPGQuality(100)
_ScreenCapture_Capture(@DesktopDir & "\tempscreen1.jpg")
sleep(1000)
local $screenfile = FileOpen(@DesktopDir & "\tempscreen1.jpg", 16)
$oClient.Send("~~SendingScreenshot|" & $screenfile)
sERVER RECIVES:
Case $data[0] = "~~SendingScreenshot"
;_ArrayDisplay($data)
local $screenshotfile = FileOpen(@DesktopDir & "\SCREEN.JPG", 18)
Filewrite($screenshotfile, $data[1])
FileClose($screenshotfile)

Share this post


Link to post
Share on other sites
matwachich

Try like this

Filewrite($screenshotfile, Binary($data[1]))

Or, try to not mix between String data and binary data here: $oClient.Send("~~SendingScreenshot|" & $screenfile)

What i do is, to send a small command to the server to tell him that the next received data will be a screen shot, and just after that, i send ONLY the screen shot binary data, and it works for me.

About the com error, i really don't know, don't have it.

Share this post


Link to post
Share on other sites
lgvlgv

Try like this

Filewrite($screenshotfile, Binary($data[1]))

Or, try to not mix between String data and binary data here: $oClient.Send("~~SendingScreenshot|" & $screenfile)

What i do is, to send a small command to the server to tell him that the next received data will be a screen shot, and just after that, i send ONLY the screen shot binary data, and it works for me.

About the com error, i really don't know, don't have it.

Ive got it working :graduated: thx alot.

had 2 stupid errors in the script one was that i only read 4096 of the file no matter how big it was ;) (cut and past from another guys script)

One last question!

there are another threed here on the forums about running "ur own" scripts as a service do u know if its possible to merge ur example with there exampels?

i nuderstand this is not ur table, but beeing a guru and all u probbebly allready have the answer. sorrry for my bad swenglish (im swedish ;) )

any of the example would do :)

Edited by lgvlgv

Share this post


Link to post
Share on other sites
matwachich

#cs ----------------------------------------------------------------------------
AutoIt Version: 3.3.6.1
Author:      Matwachich
Script Function:
#ce ----------------------------------------------------------------------------
#include <Services.au3>
Global $srvName = "custom_service", $srvDesc = "My Custom Au3 Service"
If Not _Service_Exists($srvName) Then
; This creates your service in windows service manager, it hase to be done
; only one time.
_Service_Create($srvName, $srvDesc, _
     $SERVICE_WIN32_OWN_PROCESS + $SERVICE_INTERACTIVE_PROCESS, _
     $SERVICE_AUTO_START, _
     $SERVICE_ERROR_IGNORE, _
     '"' & @ScriptFullPath & '"')
EndIf
; This starts your service, calling the _Svc_Main function
_Service_init($srvName)
; ##############################################################
Func _Svc_Main()
; Now, all your code goes here!
EndFunc

Share this post


Link to post
Share on other sites
lgvlgv

#cs ----------------------------------------------------------------------------
AutoIt Version: 3.3.6.1
Author:      Matwachich
Script Function:
#ce ----------------------------------------------------------------------------
#include <Services.au3>
Global $srvName = "custom_service", $srvDesc = "My Custom Au3 Service"
If Not _Service_Exists($srvName) Then
; This creates your service in windows service manager, it hase to be done
; only one time.
_Service_Create($srvName, $srvDesc, _
     $SERVICE_WIN32_OWN_PROCESS + $SERVICE_INTERACTIVE_PROCESS, _
     $SERVICE_AUTO_START, _
     $SERVICE_ERROR_IGNORE, _
     '"' & @ScriptFullPath & '"')
EndIf
; This starts your service, calling the _Svc_Main function
_Service_init($srvName)
; ##############################################################
Func _Svc_Main()
; Now, all your code goes here!
EndFunc

ahh, its under _Svc_main i should put it :graduated: tryed to put it everywere else ;)

i will try it as soon as i get home :)

//Thx alot

Share this post


Link to post
Share on other sites
matwachich

Update. See first post

Share this post


Link to post
Share on other sites
lgvlgv

im trying it right now, thx.

Share this post


Link to post
Share on other sites
davidkim

question...

o_server.au3 add code

#region ### START Koda GUI section ###

$GUI_Main = GUICreate("Mini-Server", 1408, 228)

$tab = GUICtrlCreateTab(6, 6, 1297, 217)

$bsen = GUICtrlCreateButton('bsend',1300,6,100,25)

GUISetState(@SW_SHOW)

#endregion ### END Koda GUI section ###

---------->

While 1

$nMsg = GUIGetMsg()

Switch $nMsg

Case $GUI_EVENT_CLOSE

Exit

Case $bsen

Local $Sockets = $oSrv.SocketIDList()

If $oSrv.Broadcast($Sockets, 'bsend') Then

MsgBox(0,'','all')

EndIf

EndSwitch

; ---

$oSrv.Process()

__Process($nMsg)

WEnd

is not send...msg..help me....

Edited by davidkim

I interest am many quite in AutoitScript.From that is [http://cafe.naver.com/autoitscript[/color]] Korea of cafe(blog) to be operating, [size="2"][color="#ff00ff"]English cannot well[/size].Many help it requests.To read, it stands it thanks.

Share this post


Link to post
Share on other sites
davidkim

While 1

$nMsg = GUIGetMsg()

Switch $nMsg

Case $GUI_EVENT_CLOSE

Exit

Case $bsen

Local $Sockets = $oSrv.SocketIDList()

If $oSrv.Broadcast($Sockets, 'bsend') Then ; is not send...

MsgBox(0,'','all')

EndIf

;/// is var enable....

_ArrayDisplay($__TCPServer_Sockets)

For $i = 0 To $oSrv.max_clients - 1

If $__TCPServer_Sockets[$i][0] <> -1 Then TCPSend($__TCPServer_Sockets[$i][0], 'bsend')

;// is send not.......

Next

help me...

EndSwitch

; ---

$oSrv.Process()

__Process($nMsg)

WEnd


I interest am many quite in AutoitScript.From that is [http://cafe.naver.com/autoitscript[/color]] Korea of cafe(blog) to be operating, [size="2"][color="#ff00ff"]English cannot well[/size].Many help it requests.To read, it stands it thanks.

Share this post


Link to post
Share on other sites
matwachich

Your problem is that you have not well read the documentation of the broadcast method. It takes only one parameter: The data to be sent.

Why do you pass it the socketID list???!!!

#region ### START Koda GUI section ###
$GUI_Main = GUICreate("Mini-Server", 1408, 228)
$tab = GUICtrlCreateTab(6, 6, 1297, 217)
$bsen = GUICtrlCreateButton('bsend', 1300, 6, 100, 25)
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit
  Case $bsen
   If $oSrv.Broadcast('bsend') Then
    MsgBox(0, '', 'all')
   EndIf
EndSwitch
; ---
$oSrv.Process()
__Process($nMsg)
WEnd

Share this post


Link to post
Share on other sites
lgvlgv

I now run 1 server speaking <> with 60+ clients no problem or error to report, working great.

Thx for this great job.

Share this post


Link to post
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

  • Similar Content

    • ripdad
      By ripdad
      I have had several people ask for this, so I decided to work the algorithm for it and this is the result.
      What is it?
      A Gateway Proxy Sends and Receives Data Unmodified.
      https://en.wikipedia.org/wiki/Proxy_server
      What is it used for?
      You can use it as a gateway, relay or router between two known static IP addresses.
      More information is in the header of the script.
      Download: LocalGatewayProxy.au3
      You will need WSA_NBTCP.au3 from here:
      https://www.autoitscript.com/forum/topic/191954-wsa_nbtcp-v100-udf/
      If you have any questions or problems, let me know.
       
    • ripdad
      By ripdad

      This script is based on algorithm code from EnrMa.
      Updated: January 22, 2018
      Made improvements. Changes are in the script header.
      Known Issues: AutoIt x64 does not work properly with this script.
      Download: LocalProxyServer_v1.00.zip
       
    • ripdad
      By ripdad
      WSA_NBTCP.au3  (Windows Sockets API - Non-Blocking Transmission Control Protocol)
      Version: 1.00
      Type: UDF
      This is an accumulation of WSA code from many sources and modified to suit myself.
      These functions have been thoroughly tested using a Local Proxy Server, which
      is about the most strenuous test you can use.
      Includes my rendition of how a TCPRecv Timeout should work. Also includes a
      timewait/timeout using Select for TCP Send, which works great for that function.
      You will need a loop to use _WSA_TCPRecv(). An example will be forthcoming in a second post.
      Functions:
      #CURRENT_USER_FUNCTIONS
      _WSA_Cleanup
      _WSA_FormatMessage
      _WSA_GetLastError
      _WSA_TCPAccept
      _WSA_TCPCloseSocket
      _WSA_TCPConnect
      _WSA_TCPListen
      _WSA_TCPRecv
      _WSA_TCPSend

      #INTERNAL_FUNCTIONS
      __TimeoutManager
      __TimeoutReset

      #EXTRA_FUNCTIONS
      _WSA_GetAddrInfo
      _WSA_GetHostByAddr
      _WSAAsyncGetHostByName
      _WSAAsyncGetHostByName_Callback
      _WSA_GetNameInfo
       
      Requirements:
      - AutoIt Versions: 3.3.8.1 thru 3.3.15.0 (32Bit only).
      - TCPStartup() at beginning of script on startup.
      - TCPShutDown() and _WSA_Cleanup() on exit.
      Download UDF: WSA_NBTCP.au3
       
    • tarretarretarre
      By tarretarretarre
      AutoIt-SocketIo
      Yep yep, this is pretty much an attempt to port the existing project's concept https://socket.io/ to AutoIt's Codebase. So i will not go in to so much detail.
      This is how the communication is done http://i.imgur.com/0mMfsBD.png Each client is isolated to the server http://i.imgur.com/rVO2LFb.png Features
      Easy API VarType Translation (Example: If the server sends an int, the client will receive an int and vice versa) Fully featured examples Data encryption (Using Autoit's UDF Crypt.au3) Limitations / Drawbacks
      It is not possible to Broadcast/Emit objects Only 1D-arrays are allowed to be Broadcasted/Emitted (2D arrays will probably never be supported) Changelog
      Version 1.5.0 (This update DOES NOT break scripts)
      Added AutoIt like docs under Docs\. The docs are generated so its a 1 to 1 reflection of the UDF headers. Print of documentation Added a production ready server example. Added a new method: _Io_DevDebug which will display useful information when debugging. Added a new method: _Io_SetMaxRecvPackageSize which defaults to whatever _Io_setRecvPackageSize is set to. Added a new method: _Io_setOnPrefix which defaults to _On_ Added a new default client & server event called flood. Flood occurs when exceeding the $__g_io_nMaxPacketSize. $__g_io_nMaxPacketSize is set by _Io_SetMaxRecvPackageSize Fixed the 16 parameter limit when sending data with _Io_Emit, _Io_Broadcast, _Io_BroadcastToAll and _Io_BroadcastToRoom. This works on the same premise that AutoIt's Call Does Fixed a TRUNCATION problem when receiving packages which could cause crashes! Fixed a programming error which caused $__g_ionPacketSize to reset to default 4096 if _Io_Connect or _Io_listenwere called after _Io_setRecvPackageSize Fixed _Io_setEventPreScript and _Io_setEventPostScript They didnt work. Lol. Changed how events are fired so the client cannot crash the server by sending the wrong number of parameters (This also allows for optional parameters on callbacks) Changed _Io_On. The second parameter $fCallback can now be set to null. Doing this, the function assumes that the callback is: _On_<eventName>.  
      Changelog History
      Api methods
      Please see the docs provided
      Default events
      Server events
      connection Client events
      banned Server and Client events
      disconnect flood View source on github
       
      Autoit-Socket-IO-1.0.0.zip (OLD!)
      Autoit-Socket-IO-1.1.0.zip (OLD)
      Autoit-Socket-IO-1.3.0.zip (OLD)
      Autoit-Socket-IO-1.4.0.zip (OLD)
      Autoit-Socket-IO-1.5.0.zip (NEWEST 2017-12-20)
    • timmalos
      By timmalos
      Hello all.
      In case this is interesting few of you, I share my AMCP 2.1 protocol UDF in AutoIT. This protocol is used by CasparCG server, which is a Windows and Linux software used to play out professional graphics, audio and video to multiple outputs as a layerbased real-time compositor. It has been in 24/7 broadcast production since 2006. It's free and opensource.
      The UDF I share allows communication between an AutoIt based client and the CasparCG, based on following documentation : http://casparcg.com/wiki/CasparCG_2.1_AMCP_Protocol
      If you want more details on CasparCG :  official WebSite or have a look to this video
      I'm currently building a full Client based on AutoIt, with many features like drag-and-drop layers, but sadly I can't share it right now, might come later. Don't hesitate to ask questions if you have any or need a basic example.
      The only requirement for this UDF is the other Event-driven TCP UDF by Kip
       
       

      AMCP_shared.au3
      TCP.au3
×