tcp func gui

  • Hey.

    Ich habe ein Script welches sobald es daten via. TCP recieved eine GUI darstellt mit dem Inhalt.
    Darauf befindet sich ein Button, der heißt "OK".

    Sprich
    if $recv <> "" then
    showgui()
    Endif

    Wenn man Ok drückt wird ein GUIdelete ausgeführt.
    Aber irgendwie hängt er dann noch in der func. Ich sag dem nämlich am das der der Func eine msgbox auftauchen soll.

    Spoiler anzeigen
    [autoit]


    #include <GUIConstantsEx.au3>
    #include <Constants.au3>
    #include <EditConstants.au3>
    #include <Array.au3>

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Opt("TrayOnEventMode",1)
    Opt("TrayMenuMode", 1)
    Traysetonevent($TRAY_EVENT_SECONDARYDOWN,"SpecialEvent")
    Traysetonevent($TRAY_EVENT_PRIMARYDOWN,"SpecialEvent")
    TraySetState()
    global $tcp,$corner,$xD, $recv
    ;==============================================
    ;==============================================
    ;SERVER!! Start Me First !!!!!!!!!!!!!!!
    ;==============================================
    ;==============================================
    test()
    Func test()
    ; Set Some reusable info
    ; Set your Public IP address (@IPAddress1) here.
    ; Local $szServerPC = @ComputerName
    ; Local $szIPADDRESS = TCPNameToIP($szServerPC)
    Local $szIPADDRESS = @IPAddress1
    Local $nPORT = 33891
    Local $MainSocket, $GOOEY, $edit, $ConnectedSocket, $szIP_Accepted
    Local $msg

    [/autoit] [autoit][/autoit] [autoit]

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

    [/autoit] [autoit][/autoit] [autoit]

    ; Create a Listening "SOCKET".
    ; Using your IP Address and Port 33891.
    ;==============================================
    $MainSocket = TCPListen($szIPADDRESS, $nPORT)

    [/autoit] [autoit][/autoit] [autoit]

    ; If the Socket creation fails, exit.
    If $MainSocket = -1 Then Exit

    [/autoit] [autoit][/autoit] [autoit]

    ; Initialize a variable to represent a connection
    ;==============================================
    $ConnectedSocket = -1

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ;Wait for and Accept a connection
    ;==============================================
    Do
    $ConnectedSocket = TCPAccept($MainSocket)
    Until $ConnectedSocket <> -1

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ; Get IP of client connecting
    $szIP_Accepted = SocketToIP($ConnectedSocket)

    [/autoit] [autoit][/autoit] [autoit]

    while 1 ; GUI Message Loop
    ;==============================================
    $msg = GUIGetMsg()

    [/autoit] [autoit][/autoit] [autoit]

    ; GUI Closed
    ;--------------------
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop

    [/autoit] [autoit][/autoit] [autoit]

    ; Try to receive (up to) 2048 bytes
    ;----------------------------------------------------------------
    $recv = TCPRecv($ConnectedSocket, 2048)

    [/autoit] [autoit][/autoit] [autoit]

    ; If the receive failed with @error then the socket has disconnected
    ;----------------------------------------------------------------
    If @error Then ExitLoop

    [/autoit] [autoit][/autoit] [autoit]

    ; Update the edit control with what we have received
    ;----------------------------------------------------------------

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    If $recv <> "" Then
    display()
    Endif

    [/autoit] [autoit][/autoit] [autoit]

    If $ConnectedSocket <> -1 Then TCPCloseSocket($ConnectedSocket)

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    TCPShutdown()

    [/autoit] [autoit][/autoit] [autoit]

    Wend
    test()
    Endfunc

    [/autoit] [autoit][/autoit] [autoit]

    ; Function to return IP Address from a connected socket.
    ;----------------------------------------------------------------------
    Func SocketToIP($SHOCKET)
    Local $sockaddr, $aRet

    [/autoit] [autoit][/autoit] [autoit]

    $sockaddr = DllStructCreate("short;ushort;uint;char[8]")

    [/autoit] [autoit][/autoit] [autoit]

    $aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $SHOCKET, _
    "ptr", DllStructGetPtr($sockaddr), "int*", DllStructGetSize($sockaddr))
    If Not @error And $aRet[0] = 0 Then
    $aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($sockaddr, 3))
    If Not @error Then $aRet = $aRet[0]
    Else
    $aRet = 0
    EndIf

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    $sockaddr = 0

    [/autoit] [autoit][/autoit] [autoit]

    Return $aRet
    EndFunc ;==>SocketToIP

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    func display()
    Guicreate("* Message Info",450,300)
    GuictrlcreateLabel("Message from *",10,30,400)
    Guictrlsetfont(-1,12)
    Guictrlsetstate(-1,$GUI_DISABLE)
    Guictrlcreatepic(@scriptdir & "\*.jpg",310,10,130,38)
    $mess = GuictrlcreateEdit("",10,70,430,150,$ES_LEFT)
    Guictrlsetstate(-1,$GUI_DISABLE)
    Guictrlcreatelabel(" For More Information please call * or write an Email to *",10,230,400)
    Guictrlsetstate(-1,$GUI_DISABLE)
    $exit = Guictrlcreatebutton("Okay, I've read !",160,270,120,25)
    GUISetState(@SW_SHOW) ; Zeigt das leere GUI-Fenster
    Guictrlsetdata($mess,$recv)

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    $msg = GUIGetMsg()
    switch $msg
    case $exit
    test()
    Guidelete()
    Endswitch
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    WEnd
    Endfunc

    [/autoit]

    Ach anders ^^ der soll einfach wenn das guideleted worden ist, wieder mit dem listen anfangen und abfragen ob er daten via. tcp bekommt un wenn dann wieder die gui. usw. usw.

    aber sobald ichn guidelete mache geht das script aus. bzw wenn ich sage ruf die func nach dem del wieder auf. bleibts script zwar da aber es passiert nichts. sobald ich neu sende.

    Einmal editiert, zuletzt von Kev (1. Juli 2010 um 11:00)

  • Ich würde an deiner stelle beide guis gleich am anfang erstellen, und nur dann einblenden wenn sie gebraucht werden.
    Vorteile:
    - übersichtlicheres skript
    - nur eine while endlosschleife

  • Stell ein GUI auf @SW_SHOW das andere auf @SW_HIDE.

    Vorher erstellen ist vorrausgesetzt ist immer besser. Wenn das andere gebraucht wird

    [autoit]

    GUISetState(@SW_SHOW, $hGUI2)

    [/autoit]

    und das andere

    [autoit]

    GUISetState(@SW_HIDE, $hGUI1)

    [/autoit]

    Eine GUIGetMsg() mit 2 GUI's sieht folgendermaßen aus:

    Quick'n'Dirty

    [autoit]

    $hgui1 = GUICreate("a")
    $hbtn1 = GUICtrlCreateButton("gui2", 1, 1)
    GUISetState(@SW_SHOW, $hgui1)
    $hgui2 = guicreate("b")
    $hbtn2 = GUICtrlCreateButton("gui1", 1, 1)
    GUISetState(@SW_HIDE, $hgui2)

    [/autoit][autoit][/autoit][autoit]

    while 1
    $nmsg = guigetmsg(1)
    switch $nmsg[1]
    case $hgui1
    Switch $nmsg[0]
    case $hbtn1
    GUISetState(@SW_HIDE, $hgui1)
    guisetstate(@SW_SHOW, $hgui2)
    case -3
    exit
    EndSwitch
    Case $hgui2
    Switch $nmsg[0]
    case $hbtn2
    GUISetState(@SW_HIDE, $hgui2)
    guisetstate(@sw_show, $hgui1)
    case -3
    GUISetState(@SW_HIDE, $hgui2)
    guisetstate(@sw_show, $hgui1)
    EndSwitch
    EndSwitch
    wend

    [/autoit]

    hierbei lohnt es sich mit

    [autoit]

    Opt("GUIOnEventMode", 1)

    [/autoit]

    zu arbeiten, da man da diese lange Schleife nicht hat.
    Switch $nMsg