Jump to content

Won't Call function


Recommended Posts

Hi there, I'm not sure if this is a GUI problem necessarily but it appears to be so...

I have this "instant messenger" type program I'm making but I cannot get the "Send" button to call my "Send" function for the client. The host is set up exactly the same with no problems. Ill post the code-

#include <GUIConstants.au3>
Global $port = 9999
Global $localhost = @IPAddress1
Global $ipto,$sendtext,$consock,$hostsendtext,$osocket,$session,$messageGUI,$hostmessageGUI,$hostsendtext,$hostsession
Opt("GUIoneventmode", 1)
$maingui = GUICreate("Messenger", 350, 350)
$filemen = GUICtrlCreateMenu("File")
$connectmen = GUICtrlCreateMenuItem("Connect directly", $filemen)
GUISetOnEvent($GUI_EVENT_CLOSE, "anexit")
GUICtrlSetOnEvent($connectmen, "Connect")
GUISetState(@SW_SHOW)
TCPStartup()
$lissock = TCPListen ($localhost, $port)

While 1
    $osocket = TCPAccept($lissock)
    If $osocket <> -1 Then
        While 1
            $cleintIp = TCPRecv($osocket, 512)
            If $cleintIp <> "" Then ExitLoop
        WEnd
        $hostmessageGUI = GUICreate("Session with " & $cleintIp, 350, 250)
        GUICtrlSetOnEvent(GUICtrlCreateButton("Host Send", 265, 190), "hostsendtext")
        $hostsession = GUICtrlCreateInput("Session History", 1, 1, 225, 185)
        $hostsendtext = GUICtrlCreateInput("enter text here", 1, 190, 225, 35)
        GUISetState(@SW_SHOW)
            While 1
                $incomming = TCPRecv($osocket, 512)
                If $incomming <> "" Then GUICtrlSetData($hostsession, GUICtrlRead($hostsession) & $cleintIp & ":" & $incomming)
            WEnd
    EndIf
WEnd

Func hostsendtext()
    TCPSend($osocket, GUICtrlRead($hostsendtext))
    GUICtrlSetData($hostsession, GUICtrlRead($hostsession) & $localhost & ":" & GUICtrlRead($hostsendtext))
    GUICtrlSetData($hostsendtext,"")
EndFunc

Func connect()
    $ipto = InputBox("Connect to who?", "Enter Ip Adress")
    If @error = 1 Then Return
    $consock = TCPConnect($ipto, $port)
    If $consock = -1 Then
        MsgBox(0, "Error: " & @error, "Could not connect to server")
        Return
    EndIf
    TCPSend($consock, $localhost)
    $messageGUI = GUICreate("Session with " & $ipto, 350, 250)
;Problem starts here++++++++++  
    GUICtrlSetOnEvent(GUICtrlCreateButton("Client Send", 265, 190), "testok");trying to call 'clientsend' but the point is it doesn't call any function
; the send buton is made...
    $session = GUICtrlCreateInput("Seesion History", 1, 1, 225, 185)
    $sendtext = GUICtrlCreateInput("enter text here", 1, 190, 225, 35)
    GUISetState(@SW_SHOW)
    While 1;;;while in this loop the send button is pressed...
        $rec = TCPRecv($consock, 512)
        If $rec <> "" Then GUICtrlSetData($session, GUICtrlRead($session) & $ipto & ":" & $rec)
    WEnd
    
EndFunc
;This function is not called....
Func testok()
    MsgBox(0,"OK","called")
    Return
EndFunc

Func clientsend()
    TCPSend($consock, GUICtrlRead($sendtext))
    GUICtrlSetData($session, GUICtrlRead($session) & $localhost & ":" & GUICtrlRead($sendtext))
    GUICtrlSetData($sendtext,"")
EndFunc

Func anexit()
    Exit
EndFunc

The comments explain the problem at the bottom...

I have no idea what the problem is... anyone see anything?

topost.au3

Edited by evilertoaster
Link to comment
Share on other sites

I'm not sure what you mean...the GUI for the conect function is not made inside a while loop. The one for the host is but if you notice it never loops at all becuase it gets stuck in a sub while loop indefintaly...the whole while loop for that isn't even nessary...interstingly enough THIS is the loop that works, not the connect funtion which is where the trouble is....look at the comments in the connect function....

