Jump to content
Sign in to follow this  
Shanheavel

Multi-client server 's loop speed

Recommended Posts

Shanheavel

Hello,

I wrote TCP/IP multi-client server. I think it's too slow for many receivers. Is it possible to optimization this code?

My server:

Global $ConnectedSocket[100]

$IP = @IPAddress1

TCPStartUp()

$MainSocket = TCPListen($IP, 33897,  100 )
If $MainSocket = -1 Then Exit

While 1
$NewSocket = TCPAccept($MainSocket)
If $NewSocket > 0 Then
For $i = 0 to 99
If $ConnectedSocket[$i] = "" Then
$ConnectedSocket[$i] = $NewSocket
ExitLoop
EndIf
Next
EndIf

For $i = 0 to 99
$Recv = TCPRecv($ConnectedSocket[$i], 1024)
If @error Then
TCPCloseSocket($ConnectedSocket[$i])
$ConnectedSocket[$i] = ""
EndIf
If $Recv <> "" Then ConsoleWrite(@CR & "Socket: " & $ConnectedSocket[$i] & " Recv: " & $Recv)
Next
Sleep(10)
Wend
Edited by Adrian777

Share this post


Link to post
Share on other sites
twitchyliquid64

Add:

if $ConnectedSocket[$i] = "" then continueloop

To the beginning of your second FOR Loop.

This means that is will only call TCPRecv and TCPCloseSocket if the Socket is set that it is in use.

Edited by hyperzap

ongoing projects:-firestorm: Largescale P2P Social NetworkCompleted Autoit Programs/Scripts: Variable Pickler | Networked Streaming Audio (in pure autoIT) | firenet p2p web messenger | Proxy Checker | Dynamic Execute() Code Generator | P2P UDF | Graph Theory Proof of Concept - Breadth First search

Share this post


Link to post
Share on other sites
twitchyliquid64

Also getting rid of Sleep(10) will make it run hell faster, but max out one of your CPU cores.


ongoing projects:-firestorm: Largescale P2P Social NetworkCompleted Autoit Programs/Scripts: Variable Pickler | Networked Streaming Audio (in pure autoIT) | firenet p2p web messenger | Proxy Checker | Dynamic Execute() Code Generator | P2P UDF | Graph Theory Proof of Concept - Breadth First search

Share this post


Link to post
Share on other sites
Shanheavel

@hyperzap

Thanks.

Edited source code:

Global $ConnectedSocket[100]

$IP = @IPAddress1

TCPStartUp()

$MainSocket = TCPListen($IP, 33897,  100 )
If $MainSocket = -1 Then Exit

While 1
$NewSocket = TCPAccept($MainSocket)
If $NewSocket > 0 Then
For $i = 0 to 99
If $ConnectedSocket[$i] = "" Then
$ConnectedSocket[$i] = $NewSocket
ExitLoop
EndIf
Next
EndIf

For $i = 0 to 99
If $ConnectedSocket[$i] = "" Then Continueloop
$Recv = TCPRecv($ConnectedSocket[$i], 1024)
If @error Then
TCPCloseSocket($ConnectedSocket[$i])
$ConnectedSocket[$i] = ""
EndIf
If $Recv <> "" Then ConsoleWrite(@CR & "Socket: " & $ConnectedSocket[$i] & " Recv: " & $Recv)
Next
Wend

Any ideas?

Edited by Adrian777

Share this post


Link to post
Share on other sites
twitchyliquid64

If you need faster, you will have to go to a compiled language, because that's just about the limits of speed.

This should be able to handle a thousand requests or so a second.


ongoing projects:-firestorm: Largescale P2P Social NetworkCompleted Autoit Programs/Scripts: Variable Pickler | Networked Streaming Audio (in pure autoIT) | firenet p2p web messenger | Proxy Checker | Dynamic Execute() Code Generator | P2P UDF | Graph Theory Proof of Concept - Breadth First search

Share this post


Link to post
Share on other sites
Mat

Right now you are looping through your $ConnectedSocket array to find an empty one when you are assigning them values... It would be much better for you to set up a stack for empties:

This code is not tested, and knowing me will almost certainly not work first time. Its the thought that counts :)

Local $aEmpty[100], $iLastEmpty = 99
For $i = 0 to 99
    $aEmpty[$i] = 99-$i
Next

$IP = @IPAddress1

TCPStartUp()

$MainSocket = TCPListen($IP, 33897,  100)
If $MainSocket = -1 Then Exit

While 1
    If $iLastEmpty >= 0 Then
        $ConnectedSocket[$aEmpty[$iLastEmpty]] = TCPAccept($MainSocket)
        If Not @error Then $iLastEmpty -= 1
    EndIf

    For $i = 0 to 99
        If $ConnectedSocket[$i] = -1 Then Continueloop

        $Recv = TCPRecv($ConnectedSocket[$i], 1024)
        If @error Then
            TCPCloseSocket($ConnectedSocket[$i])
            $ConnectedSocket[$i] = -1
            $iLastEmpty += 1
            $aEmpty[$iLastEmpty] = $i
            ContinueLoop
        EndIf
        If $Recv <> "" Then ConsoleWrite(@CR & "Socket: " & $ConnectedSocket[$i] & " Recv: " & $Recv)
     Next
Wend

No idea what that looks like speed wise, but it should be faster for allocating sockets, and I also added a ContinueLoop when you close the socket as $Recv will always be "" if @error is set so you don't need to finish off the end of the loop.

Mat

Edited by Mat

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  

×