Jump to content
Sign in to follow this  
matwachich

[Class] Simple TCP with OOP

Recommended Posts

one question,

can u see any problem if the "client" part is running and if the user is issueing a reboot or shutdown, does the client understand that tcpip and stuff is closeing down on him?

Sorry for me crappy english and programming skills :D

what i want to know is if the client part would generate an error or if it just unload nicley?

Share this post


Link to post
Share on other sites

If you disconnect the electrical alimentation of your computer, of cours it will not close cleanely, but the only problem is that: it's possible (and i say possible) that the server will not be notified of the disconnection of the client.

If the computer runing the client is shuted down cleanly, then every thing will be fine, the client will free all ressources and shutdown normally, and the server will be notified of the disconnection

Ciao!

Share this post


Link to post
Share on other sites

ok thx, i notice that some clients (older hardware) do generate a windows error like (memory could not be written error) CH-Agent.exe (witch is my client) but only on reboot or shutdown and only sometimes. this dosent really matter becose its only during that circumstance.

i have no ide how to tackle it sence it only points to a memory adress and its not really importend in this case..

right now i add the row belove to try get the error go away

"C:windowsCH-Agent.exe /StdErrorOut" <<-- is this right? dident really find any dokumentations or example on this?

or should i do something like "C:windowsCH-Agent.exe /StdErrorOut >null"

//Thx Lasse

PS

I think the error could come from a crypted string i send up to a webserver like post, i do this every 2 min and not with ur great code but with winhttp, but this is just a guess.

Edited by lgvlgv

Share this post


Link to post
Share on other sites

Yo.

I'm trying to convert to this UDF. Right now I seem to be missing the include files. The orriginal post in this thread says they're in the zip. I got a .rar rather than a zip for one thing, and the only thing in that .rar are server.AU3 and client.AU3.and a docs folder. Any tips?

Thanks

Share this post


Link to post
Share on other sites

Explaining what it is you're missing would be a great way to start.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

The scripts want to include <persoAutoitObject.au3>, which is not part of the download package. I've briefly looked at this but unsure how to make it work without this file, as when you try to run the examples provided, they fail when referencing <persoAutoitObject.au3>.

Share this post


Link to post
Share on other sites

AutoItObject can be found here.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2020-10-10 - Version 1.5.2.1) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2020-06-30 - Version 1.6.2.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (NEW 2020-06-27 - Version 1.3.2.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

Share this post


Link to post
Share on other sites

Thanks.

I've gotten hold of the Verso autoit object files needed and have constructed a server. Now I am working on the client, and I'm stumped. All I can think of to do is to give the basic idea and see if anyone can jog my brain. From my point of view it kind of looks like I can't use this client but I'm sure I can once I know how to start.

Basically the client will load the port and IP from an .ini when it is started. It should try to connect using that immediately on load. If the connection fails or the user supplies a different IP or port that makes the current connection invalid it needs to try to connect using the new settings. It should always try to reconnect if the connection is lost or if the attempt to connect fails. The outcome is that the client should always be connected when it is running unless the server isn't available or there is a connection problem or the IP or port is typed wrong etc.

The version I had before I tried this new one here would disconnect the client when the user started to edit the IP address field which was always on screen even while connected and would try to reconnect again when it decided the user had typed a complete IPv4 address. This worked pretty nicely but there could be issues like the user may have been going to put two more digits on the IP address when the loop came around and decided the data in the input field was an IPv4 address, which it may have been. Think 184.84.84.1 , could have been .184 on the end. That and the user could not try a host name. I will be dropping this idea but I do want the client to do a great job at maintaining it's connection indefinitely and if the connection fails I want it to paciently lounge around and make periodic attempts to reestablish the connection indefinitely.

Can I have some help with how to do this with the object? I'm stumped on how and where to construct the class, and how to modify it and handle things properly when the configuration is changed. I also plan to support use of different passwords, but I hope that if I can get some help with what I've asked about I'll be able to figure the rest out.

I can run a function in my main loop that would make it connect, but the class is constructed with the port and IP specified. How to try next time using the new configuration? The mini client just tries to connect once and if that fails it closes. That's definitely not what I need to do. My client will sit with certain controls grayed out while it makes attempts to connect.

I'll shut up now because I'm afraid if I try to say anything else I'll start to confuse myself. I could add in snipets from my main code but I haven't really done anything with it yet because I haven't an idea how to even start so there is nothing to show.

Thank you all very much, especially the one who wrote this class.

Share this post


Link to post
Share on other sites

i will propose a patch to both your TCP project both with and without OOP

Current Version:

; Check buffer ok
$tmp = __TCPClient_CheckBuffer($iClient)
If IsArray($tmp) Then
For $elem In $tmp
If $__TCPClient_Clients[$iClient][$__TCPc_CB_RECV] Then _
     Call($__TCPClient_Clients[$iClient][$__TCPc_CB_RECV], $iClient, $elem)
Next
$__TCPClient_Clients[$iClient][$__TCPc_BUFFER] = ""  ;<-----------------------------------------------------
; ---
EndIf

Proposed:

; Check buffer ok
$tmp = __TCPClient_CheckBuffer($iClient)
If IsArray($tmp) Then
$__TCPClient_Clients[$iClient][$__TCPc_BUFFER] = "" ;<-----------------------------------------------------
For $elem In $tmp
     If $__TCPClient_Clients[$iClient][$__TCPc_CB_RECV] Then _
Call($__TCPClient_Clients[$iClient][$__TCPc_CB_RECV], $iClient, $elem)
Next
; ---
EndIf

if ever the user who use that UDF call process within the Callback _TCP_Recv() he start receiving duplicate data

That patch should be apply to "timeout check" also, saving the buffer into a temp val and doing the binary count with that temp val, delete the real buffer data before the callback happen :)

