Shanheavel Posted July 18, 2011 Share Posted July 18, 2011 (edited) 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 July 18, 2011 by Adrian777 Link to comment Share on other sites More sharing options...
twitchyliquid64 Posted July 18, 2011 Share Posted July 18, 2011 (edited) 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 July 18, 2011 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 Link to comment Share on other sites More sharing options...
twitchyliquid64 Posted July 18, 2011 Share Posted July 18, 2011 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 Link to comment Share on other sites More sharing options...
Shanheavel Posted July 18, 2011 Author Share Posted July 18, 2011 (edited) @hyperzapThanks. 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 WendAny ideas? Edited July 18, 2011 by Adrian777 Link to comment Share on other sites More sharing options...
twitchyliquid64 Posted July 19, 2011 Share Posted July 19, 2011 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 Link to comment Share on other sites More sharing options...
Mat Posted July 19, 2011 Share Posted July 19, 2011 (edited) 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 July 19, 2011 by Mat AutoIt Project Listing Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now