Jump to content

Optimized TCP Server


themax90
 Share

Recommended Posts

Here is my new revolutionized TCP Server Structure. Before, in my ITS Project it wrote everything to an ini. Which does take longer then expected. Cycles to go through the while structure on a low specs machine for the File based version was around 120 to 500 milliseconds, while the internal structure is 20 to 70 milliseconds. It is a dramatic improvement and will perform better under high traffic. This hopefully enables everyone to host a chat server as long as there is a direct connection. It even works well under 56k but is not recommended.

Sending ~bye will terminate the connection.

Optimized Server

Global Const $Port = 50911
Global $MaxConc = 100
Global $MainSocket = TCPStartServer($Port, $MaxConc)
If @Error <> 0 Then Exit MsgBox(16, "Error", "Server unable to initialize.")
Global Const $MaxLength = 512
Global $ConnectedSocket[$MaxConc]
Global $CurrentSocket = 0
Local $Track = 0
Global Const $MaxConnection = ($MaxConc - 1)
For $Track = 0 To $MaxConnection Step 1
    $ConnectedSocket[$Track] = -1
Next
While 1
    $ConnectedSocket[$CurrentSocket] = TCPAccept($MainSocket)
    If $ConnectedSocket[$CurrentSocket] <> -1 Then
        $CurrentSocket = SocketSearch()
    EndIf
    $Track = 0
    For $Track = 0 To $MaxConnection Step 1
        If $ConnectedSocket[$Track] <> - 1 Then 
            $Data = TCPRecv($ConnectedSocket[$Track], $MaxLength)
            If $Data = "~bye" Then
                TCPCloseSocket($ConnectedSocket[$Track])
                $ConnectedSocket[$Track] = -1
                $CurrentSocket = SocketSearch()
            ElseIf $Data <> "" Then
                TCPSendMessageAll($MaxConnection, $Data)
            EndIf
        EndIf
    Next
WEnd
Func TCPSendMessageAll($ConnectionLimit, $Data)
    Local $Track = 0
    For $Track = 0 To $ConnectionLimit Step 1
        If $ConnectedSocket[$Track] <> - 1 Then TCPSend($ConnectedSocket[$Track], $Data)
    Next
EndFunc  ;==>TCPSendMessageAll
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
EndFunc
Func SocketSearch()
    Local $Track = 0
    For $Track = 0 To $MaxConnection Step 1
        If $ConnectedSocket[$Track] = -1 Then
            Return $Track
        Else
        ; Socket In Use
        EndIf
    Next
EndFunc  ;==>SocketSearch

I know it might a bit of a jarbled mess but it is a start to others. If you have any enhancements or ideas I would love to hear them.

AutoIt Smith

Link to comment
Share on other sites

It is a good upgrade from your old code...if I would have read your old code then I might have said something about it. I have been using a method similar to this to accept multiple connections at a time, and then looping through each one, then using the SocketToIP function to put them all in a list box. The user would select which one they wanted to connect to, and then it would close all of the others besides the selected IP.

Nice work though, and good luck with future ITS development.

Link to comment
Share on other sites

Does no one care?

Sorry been away from the forums today :P

I get it to run and I can connect to it with putty using the port set inthe script, I get back echos so everything I type is doube - ~bye does not work for me... How did you mean for a client to connect, I could see somehow using it like your remote server / client combo would be usefull...

Are you still planning on working on yoru remote server?

Thanks!

Link to comment
Share on other sites

Yah I will eventually work more on the server. Here is a test client, really simple, no GUI.

Global $MainSocket
Local $MaxLength = 512; Maximum Length Of Text
Local $Port = 50911; Port Number
Local $Server = @IPAddress1; Server IpAddress
TCPStartup()
$MainSocket = TCPConnect($Server, $Port)
If $MainSocket = -1 Then Exit MsgBox(16, "Error", "Unable to connect.")
TCPSend($MainSocket, "Connection Initialized")
While 1
    $Data = TCPRecv($MainSocket, $MaxLength)
    If $Data = "~bye" Then
        MsgBox(16, "Session Ended", "Connection Terminated.")
        Exit
    ElseIf $Data <> "" Then
   ; Unconditional Receive
        MsgBox(0, "Received Packet", $Data)
    EndIf
WEnd
Func OnAutoItExit()
    If $MainSocket <> - 1 Then
        TCPSend($MainSocket, "~bye")
        TCPCloseSocket($MainSocket)
    EndIf
    TCPShutdown()