Thanks you very much i appreciate that UDF very much :)


GreenseedMCSE+I, CCNA, A+Canada, QuebecMake Love Around You.

Share this post


Link to post
Share on other sites

Anybody got an example client I can look at that is able to change connection details and reconnect while the application is running? That's what I was talking about in my last pot. I cant figure out how to connect with new details after the client is already running. The example mini client I got with this just connects on startup and if it fails to connect closes.

Share this post


Link to post
Share on other sites

I have been using this tcp server client script for a year now and I haven't had any problem.

everything runs fine.

the problem I found is that it seems to convert foreign characters to question marks "?". when I try to copy some arabic characters from the character map and send them, I always receive a string of question marks.

how to fix this issue? I need to use this script with asian characters.

Share this post


Link to post
Share on other sites

Something must have changed in latest AutoIT, non of my old scripts work, only get Connection lost right away after connect?

The compiled work but not if i try to make a new, not even the examples included work ?

Anyone knows why?

Share this post


Link to post
Share on other sites

Here is the fix.

In server.au3 search for:

$sRecv = TCPRecv($__TCPServer_Sockets[$i][0], $oSelf.max_recv_len)
If @error Then

And replace it with:

$sRecv = TCPRecv($__TCPServer_Sockets[$i][0], $oSelf.max_recv_len)
If @error < -1 Then

In client.au3 search for:

Local $recv = TCPRecv($oSelf.socket, $oSelf.max_recv_len)
;ConsoleWrite($recv & " - @error = " & @error & " - @extended = " & @extended & @CRLF)
If @error Then ; Disconnected

And replace it with:

Local $recv = TCPRecv($oSelf.socket, $oSelf.max_recv_len)
;ConsoleWrite($recv & " - @error = " & @error & " - @extended = " & @extended & @CRLF)
If @error < -1 Then ; Disconnected

For more information check this out:

'?do=embed' frameborder='0' data-embedContent>>

Share this post


Link to post
Share on other sites

Here is the fix.

In server.au3 search for:

$sRecv = TCPRecv($__TCPServer_Sockets[$i][0], $oSelf.max_recv_len)
If @error Then

And replace it with:

$sRecv = TCPRecv($__TCPServer_Sockets[$i][0], $oSelf.max_recv_len)
If @error < -1 Then

In client.au3 search for:

Local $recv = TCPRecv($oSelf.socket, $oSelf.max_recv_len)
;ConsoleWrite($recv & " - @error = " & @error & " - @extended = " & @extended & @CRLF)
If @error Then ; Disconnected

And replace it with:

Local $recv = TCPRecv($oSelf.socket, $oSelf.max_recv_len)
;ConsoleWrite($recv & " - @error = " & @error & " - @extended = " & @extended & @CRLF)
If @error < -1 Then ; Disconnected

For more information check this out:

'?do=embed' frameborder='0' data-embedContent>>

 Thx :)  will try  it as soon as i get back from Thailand :)

Share this post


Link to post
Share on other sites

Here is the fix.

In server.au3 search for:

$sRecv = TCPRecv($__TCPServer_Sockets[$i][0], $oSelf.max_recv_len)
If @error Then

And replace it with:

$sRecv = TCPRecv($__TCPServer_Sockets[$i][0], $oSelf.max_recv_len)
If @error < -1 Then

