Sign in to follow this  
Followers 0
Snarg

TCP string functions

9 posts in this topic

#1 ·  Posted (edited)

I have never posted something like this before so please keep the flames to a minimum. I would like to give the vast majority of the credit to Larry. I only came upon this idea by browsing what he has posted. These are a variation of some of the functions he has posted. Any errors in these functions are soley mine and mine alone.

These functions will add a measure of data integrity to TCP. A simple checksum is added to the beginning of every packet sent. This checksum is verified when a packet is received to ensure the whole packet has been received.

;===============================================================================
;
;Description:   Sends data via TCP. It can be any type of non-binary data. A
;                length identifier is added to the beginning of the packet to
;                ensure all the data is present on the receiving end.
;
;Parameter(s):  $vData - Accepts any type of formated data.
;                 $iSocket - The connected socket identifier (SocketID) as returned by TCPConnect.
;
;Requirement(s):A TCP connection.
;
;Author:        Snarg  - Supa(dot)Snarg(at)gmail(dot)com
;                (I would like to give the vast majority of the credit to Larry)
;
;===============================================================================
Func _TCPSendText ($iSocket, $vData)
    
    Local $vBuffer = ""
    $vBuffer = StringLen ($vData) & "," & $vData
    TCPSend ($iSocket, $vBuffer)
    
EndFunc

;===============================================================================
;
;Description:     Recieves a data packet via TCP. It can be any type of non-binary
;                 data. A length identifier is checked at the start of the packet
;                 to ensure all the data has been recieved.
;
;Parameter(s):    $iSocket - The connected socket identifier (SocketID) as returned by TCPAccept or
;                 TCPConnect.
;
;Requirement(s):  A TCP connection.
;
;Return Value:    $vBuffer - Data recieved on the TCP connection.
;
;Author:          Snarg  - Supa(dot)Snarg(at)gmail(dot)com
;                 (I would like to give the vast majority of the credit to Larry)
;
;===============================================================================

Func _TCPReciveText ($iSocket)
    Local $vBuffer = ""
    Local $iBytes = -1
    
    While 1
        $vBuffer &= TCPRecv ($iSocket, 1024)
        If $iBytes = -1 And StringInStr ($vBuffer, ",") Then
            $iBytes = StringLeft ($vBuffer, StringInStr ($vBuffer, ",")-1)
            $vBuffer = StringTrimLeft ($vBuffer, StringInStr ($vBuffer, ","))
        Else
            If StringLen ($vBuffer) = $iBytes Then ExitLoop
        EndIf
    WEnd
    
    Return $vBuffer