EndFunc ;==>OnAutoItExit

I still need to figure out how to run a function as variable to complete the server because I want FULL AutoIt Functionality.

Here is a useless example of what I am looking for(Without REAL functions):

$Recv = TCRecv($Socket, 512)
; In this case $Recv = MsgBox(0, "", "Test")
If SyntaxCheck($Recv) Then Command($Recv)

I will sometime, whenever I get the time. Cheers :P

AutoIt Smith

Link to comment
Share on other sites

I still need to figure out how to run a function as variable to complete the server because I want FULL AutoIt Functionality.

Here is a useless example of what I am looking for(Without REAL functions):

$Recv = TCRecv($Socket, 512)
; In this case $Recv = MsgBox(0, "", "Test")
If SyntaxCheck($Recv) Then Command($Recv)
You want Execute(), but you need to be aware that it will not execute all of the available functions (please refer to the Remarks section of the Execute() help page) and if you want to be able to receive and execute those unavailable functions, you will need to implement them individually. (I suppose you also have the option of invoking another AutoIt instance to do it.)
Link to comment
Share on other sites

Yes but then they require either

1. AutoIt Beta Environment

or

2. Aut2Exe, which takes a while to compile and may error.

I just think I will need to write out EVERYTHING by myself. Horay!

So expect the Remote Server too be out within two months, maybe more. I am going to make a Server per computer version, and a Remote Client per computer version. This way if it's a home computer your cool but if it's a home computer on a network and cannot have a direct connection you can use the client version. Many things still unknown! Thanks for the enthusiasm!

@LxP

That's my problem, I need it to check the syntax of EVERY statement, that's atleast one or two lines PER value, and then operate the function, which can be about three to as many as six per function. Soon I have useless functions that do everything that AutoIt does only twenty lines extended. Who knows..... I requested IsFunc($Func) to return Native, User-Defined, or None but Valik seems too think that there is no use. So I am stuck reinventing the wheel in a longer way now. :P Thanks for the post however.

Link to comment
Share on other sites

Don't they need the beta version anyway for all of the TCP stuff?

Can't you just do something like this (requires beta)? --

Func MyExecute($Cmd)
    RunWait(@AutoItEXE & ' /AutoIt3ExecuteLine "' & $Cmd & '"')
EndFunc

This doesn't take into account the fact that $Cmd may contain double quotes (which is bad in this instance) but it's not impossible to either work around that or write appropriate conversion code before the RunWait().

Link to comment
Share on other sites

Thank you VERY MUCH Alex!!! You are the first person to ACTUALLY solve my problem!!! Thank you.

Hehe this is WHAT I WANTED YES!!!!!!!!!

MyExecute("MsgBox(0, 'Title', 'Text')")
Func MyExecute($Cmd)
    RunWait(@AutoItEXE & ' /AutoIt3ExecuteLine "' & $Cmd & '"')
EndFunc

Remote Server due out by January 7th.

AutoIt Smith

Edited by AutoIt Smith
Link to comment
Share on other sites

  • 3 months later...

It gave me a virus!

:think::(:)

What the hell are you talking about?

Ditto, That is extreme bullshit. None of my code has ever given anyone a virus and you have to COPY and PASTE the code yourself. Explain yourself please. Atleast 1000 users have used this from my school and other locations off this very site so there is nothing wrong with it.

Edited by AutoIt Smith
Link to comment
Share on other sites

It gave me a virus!

:think::(:)

Please advise name of anti-virus product that tells you that you have been infected, version number, signature pattern, and version of AutoIT you are running.

Search for 'false positive' in these threads and submit the offending code to your anti-virus vendor for further testing.

Link to comment
Share on other sites

Windows gave my a virus too! I installed the Microsoft Windows Operating System and i realized the whole thing was just a huge virus that was intent on taking over the world. :think:

"So man has sown the wind and reaped the world. Perhaps in the next few hours there will no remembrance of the past and no hope for the future that might have been." & _"All the works of man will be consumed in the great fire after which he was created." & _"And if there is a future for man, insensitive as he is, proud and defiant in his pursuit of power, let him resolve to live it lovingly, for he knows well how to do so." & _"Then he may say once more, 'Truly the light is sweet, and what a pleasant thing it is for the eyes to see the sun.'" - The Day the Earth Caught Fire

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