Edited by evilertoaster
Link to comment
Share on other sites

like this

****** NOT TESTED *********

but, it is the right way

#include <GUIConstants.au3>
Global $port = 9999
Global $localhost = @IPAddress1
Global $ipto,$sendtext,$consock,$hostsendtext,$osocket,$session,$messageGUI,$hostmessageGUI,$hostsendtext,$hostsession
Opt("GUIoneventmode", 1)
$maingui = GUICreate("Messenger", 350, 350)
$filemen = GUICtrlCreateMenu("File")
$connectmen = GUICtrlCreateMenuItem("Connect directly", $filemen)
GUISetOnEvent($GUI_EVENT_CLOSE, "anexit")
GUICtrlSetOnEvent($connectmen, "Connect")
GUISetState(@SW_SHOW)
TCPStartup()
$lissock = TCPListen ($localhost, $port)

; put it here
$hostmessageGUI = GUICreate("Session with " & $cleintIp, 350, 250)
        GUICtrlSetOnEvent(GUICtrlCreateButton("Host Send", 265, 190), "hostsendtext")
        $hostsession = GUICtrlCreateInput("Session History", 1, 1, 225, 185)
        $hostsendtext = GUICtrlCreateInput("enter text here", 1, 190, 225, 35)
GUISetState(@SW_SHOW)


While 1
    $osocket = TCPAccept($lissock)
    If $osocket <> -1 Then
        While 1
            $cleintIp = TCPRecv($osocket, 512)
            If $cleintIp <> "" Then ExitLoop
        WEnd
      ; show it here... dont keep creating the gui
        GUISetState(@SW_SHOW, $maingui )
            While 1
                $incomming = TCPRecv($osocket, 512)
                If $incomming <> "" Then GUICtrlSetData($hostsession, GUICtrlRead($hostsession) & $cleintIp & ":" & $incomming)
            WEnd
    EndIf
WEnd

8)

NEWHeader1.png

Link to comment
Share on other sites

1. You keep saying to fix the "hostsendmessageGUI" , this is not the problem. In fact it is the only one that works.

2. You keep saying It recreates the GUI in the while loop. It doesnt... look two lines below your "; show it here... dont keep creating the gui" comment.... the first time though it stays in THAT while loop and never leaves. The GUI is never recreated.

3. The problem is in the conenct function... it has nothing to do with anything else besides that...

Func connect()
    $ipto = InputBox("Connect to who?", "Enter Ip Adress")
    If @error = 1 Then Return
    $consock = TCPConnect($ipto, $port)
    If $consock = -1 Then
        MsgBox(0, "Error: " & @error, "Could not connect to server")
        Return
    EndIf
    TCPSend($consock, $localhost)
    $messageGUI = GUICreate("Session with " & $ipto, 350, 250)

;Problem starts here++++++++++ 
   
    GUICtrlSetOnEvent(GUICtrlCreateButton("Client Send", 265, 190), "testok");trying to call 'clientsend' but the point is it doesn't call any function

; the send buton is made...

    $session = GUICtrlCreateInput("Seesion History", 1, 1, 225, 185)
    $sendtext = GUICtrlCreateInput("enter text here", 1, 190, 225, 35)
    GUISetState(@SW_SHOW)

    While 1;;;while in this loop the send button is pressed...
        $rec = TCPRecv($consock, 512)
        If $rec <> "" Then GUICtrlSetData($session, GUICtrlRead($session) & $ipto & ":" & $rec)
    WEnd
    
EndFunc


;This function is not called....
Func testok()
    MsgBox(0,"OK","called")
    Return
EndFunc

Func clientsend()
    TCPSend($consock, GUICtrlRead($sendtext))
    GUICtrlSetData($session, GUICtrlRead($session) & $localhost & ":" & GUICtrlRead($sendtext))
    GUICtrlSetData($sendtext,"")
EndFunc

Unless I am seriusly missunderstanding your example...i think you are seriusly misunderstanding my problem...

(Feel free to slap me in the face if your right and i'm just not seeing it.... but I don't see the realtion to the example you posted....)

