Sign in to follow this  
Followers 0
Suzuran

TCP explained

6 posts in this topic

#1 ·  Posted (edited)

I didn't understand a few things about TCP...here's the code with questions in the brackets.

Global $MainSocket = -1   
Global $ConnectedSocket = -1      ;(what is with this variable and why is it "-1" like the $MainSocket? )
Local $MaxConnection = 1; Maximum Amount Of Concurrent Connections
Local $MaxLength = 512; Maximum Length Of String
Local $Port = 1000; Port Number
Local $Server = @IPAddress1; Server IpAddress
TCPStartup()
$MainSocket = TCPListen($Server, $Port)
If $MainSocket = -1 Then Exit MsgBox(16, "Error", "Unable to intialize socket.")  ; (Well, $MainSocket is already -1 because we ;just declared it like that...)
While 1
    $Data = TCPRecv($ConnectedSocket, $MaxLength)      ; ( preety confuzed)         
    If $Data = "~bye" Then
        MsgBox(16, "Session Ended", "Connection Terminated.")
        Exit
    ElseIf $Data <> "" Then 
; Unconditional Receive
        MsgBox(0, "Received Packet", $Data)
    EndIf
    If $ConnectedSocket = -1 Then
        $ConnectedSocket = TCPAccept($MainSocket)
        If $ConnectedSocket <> -1 Then
; Someone Connected
            TCPSend($ConnectedSocket, "Connected!")
        EndIf
    EndIf
WEnd
Func OnAutoItExit()
    If $ConnectedSocket <> - 1 Then
        TCPSend($ConnectedSocket, "~bye")
        TCPCloseSocket($ConnectedSocket)
    EndIf
    If $MainSocket <> -1 Then TCPCloseSocket($MainSocket)
    TCPShutdown()
EndFunc;==>OnAutoItExit
:) The help in AutoIt isn't very good on TCP for begginers like me... Edited by Suzuran

Share this post


Link to post
Share on other sites



Flawed? I can't belive I spent 30 minutes to decipher it...and still confused on how AutoIt works with TCP.

Share this post


Link to post
Share on other sites

Thanks alot, Larry!!! Now it's clear.

Share this post


Link to post
Share on other sites

Suzuran.. if you need more info theres a lovely thread here


Start here if you are new Valuater's AutoIT 1-2-3Looking for an UDF - Look hereDo you need to do it twice - Autoit

Share this post


Link to post
Share on other sites

The foundation you need before using TCP is generally knowing how to script in AutoIt... once you have a good foundation...

TCP is like this... (Single connection for now)

SERVER

- TCPStartup() always... to tell the TCP Gods you are coming...

- TCPListen() on public IP of this PC .. returns a listening socket handle

- BIG FOREVER LOOP

--- little loop around TCPAccept( listening socket here ) until successful ... returns a connected socket handle

------ return success then exitloop

--- little loop TCPRecv( connected socket here )/TCPSend( connected socket here )

------ to get data if the is any incoming... and send data if there is any to send

------ if TCPRecv() or TCPSend() fails (@error) then exitloop (go back) to TCPAccept() and wait for another...

-

CLIENT

- TCPStartup() always... to tell the TCP Gods you are coming...

- TCPConnect() hopefully successful ... returns a connected socket handle

- little loop TCPRecv( connected socket here )/TCPSend( connected socket here )

--- to get data if the is any incoming... and send data if there is any to send

--- if TCPRecv() or TCPSend() fails (@error) then exit or try to connect again (BIG FOREVER LOOP around this)

Lar.

Thank you very much Larry. Finally !

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