EndFuncoÝ÷ ØLZ^±«­¢+ØíáµÁ±½}Q
AM¹QáÐè()¥´ÀÌØíÍ5åYÉ¥±ôÜØ)¥´ÀÌØíÙ5åÑôÅÕ½ÐíQ¡¥Ì¥ÌͽµÑ¸%̡̱ÑÑÉÌ°¹ÕµÉÌ Ä°È°Ì°Ð°Ô°Ø¸¸¸¤°ÍÁ¥°¡ÉÑÉÌÌÌíÀÌØìxµÀ쨹¸±Í¼½¹Ñ¥¸ÙÉ¥±Ì¸Q¡ÙÉ¥±¥ÌôÅÕ½ÐìµÀìÀÌØíÍ5åYÉ¥±()Q
AMÑÉÑUÀ ¤(ÀÌØí¥M½­ÐôQ
A
½¹¹Ð ÄÈܸÀ¸À¸Ä°ÌÌÌ̤)}Q
AM¹QáÐ ÀÌØí¥M½­Ð°ÀÌØíÙ5åѤ)Q
AM¡Õѽݸ ¤((íáµÁ±½}Q
AI¥ÙQáÐè()¥´ÀÌØíÙ5åÑôÅÕ½ÐìÅÕ½Ðì()Q
AMÑÉÑUÀ ¤(ÀÌØí¥1¥ÍѸôQ
A1¥ÍѸ ÄÈܸÀ¸À¸Ä°ÌÌÌ̤(ÀÌØí¥M½­ÐôQ
AÁÐ ÀÌØí¥1¥ÍѸ¤(ÀÌØíÙ5åÑô}Q
AI¥ÙQáÐ ÀÌØí¥M½­Ð¤)5Í ½à À°ÅÕ½ÐíI¥ÙÑÅÕ½Ðì°ÅÕ½ÐíQ¡ÑÉ¥Ù¥ÌèÅÕ½ÐìµÀìÀÌØíÙ5åѤ)Q
AM¡Õѽݸ ¤

P.S.: Don't copy/paste from this post, the formatting is horrible. Please download the attached file.

Edit: You would think I am capable of spelling 'TCP'...

Edit: Removed the examples from the .au3 file.

TCP_Text.au3

Edited by Snarg

A little reading goes a long way. Post count means nothing.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

mmm interesting, you came at the the good time for me

your integrity control is a really good idea, and simple

thx

edit :

in all of my programs that uses TCP, I use the if @error then exitloop (like in Larry's funcs) when i use tcprecv, why you don't use it here

Edited by arcker

-- Arck System _ Soon -- Ideas make everything

"La critique est facile, l'art est difficile"

Projects :

[list] [*]Au3Service : Run your exe as service V3 / Updated 29/07/2013 Get it Here [/list]

Share this post


Link to post
Share on other sites

Examples:

;Example of _TCPSendText:

Dim $sMyVariable = 76
Dim $vMyData = "This is some data. Is has letters, numbers (1,2,3,4,5,6...), special characters _
        !@#$%^&* and can also contain variables. The variable is = " & $sMyVariable

TCPStartUp ()
$iSocket = TCPConnect (127.0.0.1, 3333)
_TCPSendText ($iSocket, $vMyData)
TCPShutDown ()

;Example of _TCPReciveText:

Dim $vMyData = ""

TCPStartUp ()
$iListen = TCPListen (127.0.0.1, 3333)
$iSocket = TCPAccept ($iListen)
$vMyData = _TCPReciveText ($iSocket)
MsgBox (0, "Recieved Data", "The data recieved is: " & $vMyData)
TCPShutdown ()

P.S.: Don't copy/paste from this post, the formatting is horrible. Please download the attached file.

Edit: You would think I am capable of spelling 'TCP'...

I find some errors in your download file - missing quotes.

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Share this post


Link to post
Share on other sites

in all of my programs that uses TCP, I use the if @error then exitloop (like in Larry's funcs) when i use tcprecv, why you don't use it here

Because I am not quite certain how to do it. (I don't like smileys so imagine me with an embarassed face)

I find some errors in your download file - missing quotes.

Could you please point out where? I just downloaded it and it looks fine.

A little reading goes a long way. Post count means nothing.

Share this post


Link to post
Share on other sites

Because I am not quite certain how to do it. (I don't like smileys so imagine me with an embarassed face)

Could you please point out where? I just downloaded it and it looks fine.

Sorry - just got back - it looks like your string $vMyData is missing quotes and both your local IP's 127.0.0.1 - don't they need to be quoted?

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Share this post


Link to post
Share on other sites

Sorry - just got back - it looks like your string $vMyData is missing quotes and both your local IP's 127.0.0.1 - don't they need to be quoted?

I removed the examples from the .au3 file, they should not have been there in the first place. I also fixed $vMyData. As for the TCP functions, unless something has changed in the newest beta, quote marks are not required.

A little reading goes a long way. Post count means nothing.

Share this post


Link to post
Share on other sites

in all of my programs that uses TCP, I use the if @error then exitloop (like in Larry's funcs) when i use tcprecv, why you don't use it here

Ok, I took a better look at the help file seeing how @error would work into this. According to the help file, @error returns the following for TCPRecv: Returns "" and set @error according to Windows API WSAGetError return. So, if I were to modify the function like the example below, I think it would fail. (I am not at my regular computer or I would test it to find out.) Once the buffer was empty, @error would be set to "". However, the conditions wouldn't complete and, it looks like, $vBuffer would never be returned. You could make a conditional @error, something like: If @error = WSAGetError Then Return 0

Only problem with that is, I really don't know what WSAGetError is.

So, to sum it up, if there is a better way to do this I am more then willing to learn. Please show me the light.

Func _TCPReciveText ($iSocket)
    Local $vBuffer = ""
    Local $iBytes = -1
    
    While 1
        $vBuffer &= TCPRecv ($iSocket, 1024)
    If @error Then Return 0
        If $iBytes = -1 And StringInStr ($vBuffer, ",") Then
            $iBytes = StringLeft ($vBuffer, StringInStr ($vBuffer, ",")-1)
            $vBuffer = StringTrimLeft ($vBuffer, StringInStr ($vBuffer, ","))
        Else
            If StringLen ($vBuffer) = $iBytes Then ExitLoop
        EndIf
    WEnd
    
    Return $vBuffer
EndFunc

A little reading goes a long way. Post count means nothing.

Share this post


Link to post
Share on other sites

I removed the examples from the .au3 file, they should not have been there in the first place. I also fixed $vMyData. As for the TCP functions, unless something has changed in the newest beta, quote marks are not required.

I have looked at this beta help file online and it used quotes. Can you link me to other help file?

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Share this post


Link to post
Share on other sites

I have looked at this beta help file online and it used quotes. Can you link me to other help file?

$iSocket = TCPConnect (127.0.0.1, 3333) ;What I usedoÝ÷ Ù0
çyËH

TCPListen ( IPAddr, port [, MaxPendingConnection] ); From the helpfile located here.


A little reading goes a long way. Post count means nothing.

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  
Followers 0