Sign in to follow this  
Followers 0
sc4ry

where is the problem? tcpconnect?

3 posts in this topic

hello,

i have one problem, but i can´t find the mistake nor how to fix it.

i have my new tool, which should ping ip adresses ports. therefor i found a "startip, endip to iprange" script by bugfix. because i don´t want to wait until the whole range is generated, i try to add my code to this function, which also works so far.

but i mention, that this is really slow and i can´t figure out, why.

to constrict, where the "mistake" could be, i try different thins. i included a timer for the ip generation and for the tcpconnect function. that shows, that the ip is really fast generated, but the tcpconnection may takes ca. 20.000 ms. after that i tried to use the Opt("TCPTimeout",100) and if anywhere in the includes is another value for this. nowhere these option was set new nor a lower timeout brings any better result.

so i start to copy only the needed function in 2 new scripts. one, where i add some ips in an array and the other i use the normal function of bugfixs ip-range script.

both shows, that the tcpconnect-function is really fast AND that the range of ips is also generated very fast.

so, where is the problem in my first script, that this is soooooo low???

from the timer its tcpconnect ... from the other script, it seems to handle with the ip-generation and tcpconnect.

could anybody find the mistake or can give me some help to speed this up?

here are all three scripts, so that you can also see, how slow/fast it is ...

MAINSCRIPT

#NoTrayIcon
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Version=beta
#AutoIt3Wrapper_icon=..\..\..\Icons\shell32.ico
#AutoIt3Wrapper_Compression=4
#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_Change2CUI=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <Array.au3>

Global $IPStart, $IPEnd
Global $fBroadcast = False

    $IPStart = "211.117.64.66"
    $IPEnd = "211.117.64.74"
        If Not _IsIPv4($IPStart) Or Not _IsIPv4($IPEnd) Then Exit
    $iPort = "8888"
        If $iPort <= 0 Or $iPort > 65535 Then Exit
    
    $sDelim = Opt("GuiDataSeparatorChar")
    Local $pattern = '(\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.' & _
            '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b)'
    If (Not StringRegExp($IPStart, $pattern, 0) Or Not StringRegExp($IPEnd, $pattern, 0)) Then Exit

    $sock = ""

    TCPStartup()
    $timer = TimerInit()

    Local $var = StringSplit($IPStart, '.')
    Local $o1 = $var[1], $o2 = $var[2], $o3 = $var[3], $o4 = $var[4]
    $var = StringSplit($IPEnd, '.')
    Local $e1 = $var[1], $e2 = $var[2], $e3 = $var[3], $e4 = $var[4]
    Local $start = 1, $end = 255
    If $fBroadcast Then
        $start = 0
        $end = 256
    EndIf
    While $o1 <= $e1
        While ($o2 <= $e2) Or ($o1 < $e1)
            If $o2 = $end Then
                $o1 += 1
                $o2 = $start
            EndIf
            While ($o3 <= $e3) Or ($o2 < $e2) Or ($o1 < $e1)
                If $o3 = $end Then
                    $o2 += 1
                    $o3 = $start
                EndIf
                While True
                    $IP = $o1 & '.' & $o2 & '.' & $o3 & '.' & $o4;TCPNameToIP($IP)
                    MsgBox(0, "generate", $ip & @CRLF & Round(TimerDiff($timer), 2))

                            $timer2 = TimerInit()
                            $sock = TCPConnect($IP, $iPort)
                            If Not @error Then
                                MsgBox(0, "OK", $ip & @CRLF & Round(TimerDiff($timer2), 2))
                            Else
                                MsgBox(0, "Fail", $ip & @CRLF & Round(TimerDiff($timer2), 2))
                            EndIf
                            TCPCloseSocket($sock)
                    $timer = TimerInit()
                    If ($o1 = $e1) And ($o2 = $e2) And ($o3 = $e3) And ($o4 = $e4) Then
                        TCPShutdown()
                        Exit
                    EndIf

                    $o4 += 1
                    If $o4 = $end Then
                        $o3 += 1
                        $o4 = $start
                    EndIf
                WEnd
            WEnd
        WEnd
    WEnd
    
Func _IsIPv4(Const $S_IP)
    If StringRegExp($S_IP, "\A(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])\z") Then Return (1)
    Return (SetError(1, 0, 0))
EndFunc   ;==>_IsIPv4

TESTSCRIPT 1

$sock = ""
$iport = "8888"

Dim $Ip[10]
$Ip[0] = 9
$Ip[1] = "211.117.64.66"
$Ip[2] = "211.117.64.67"
$Ip[3] = "211.117.64.68"
$Ip[4] = "211.117.64.69"
$Ip[5] = "211.117.64.70"
$Ip[6] = "211.117.64.71"
$Ip[7] = "211.117.64.72"
$Ip[8] = "211.117.64.73"
$Ip[9] = "211.117.64.74"

