Jump to content

Check a TCP IP connection


p)(d
 Share

Recommended Posts

i want to check if a tcp connection is established

but my code does not work 100% - any help or better ideas?

While 1
        Sleep(1000)
        If CheckConnection() Then
            GUICtrlSetColor($label, 0x00ff00)
        Else
            GUICtrlSetColor($label, 0xff0000)
        EndIf
    WEnd

Func CheckConnection()
    Local $port = "6510"    
    Local $result = RunWait(@ComSpec & " /c netstat -anop TCP", "", @SW_HIDE ,$STDERR_CHILD + $STDOUT_CHILD)
    Local $line = StdoutRead($result)
    Local $result_array = StringSplit($line, @LF)
    Local $found = False
    For $i =1 to $result_array[0]
        If StringInStr($result_array[$i], $port) Then
            $found = True
            ExitLoop
            EndIf
    Next
    Return $found
EndFunc    ;==>CheckConnection

SYS 64738

Link to comment
Share on other sites

  • Developers

You need to ensure that you retrieve all STDOUT information.

Check the helpfile for a good example.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

@fctd: i dont want to check if i can reach a system - i want to check if a connection is established

p)(d,

Like this ?

; Start The TCP Services
;==============================================
TCPStartUp()

; Connect to a Listening "SOCKET"
;==============================================
$socket = TCPConnect( "0.0.0.0", 12345 )
If $socket = -1 Then 
    Exit
Else
    MsgBox(0, "Connected to: ", $socket)
EndIf

; Shutdown the TCP connection
;==============================================
Func OnAutoItExit()
    ConsoleWrite("Can't connect =")
    TCPShutdown()
EndFunc

[list][font="Century Gothic"]If nothing is certain, everything is possible.[/font][/list][font="Century Gothic"]Experience is something you get, just after you need it.[/font]

Link to comment
Share on other sites

hi fctd,

this script starts a tcpip connection.

but in my case a other application starts the connection and i want to use autoit to show if this connection is etablished.

p)(d,

Like this ?

; Start The TCP Services
;==============================================
TCPStartUp()

; Connect to a Listening "SOCKET"
;==============================================
$socket = TCPConnect( "0.0.0.0", 12345 )
If $socket = -1 Then 
    Exit
Else
    MsgBox(0, "Connected to: ", $socket)
EndIf

; Shutdown the TCP connection
;==============================================
Func OnAutoItExit()
    ConsoleWrite("Can't connect =")
    TCPShutdown()
EndFunc

SYS 64738

Link to comment
Share on other sites

  • Moderators

hi fctd,

this script starts a tcpip connection.

but in my case a other application starts the connection and i want to use autoit to show if this connection is etablished.

fctd's idea should work, as "TCPConnect" should only connect if the socket is "already open". Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

fctd's idea should work, as "TCPConnect" should only connect if the socket is "already open".

How are you making a connection ? :)

Greeting.

[list][font="Century Gothic"]If nothing is certain, everything is possible.[/font][/list][font="Century Gothic"]Experience is something you get, just after you need it.[/font]

Link to comment
Share on other sites

hi fctd,

this script starts a tcpip connection.

but in my case a other application starts the connection and i want to use autoit to show if this connection is etablished.

the server on the other side is always listening!

i want to know if my computer has a established connection to this server or not.

i can see with sysinternals tcpview fctd's script starts a new tcp connection to my server and shows in every case the "Connected to:" message

on my computer there is a client application (no autoitscript!) that starts the TCP connection and i want to use my script as GUI/monitoring

if fctd's "test" would work it would be my favorite solution.

Edited by p)(d

SYS 64738

Link to comment
Share on other sites

Ok, Thanks for the explanation.

What is the end result? Was placing the "StdoutRead" in a loop enough?

Greetings.

[list][font="Century Gothic"]If nothing is certain, everything is possible.[/font][/list][font="Century Gothic"]Experience is something you get, just after you need it.[/font]

Link to comment
Share on other sites

  • 2 months later...

hmm

stdout in a loop is no solution if the program runs a lot of days.

the memory usage increases and increases...

my program is a monitoring tool and it logs the established connections only

some idea for keeping the memory usage low?

SYS 64738

Link to comment
Share on other sites

RunWait() won't accept $STDERR_CHILD + $STDOUT_CHILD.

You can only do that with Run()

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

this is my code:

CODE
Func CheckConnection()

Local $found = False

Local $port = "4234"

Local $result = Run(@ComSpec & " /c netstat -anop TCP", "", @SW_HIDE ,$STDERR_CHILD + $STDOUT_CHILD)

Local $line

While 1

$line = StdoutRead($result)

If @error Then ExitLoop

Local $result_array = StringSplit($line, @LF)

For $i =1 to $result_array[0]

If StringInStr($result_array[$i], $port) Then

If StringInStr($result_array[$i], "HERGESTELLT") Then

$found = True

ExitLoop

EndIf

EndIf

Next

Wend

Return $found

EndFunc ;==>CheckConnection

Edited by p)(d

SYS 64738

Link to comment
Share on other sites

func _IsInternet()
    
    local $Ret = DllCall('wininet.dll', 'int', 'InternetGetConnectedState', 'dword*', 0x20, 'dword', 0)
    
    if (@error) then
        return SetError(1, 0, 0)
    endif
    
    local $wError = _WinAPI_GetLastError()
    
    return SetError((not ($wError = 0)), $wError, $Ret[0])
endfunc; _IsInternet

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