In client.au3 search for:

Local $recv = TCPRecv($oSelf.socket, $oSelf.max_recv_len)
;ConsoleWrite($recv & " - @error = " & @error & " - @extended = " & @extended & @CRLF)
If @error Then ; Disconnected

And replace it with:

Local $recv = TCPRecv($oSelf.socket, $oSelf.max_recv_len)
;ConsoleWrite($recv & " - @error = " & @error & " - @extended = " & @extended & @CRLF)
If @error < -1 Then ; Disconnected

For more information check this out:

'?do=embed' frameborder='0' data-embedContent>>

 

Seems to work for the connection, but the disconnect dosent work, guess the auto disconnect also have to be modifyed.

//Lasse

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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By tarretarretarre
      Version 2.x.x and 3.x.x has been moved to branch 3.x
      About Autoit-Socket-IO
      Autoit-Socket-IO is a event driven TCP/IP wrapper heavily inspired from Socket.IO with focus on user friendliness and long term sustainability.
      I constantly want to make this UDF faster and better, so if you have any suggestions or questions (beginner and advanced) Do not hesitate to ask them, I will gladly help!
      Key features
      Simple API 99% data-type serialization thanks to Autoit-Serialize Can easily be extended with your own functionality thanks to Autoit-Events "Educational" examples Data encryption thanks to _<Crypt.au3> Limitations
      Speed. This UDF will sacrifice some speed for convenience Getting started
      Download the script from AutoIt or pull it from the official github repo git@github.com:tarreislam/Autoit-Socket-IO.git and checkout the tag 4.0.0-beta Check out the documentaion Take a look in the examples/ folder Changelog
      To see changes from 3.x.x and 2.x.x please checkout the 3.x branch
      Version 4.0.0-beta (This update break scripts.)
      Code base fully rewritten with Autoit-Events and decoupled to improve code quality and reduce bloat. The new UDF is very different from 3.x.x so please checkout the UPGRADE guide to fully understand all changes Added new documentation documentaion Success stories
      Since December 2017-now I have used version 1.5.0 in an production environment for 150+ clients with great success, the only downtime is planned windows updates and power outages.
       
      Newest version (2020-09-15!)

       
      Older versions (Not supported anymore)
      Autoit-Socket-IO-1.0.0.zip Autoit-Socket-IO-1.1.0.zip Autoit-Socket-IO-1.3.0.zip Autoit-Socket-IO-1.4.0.zip Autoit-Socket-IO-1.5.0.zip
      Autoit-Socket-IO-2.0.0.zip
    • By tarretarretarre
      Version 2.x.x and 3.x.x has been moved to branch 3.x
      About Autoit-Socket-IO
      Autoit-Socket-IO is a event driven TCP/IP wrapper heavily inspired from Socket.IO with focus on user friendliness and long term sustainability.
      I constantly want to make this UDF faster and better, so if you have any suggestions or questions (beginner and advanced) Do not hesitate to ask them, I will gladly help!
      Key features
      Simple API 99% data-type serialization thanks to Autoit-Serialize Can easily be extended with your own functionality thanks to Autoit-Events "Educational" examples Data encryption thanks to _<Crypt.au3> Limitations
      Speed. This UDF will sacrifice some speed for convenience Read more in the official thread
    • By TheXman
      Purpose (from Microsoft's website)
      The HTTP Server API enables applications to communicate over HTTP without using Microsoft Internet Information Server (IIS). Applications can register to receive HTTP requests for particular URLs, receive HTTP requests, and send HTTP responses. The HTTP Server API includes SSL support so that applications can exchange data over secure HTTP connections without IIS.
      Description
      There have been several times in the past that I wanted to either retrieve information from one of my PCs or execute commands on one of my PCs, whether it was from across the world or sitting on my couch.  Since AutoIt is one of my favorite tools for automating just about anything on my PC, I looked for ways to make to make it happen.  Setting up a full blown IIS server seemed like overkill so I looked for lighter weight solutions.  I though about creating my own AutoIt UDP or TCP server but that just wasn't robust enough,  Then I found Microsoft's HTTP Server API and it looked very promising.  After doing a little research into the APIs, I found that it was flexible & robust enough to handle just about any of the tasks that I required now and in the future.  So a while back I decided to wrap the API functionality that I needed into an AutoIt UDF file to allow me to easily create the functionality I needed at the time.  It has come in very handy over the years.  Of course it wasn't all wrapped up with a nice little bow like it is now.  That only happened when I decided to share it with anyone else who could use it or learn from it.
      The example file that I included is a very granular example of the steps required to get a lightweight HTTP Server up and listening for GET requests.  The UDF is a wrapper for the Microsoft APIs.  That means to do anything over and above what I show in the example, one would probably have to have at least a general knowledge of APIs or the ability to figure out which APIs/functions to use, what structures and data is needed to be passed to them, and in what order.  However, the UDF gives a very solid foundation on which to build upon.  Of course, if anyone has questions about the UDF or how to implement any particular functionality, I would probably help to the extent that I could or point you in the right direction so that you can figure out how to implement your own solution.
      The APIs included in the UDF are the ones that I needed in the past to do what I needed to do.  If any additional APIs need to be added to the UDF file, please make those suggestions in the related forum topic.
      Being that this is basically an AutoIt wrapper for the Microsoft API functions, there's no need to create AutoIt-specific documentation.  All of the UDF functions, structures, constants, and enumerations are named after their Microsoft API counterparts.  Therefore, you can refer to Microsoft's extensive documentation of their HTTP Server API.  As stated earlier, if there is one or more APIs that you find yourself needing for your particular solution, please suggest it in the related Example Scripts forum topic.
      Related Links
      Microsoft HTTP Server API - Start Page
      Microsoft HTTP Server API - API v2 Reference
      Microsoft HTTP Server API - Programming Model
    • By YogendraAtluri
      Hi, 
      I am new to AutoIT scripting and I am still learning. I am trying to communicate with a Labview application that acts like a server. it basically takes commands from the client. But for some commands, it also send back some data. 
      When i am sending commands from my script, i can see that the labview is getting them. But i am not able to get anything back. I tried different code pieces that are available online in the forum.
      This is the working piece of code which i been using to send data.
      #cs This module is used to establish tcp connection with lab view #ce #include <File.au3> Func SendCmd($cmd) TCPStartup() Local $IpAddress="192.168.10.101" Local $Port="5353" $Labview = TCPConnect($IpAddress,$Port) If @error Then ConsoleWrite('!--> TCPConnect error number ( ' & @error & ' ).' & @CRLF) TCPCloseSocket($Labview) TCPShutdown() Exit EndIf TCPSend($Labview, $cmd & @CRLF) TCPCloseSocket($Labview) TCPShutdown() EndFunc SendCmd("wt42d")  
      This is slightly modified code to send and receive data, which is not working. I am not getting any response back
      SendCmd("galil") Func SendCmd($cmd) TCPStartup() Local $IpAddress="192.168.10.101" Local $Port="5353" $Labview = TCPConnect($IpAddress,$Port) If @error Then ConsoleWrite('!--> TCPConnect error number ( ' & @error & ' ).' & @CRLF) TCPCloseSocket($Labview) TCPShutdown() Exit EndIf TCPSend($Labview, $cmd & @CRLF) $ip = @IPAddress1 ;create listening socket $Listensocket = TCPListen($ip, $Port) ConsoleWrite("Listening to Socket - " & $Listensocket & @CRLF) If $Listensocket = -1 Then ConsoleWrite("Exiting..." & @CRLF) Exit EndIf ;Accept incoming clients and recieve info While 1 $connectedsocket = TCPAccept($Listensocket) ConsoleWrite("Connecting to Socket - " & $connectedsocket & "Error -" & @error & @CRLF) If $ConnectedSocket >= 0 Then $ip2 = TCPRecv($connectedsocket,1000000) EndIf WEnd TCPCloseSocket($connectedsocket) TCPCloseSocket($Labview) TCPShutdown() EndFunc I am not getting anything back. I am getting the following output in the console
      +>Setting Hotkeys...--> Press Ctrl+Alt+Break to Restart or Ctrl+BREAK to Stop. Listening to Socket - 544 Connecting to Socket - -1Error -0 Connecting to Socket - -1Error -0 Connecting to Socket - -1Error -0 Connecting to Socket - -1Error -0 Connecting to Socket - -1Error -0 Connecting to Socket - -1Error -0 Connecting to Socket - -1Error -0 Connecting to Socket - -1Error -0 Connecting to Socket - -1Error -0 its going through that loop forever. i need to force stop it.
      But when i open putty and send the same command, i am getting response right away. 
      Can someone please help me with that.
      Thanks in advance
      Regards
      Yogendra
    • By Colduction
      Hello again to all, I've searched many times in forum and help file to find AES-256-GCM Algorithm for Crypt\Decrypt strings and files but i did not find anything about GCM Algorithm.

      Do you have any idea for solving this issue?
×
×
  • Create New...