Edited by evilertoaster
Link to comment
Share on other sites

I appreciate your help with this

I rewrote the code to make it a bit more readable for you...

Outlandish as your recommendations seemed to me I did try them but they didn't work...

GUICtrlSetOnEvent(GUICtrlCreateButton("Host Send", 265, 190)

GUICtrlSetOnEvent(-1, "hostsendtext")

That first line didnt compile well because it's missing a closing ')' and the "FunctionName" argument of the function maybe you meant -

GUICtrlSetOnEvent(GUICtrlCreateButton("Host Send", 265, 190), hostsendtext)
GUICtrlSetOnEvent(-1, "hostsendtext")

The second line was fine I guess but I dont see any reference to what -1 means as a GUICtrl ID but I'm assuming it means that last CTRL ID created?

In any case the client send function is still not being called by pressing the client send button

What do I do next?

Here's the new code-

;Variables and Options
;#######################################################################
;#######################################################################
#include <GUIConstants.au3>
Global $port = 9999
Global $localhost = @IPAddress1
Global $ipto,$sendtext,$consock,$hostsendtext,$osocket,$session,$messageGUI,$hostmessageGUI,$hostsendtext,$hostsession

If iniread (@scriptdir & "\config.ini","Settings","port","notthere") <> "notthere" then $port=iniread (@scriptdir & "\config.ini","Settings","port","notthere")
If iniread (@scriptdir & "\config.ini","Settings","localhost","notthere") <> "notthere" then $localhost=iniread (@scriptdir & "\config.ini","Settings","localhost","notthere")

Opt("GUIoneventmode", 1)
;########################################################################
;########################################################################

;GUI's

;#########################################################################
;#########################################################################

;Main GUI****
$maingui = GUICreate("Messenger", 350, 350)
$filemen = GUICtrlCreateMenu("File")
$connectmen = GUICtrlCreateMenuItem("Connect directly", $filemen)
GUISetOnEvent($GUI_EVENT_CLOSE, "anexit")
GUICtrlSetOnEvent($connectmen, "Connect")
;*****

;host send GUI *****
$hostmessageGUI = GUICreate("noname1", 350, 250)
GUICtrlSetOnEvent(GUICtrlCreateButton("Host Send", 265, 190), "hostsendtext")
GUICtrlSetOnEvent(-1,"hostsendtext")
$hostsession = GUICtrlCreateInput("Session History", 1, 1, 225, 185)
$hostsendtext = GUICtrlCreateInput("enter text here", 1, 190, 225, 35)
;*****

;Clientsend GUI*****
$messageGUI = GUICreate("noname2", 350, 250)
GUICtrlSetOnEvent(GUICtrlCreateButton("Client Send", 265, 190), "clientsend")
GUICtrlSetOnEvent(-1,"clientsend")
$session = GUICtrlCreateInput("Seesion History", 1, 1, 225, 185)
$sendtext = GUICtrlCreateInput("enter text here", 1, 190, 225, 35)
;******
;#############################################################################
;#############################################################################

;Functions

;#############################################################################
;#############################################################################
;hostsend
Func hostsendtext()
    TrayTip("Message to be sent-", GUICtrlRead($hostsendtext), 15)
    $b = TCPSend($osocket, GUICtrlRead($hostsendtext))
    If $b = 0 Then MsgBox(0, "Error: " & @error, "Could not send message")
    GUICtrlSetData($hostsession, GUICtrlRead($hostsession) & "             " & $localhost & ":" & GUICtrlRead($hostsendtext))
    GUICtrlSetData($hostsendtext,"")
EndFunc

