Jump to content
Sign in to follow this  
p)(d

Check a TCP IP connection

Recommended Posts

p)(d

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

Share this post


Link to post
Share on other sites
p)(d

Thanx Jos,

with your tip it works now.

but is there a better way?

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


SYS 64738

Share this post


Link to post
Share on other sites
fctd

@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]

Share this post


Link to post
Share on other sites
p)(d

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

Share this post


Link to post
Share on other sites
SmOke_N

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.

Share this post


Link to post
Share on other sites
fctd

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]

Share this post


Link to post
Share on other sites
p)(d

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

Share this post


Link to post
Share on other sites
fctd

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]

Share this post


Link to post
Share on other sites
p)(d

yes, stdout in a loop was the solution for me.


SYS 64738

Share this post


Link to post
Share on other sites
p)(d

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

Share this post


Link to post
Share on other sites
GEOSoft

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!"

Share this post


Link to post
Share on other sites
p)(d

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

Share this post


Link to post
Share on other sites
p)(d

How are you making a connection ? :D

Greeting.

i have a "black box software" on server one with my autoit program for monitoring the connections from server one to server two.


SYS 64738

Share this post


Link to post
Share on other sites
Yashied

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

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  

×