For $i = 1 To $Ip[0]
    $timer = TimerInit()
    $sock = TCPConnect($IP[$i], $iPort)
    If Not @error Then
        MsgBox(0, "", $IP[$i] & ":" & $iPort & @CRLF & Round(TimerDiff($timer), 2) & "ms")
    Else
        MsgBox(0, "ERROR", $IP[$i] & ":" & $iPort & @CRLF & Round(TimerDiff($timer), 2) & "ms")
    EndIf
    TCPCloseSocket($sock)
Next

TESTSCRIPT 2

$Range = _GenerateIPsInRange("211.117.64.66", "211.117.64.74")

    $iPort = "8888"

For $i = 1 To $Range[0]
    $IP = $Range[$i]
    $timer = TimerInit()
    $sock = TCPConnect($IP, $iPort)
    If Not @error Then
        MsgBox(0, "OK", $ip & @CRLF & Round(TimerDiff($timer), 2))
    Else
        MsgBox(0, "Fail", $ip & @CRLF & Round(TimerDiff($timer), 2))
    EndIf
    TCPCloseSocket($sock)
Next

Func _IsIPv4(Const $S_IP)
    If StringRegExp($S_IP, "\A(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])\z") Then Return (1)
    Return (SetError(1, 0, 0))
EndFunc   ;==>_IsIPv4

;-------------------------------------------------------------------------------------------------
;   Function        _GenerateIPsInRange($IPstart, $IPend, $iRetType=0, $sDelim=Default, $fBroadcast=False)
;   Description     Erstellt alle möglichen IP's zwischen $IPstart und $IPend
;   Parameter       $IPstart        erste IP im Bereich
;                   $IPend          letzte IP im Bereich
;                   $iRetType       Return-Type 0-Array (Standard), 1-String, trennzeichenbasiert
;                   $sDelim         Trennzeichen bei Stringausgabe, Default= Zeichen von Opt("GuiDataSeparatorChar")
;   Return          Succes          Array 1D (Anzahl in $a[0]) oder String mit allen erstellten IP's
;                   Failure         0 und set @error 1 : übergebene IP nicht korrekt
;   Author          BugFix  (bugfix@autoit.de)
;----------------------------------------------------------------------------------------------------------------------
Func _GenerateIPsInRange($IPstart, $IPend, $iRetType=0, $sDelim=Default, $fBroadcast=False)
    If IsKeyword($sDelim) Then $sDelim = Opt("GuiDataSeparatorChar")
    Local $pattern = '(\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.'& _
                     '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b)'
    If ( Not StringRegExp($IPstart, $pattern, 0) Or _
         Not StringRegExp($IPend, $pattern, 0) ) Then
        SetError(1)
        Return 0
    EndIf
    Local $var = StringSplit($IPstart, '.')
    Local $o1 = $var[1], $o2 = $var[2], $o3 = $var[3], $o4 = $var[4]
    $var = StringSplit($IPend, '.')
    Local $e1 = $var[1], $e2 = $var[2], $e3 = $var[3], $e4 = $var[4]
    Local $sOut = '', $start = 1, $end = 255
    If $fBroadcast Then
        $start = 0
        $end = 256
    EndIf
    While $o1 <= $e1
        While ($o2 <= $e2) Or ($o1 < $e1)
            If $o2 = $end Then
                $o1 += 1
                $o2 = $start
            EndIf
            While ($o3 <= $e3) Or ($o2 < $e2) Or ($o1 < $e1)
                If $o3 = $end Then
                    $o2 += 1
                    $o3 = $start
                EndIf
                While True
                    $sOut &= $o1 &'.'& $o2 &'.'& $o3 &'.'& $o4 & $sDelim
                    If ($o1 = $e1) And ($o2 = $e2) And ($o3 = $e3) And ($o4 = $e4) Then
                        If $iRetType Then
                            Return $sOut
                        Else
                            Return StringSplit(StringTrimRight($sOut, StringLen($sDelim)), $sDelim, 1)
                        EndIf
                    EndIf
                    $o4 += 1
                    If $o4 = $end Then
                        $o3 += 1
                        $o4 = $start
                    EndIf
                WEnd
            WEnd
        WEnd
    WEnd
EndFunc  ;==>_GenerateIPsInRange

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

hey,

i found the "mistake" now, it is the while-loop, which "steal" the speed.

now i write each ip in a txt and read it via adlib out again ...

EDIT: ok, it was not the mistake ... it was mine in the testscripts, because i forgot tcpstartup ... and so i could not receive any answer. and that made my testscript such fast.

but when i ping an ip+port ... why the hell i have to wait 20000 ms for an answer oO

Edited by sc4ry

Share this post


Link to post
Share on other sites

if anyone is interested ... it should be fixed in milestone 3.3.7.0

-> http://www.autoitscript.com/trac/autoit/ticket/1573

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  
Followers 0