Jump to content

TCPStartServer


Recommended Posts

This is a server app written by AutoIt Smith.

http://www.autoitscript.com/fileman/users/AutoIt%20Smith/AutoIt%20Server%20Update%201.au3

Global Const $Port = 8000
Global Const $MaxConnection = 100


Global $MainSocket = TCPStartServer($Port, ($MaxConnection + 1))





Func TCPStartServer($Port, $MaxConnect = 1)
    Local $Socket
    $Socket = TCPStartup()
    Select
        Case $Socket = 0
            SetError(@error)
            Return -1
    EndSelect
    $Socket = TCPListen(@IPAddress1, $Port, $MaxConnect)
    Select
        Case $Socket = -1
            SetError(@error)
            Return 0
    EndSelect
    SetError(0)
    Return $Socket

What I don't understand is:

Global $MainSocket = TCPStartServer($Port, ($MaxConnection + 1))

a number ($MaxConnection + 1) was given to TCPStartServer, how come

Func TCPStartServer($Port, $MaxConnect = 1) used 1 for the function?

Also, is $mainsocket an array or a variable?

Link to comment
Share on other sites

Global $CurrentSocket = 0

Does this mean it's 0 throughout the script?

yet there are codes that changes this varibale to something else ....

Global is a keyword that tells the interpreter to use the same handle for the variable in all the script. It is opposite to the Local keyword, which is used to make a variable ONLY exist in the current instance of a function (especially useful if 1. a function recursively calls itself, you can still use the same variable name, or 2. you use the same variable names in cut-and-pasted pieces of code in different functions).

In general, using Global and Local to set the 'scope' of a variable is very useful if you want to actively avoid 'spaghetti code' where changing a (global) variable somewhere in the script and forgetting that in other parts of the script can lead to the most hideous, untraceable and head-breaking-to-solve runtime problems. (Or if you use a lot of recursion.)

Ofcourse, just pressing F1 and looking up 'global' and 'local' in the help file (remember that - it might come in handy), or even googling it, gives you craploads of information where you can learn all this for yourself.

/edit: btw, a 'global' declaration does NOT mean the variable has to be the same in all the script, but only that the same variable exists in all the script. It can be changed anywhere. To actually fix a value onto a variable in such a way that it can never be changed, use the 'Const' (constant) keyword in the declaration. Tradition pretty much dictates that Constant variable names have to be written in capital to be able to quickly distinguish them from other variables. As in: $GUI_EVENT_CLOSE, $GUI_CHECKED etc..

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Global $ConnectedSocket[ ($MaxConnection + 1) ]
For $Track = 0 To $MaxConnection Step 1
    $ConnectedSocket[$Track] = -1
Next

Does it mean that $connectsocket was declared as an array whose number of array is ($MaxConnection + 1)

and then all of it is filled with -1 ?

... yes. Only you should say "an array whose number of elements is ($MaxConnection + 1)".

Roses are FF0000, violets are 0000FF... All my base are belong to you.

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...