Jump to content
Sign in to follow this  
Jkeith247

Calling a function sent as a UDP string with variables

Recommended Posts

Jkeith247

I am trying to call a function sent to my program through a UDP string. On the client side, I am trying to send

$portsend = UDPOpen("192.168.100.87", 6969)
$string = '"RENAMEPC", "VANQUISH"'
UDPSend($portsend, $string)

On the server side, I am using the while loop to listen for the UDP command with the following loop

While 1
$funccal = UDPRecv($port, 100000)
If $funccal <> "" Then
  Call($funccal)
  ConsoleWrite($funccal & @lf)
  UDPSend($portsend, "RECEIVED" & $funccal)
  $funccal = ""
EndIf
Sleep(10)
WEnd

For some reason I can not get the function called with the arguments. I do not get it. What can I be doing wrong?

Thank you for all of your help!

Edited by Jkeith247

Share this post


Link to post
Share on other sites
BrewManNH

What does $funccal contain after it receives the data? You're only checking to see if it's empty, you're not checking to see if it contains what you want. Put a consolewrite after the UDPRecv line to see what is inside $funccal. Also, have you checked the return from UDPSend to see if it was successful?


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
Jkeith247

What does $funccal contain after it receives the data? You're only checking to see if it's empty, you're not checking to see if it contains what you want. Put a consolewrite after the UDPRecv line to see what is inside $funccal. Also, have you checked the return from UDPSend to see if it was successful?

I do have a console write line in to see what it contains. It most definetly contains everything I am sending verbatim... Basically I am not having issue sending or receiving strings... This issue is "How to send this string" so that it calls the function with the variables properly. I can call standard functions any time ex. Function() When I try to call a function ex. Function(variable) is when the problem starts.

Thanks again for the help!

Share this post


Link to post
Share on other sites
BrewManNH

Try doing a string split on the received data and use the array to call the function. Maybe something like this would work?

While 1
    $funccal = UDPRecv($port, 100000)
    If $funccal <> "" Then
        $Test = StringSplit($funccal, ",")
        Call($Test[1], $Test[2])
        ConsoleWrite($funccal & @LF)
        UDPSend($portsend, "RECEIVED" & $funccal)
        $funccal = ""
    EndIf
    Sleep(10)
WEnd

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
Jkeith247

Try doing a string split on the received data and use the array to call the function. Maybe something like this would work?

While 1
    $funccal = UDPRecv($port, 100000)
    If $funccal <> "" Then
        $Test = StringSplit($funccal, ",")
        Call($Test[1], $Test[2])
        ConsoleWrite($funccal & @LF)
        UDPSend($portsend, "RECEIVED" & $funccal)
        $funccal = ""
    EndIf
    Sleep(10)
WEnd

Unfortunately this is going to cause me a bit of an issue when there is a function that does not contain a variable. I tried to call it and it reported errors when the second field after the comma was left empty... However the Function that requried variables did work ok... Something strange I hav enoticed though is in my console I am finding digits added to the left of my strings when I console write the $funcal. So MUTE, appeared as 5060MUTE, . I guess I could write a If to check for the variable containing a comma, but this seems inefficient... What do you think?

Share this post


Link to post
Share on other sites
BrewManNH

If you're using it sometimes with and without optional parameters, you could do it this way.

Local $Parameters, $Test
While 1
    $funccal = UDPRecv($port, 100000)
    If $funccal  "" Then
        $Test = StringSplit($funccal, ","); split on the comman
        $Parameters = ""
        If $Test[0] > 1 Then
            For $I = 2 To $Test[0]
                $Parameters &= $Test[$I] & "," ;adds the comma back in.
            Next
            Call($Test[1], $Parameters)
        Else
            Call($Test[1])
        EndIf
        ConsoleWrite($funccal & @LF)
        UDPSend($portsend, "RECEIVED" & $funccal)
        $funccal = ""
    EndIf
    Sleep(10)
WEnd

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
kylomas

jkeith247,

The following may be a simpler way to do this:

SERVER

; server
UDPStartup()
OnAutoItExitRegister("Cleanup")
Local $socket = UDPBind("127.0.0.1", 65532)
If @error <> 0 Then Exit
While 1
    Local $data = UDPRecv($socket, 50)
 ConsoleWrite($data & @lf)
    If $data <> "" Then Execute($data)
    Sleep(100)
WEnd
Func Cleanup()
    UDPCloseSocket($socket)
    UDPShutdown()
EndFunc   ;==>Cleanup
Func renamepc($parm1)
 MsgBox(0,'',$parm1,1)
endfunc

CLIENT

;  client
UDPStartup()
OnAutoItExitRegister("Cleanup")
Local $socket = UDPOpen("127.0.0.1", 65532)
If @error <> 0 Then Exit
Local $n = 0
While 1
    Sleep(2000)
    $n = $n + 1
    Local $status = UDPSend($socket, "renamepc('vanquish')")
    If $status = 0 Then
        MsgBox(0, "ERROR", "Error while sending UDP message: " & @error)
        Exit
    EndIf
WEnd
Func Cleanup()
    UDPCloseSocket($socket)
    UDPShutdown()
EndFunc   ;==>Cleanup

If this is incredibly naive then mea culpa, first attempt at network code.

Good Luck,

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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  

×