Jump to content

TCP explained


Recommended Posts

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
Link to comment
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 !

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...