;conenct to host
Func connect()
    $ipto = InputBox("Connect to who?", "Enter Ip Adress")
    If @error = 1 Then Return
    $consock = TCPConnect($ipto, $port)
    If $consock = -1 Then
        TrayTip("Error: " & @error, "could not connect to " & $consock & ". Trying  resolved host name of " & TCPNameToIP($consock), 15)
        $consock = TCPConnect(TCPNameToIP($consock), $port)
        If $consock = -1 Then
            MsgBox(0, "Error: " & @error, "Could not connect to server")
            Return
        EndIf
    EndIf
    TrayTip("Connected", "Connection to " & $ipto & " established", 15)
    TCPSend($consock, $localhost)
    GUISetState(@SW_SHOW,$messageGUI)
    WinSetTitle("noname2","","Session with " & $ipto)
    While 1
        $rec = TCPRecv($consock, 512)
        If $rec <> "" Then 
            MsgBox(0,"Client Recieved",$rec)
            MsgBox(0,"Client Recieved Text to be set",GUICtrlRead($session) & "         " & $ipto & ":" & $rec)
            GUICtrlSetData($session, GUICtrlRead($session) & "          " & $ipto & ":" & $rec)
        EndIf
    WEnd
    
EndFunc

;client send
Func clientsend()
    TrayTip("Message to send-", GUICtrlRead($sendtext), 15)
    $b = TCPSend($consock, GUICtrlRead($sendtext))
    If $b = 0 Then MsgBox(0, "Error: " & @error, "Could not send message")
    MsgBox(0,"Client Sent",$b & " Bytes sent")
    MsgBox(0,"Client Sent text to be set",GUICtrlRead($session) & "             " & $localhost & ":" & GUICtrlRead($sendtext))
    GUICtrlSetData($session, GUICtrlRead($session) & "              " & $localhost & ":" & GUICtrlRead($sendtext))
    GUICtrlSetData($sendtext,"")
EndFunc

;exit
Func anexit()
    Exit
EndFunc
;#############################################################################
;#############################################################################

;Startups and error checks

;#############################################################################
;#############################################################################

If TCPStartup()=0 Then 
    MsgBox(0,"Error- " & @error,"Couldn't start TCP services")
    Exit
EndIf

$lissock = TCPListen ($localhost, $port)

If $lissock = -1 Then
    MsgBox(0, "Error: " & @error, "Could not create socket on " & $localhost & ". Starting in client only mode")
    GUISetState(@SW_SHOW,$maingui)
    While 1
    WEnd
EndIf
GUISetState(@SW_SHOW,$maingui)
;#############################################################################
;#############################################################################

While 1
    $osocket = TCPAccept($lissock)
    If $osocket <> -1 Then ExitLoop
WEnd

TraySetToolTip ("Connection attempt sucessfull, waiting for IP of Client")

While 1
    $cleintIp = TCPRecv($osocket, 512)
    If $cleintIp <> "" Then ExitLoop
WEnd
    
TraySetToolTip ("Client " & $cleintIp & " has directly connected to you")
GUISetState(@SW_SHOW,$hostmessageGUI)
WinSetTitle("noname1","","Session with " & $cleintIp)

While 1
    $incomming = TCPRecv($osocket, 512)
    If $incomming <> "" Then GUICtrlSetData($hostsession, GUICtrlRead($hostsession) & "                " & $cleintIp & ":" & $incomming)
WEnd
Link to comment
Share on other sites

GUICtrlCreateButton("Host Send", 265, 190)
GUICtrlSetOnEvent(-1,"hostsendtext")

GUICtrlCreateButton("Client Send", 265, 190)
GUICtrlSetOnEvent(-1,"clientsend")

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

Ok. I've made a skimmed down version of the problem...

#include <GUIConstants.au3>
opt ("Guioneventmode",1)
;make GUI1
$GUI1=GUICreate("GUI1")
$button1=GUICtrlCreateButton("button1", 50, 50)
GUICtrlSetOnEvent($button1, "action1")

;Make GUI2
$GUI2 = GUICreate("GUI2")
$button2=GUICtrlCreateButton("button2", 50, 50)
GUICtrlSetOnEvent($button2,"action2")

;Show GUI1
GUIsetstate(@SW_SHOW,$GUI1)

;Wait around
While 1
    sleep (1000)
WEnd

;Button1 is pressed...
func action1 ()
    GUISetState(@SW_SHOW,$GUI2)
;show GUI2 then wait around
    while 1
        sleep (1000)
    WEnd
endfunc

;when button2 is pressed this function IS NOT CALLED
func action2 ()
    MsgBox(0,"OK","You pressed button2")
endfunc

Maybe that will help simplify things... Basically it's calling a function for inside a funcion

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