Sign in to follow this  
Followers 0
Jaysen

Issue with TCP and Switch Statement

13 posts in this topic

I have a VB.Net program I wrote for my Windows Mobile phone that will send a Logoff, Reboot, or Shutdown command to a AutoIT script running on multiple computers at the Charter School I work for. The AutoIT script (below) receives the command and depending on which command it is, do a different shutdown method. Whenever the $recv variable is used on a Switch, Select, or If statements, none of the conditions are executed, but the program closes without any errors and I don't receive the TCP message sent back on my mobile device. The very bottom TCPSend that is commented out does get sent back to my mobile device if I uncomment it.

Is there something I'm missing, or does anyone else have any suggestions?

Local $szIPADDRESS = @IPAddress1
Local $nPORT = 63000
Local $MainSocket, $ConnectedSocket
Local $recv

TCPStartup()

$MainSocket = TCPListen($szIPADDRESS, $nPORT)

If $MainSocket = -1 Then Exit

$ConnectedSocket = -1

Do
    $ConnectedSocket = TCPAccept($MainSocket)
Until $ConnectedSocket <> -1

$recv = TCPRecv($ConnectedSocket, 2048)

If $recv <> "" Then 
    MsgBox(0, "", "Received > " & $recv)
        
;#cs - Test 1
    Switch $recv
    Case "###LOGOFF###"
        TCPSend($ConnectedSocket, "Log Off Intiated")
        MsgBox(0, "", "Log Off Intiated")
    Case "###REBOOT###"
        TCPSend($ConnectedSocket, "Reboot Intiated")
        MsgBox(0, "", "Reboot Intiated")
    Case "###SHUTDOWN###"
        TCPSend($ConnectedSocket, "Shut Down Intiated")
        MsgBox(0, "", "Shut Down Intiated")
    EndSwitch
;#ce
        
    #cs - Test 2
    Select
    Case $recv = "###LOGOFF###"
        TCPSend($ConnectedSocket, "Log Off Intiated")
        MsgBox(0, "", "Log Off Intiated")
    Case $recv = "###REBOOT###"
        TCPSend($ConnectedSocket, "Reboot Intiated")
        MsgBox(0, "", "Reboot Intiated")
    Case $recv = "###SHUTDOWN###"
        TCPSend($ConnectedSocket, "Shut Down Intiated")
        MsgBox(0, "", "Shut Down Intiated")
    EndSelect
    #ce
        
    #cs - Test 3
    If $recv = "###LOGOFF###" Then
        TCPSend($ConnectedSocket, "Log Off Intiated")
        MsgBox(0, "", "Log Off Intiated")
    EndIf
    If $recv = "###REBOOT###" Then
        TCPSend($ConnectedSocket, "Reboot Intiated")
        MsgBox(0, "", "Reboot Intiated")
    EndIf
    If $recv = "###SHUTDOWN###" Then
        TCPSend($ConnectedSocket, "Shut Down Intiated")
        MsgBox(0, "", "Shut Down Intiated")
    EndIf
    #ce
        
    If $ConnectedSocket <> -1 Then TCPCloseSocket($ConnectedSocket)
EndIf

TCPShutdown()

Share this post


Link to post
Share on other sites



Hi there,

As far as i can see you have a bad loop :)

Do
    $ConnectedSocket = TCPAccept($MainSocket)
Until $ConnectedSocket <> -1

You'll never check anything, unless you redirect within the loop for a func

Cheers


Old Scriptology

Visual Ping 1.8 - Mass Ping Program with export to txt delimited.

Desktop 2 RGB and YMCK - Pick a color in the desktop and get the RGB and YMCK code.

Desktop 2 RGB - Pick a color in the desktop and get the RGB code.

ShootIT 1.0 - Screen Capture full and partial screen

[font="'Arial Black';"]Remember Remember The Fifth of November.[/font]

Share this post


Link to post
Share on other sites

Thanks for the quick reply. That code is slightly modified version of the example in the Online Documentation for TCPAccept. All I really did was take out the GUI and send a reply back. It runs through the entire program, but anything after any of the decisions in the different test cases isn't executed.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Hi again,

I digged deeply and i tested your script and created a send script to and every thing went fine:

$send = "###LOGOFF###"
TCPStartup()

$go = TCPConnect(@IPAddress1, 63000)
TCPSend($go, $send)

I advice you to check if the sender is really sending something!

Cheers

Edited by November

Old Scriptology

Visual Ping 1.8 - Mass Ping Program with export to txt delimited.

Desktop 2 RGB and YMCK - Pick a color in the desktop and get the RGB and YMCK code.

Desktop 2 RGB - Pick a color in the desktop and get the RGB code.

ShootIT 1.0 - Screen Capture full and partial screen

[font="'Arial Black';"]Remember Remember The Fifth of November.[/font]

Share this post


Link to post
Share on other sites

I don't think you quite understand what I'm having a issue with...

I have a program I wrote for my Windows Mobile phone that sends a command to the AutoIT Script and assigns that command to the $recv variable. The script needs to pass that variable to a switch or select statement and take a different action depending on what command was sent. The AutoIT script receives the command sent from my phone. I put a messagebox there to show that the AutoIT script receives the command, which it does. What's supposed to happen is the AutoIT script is supposed reply with the appropriate message and showing a messagebox to confirm that it was sent. But instead, the script just closes without errors, doesn't send a reply back and doesn't show any more message boxes.

Share this post


Link to post
Share on other sites

And i don't think that you didnt quite understand too...

I compiled your script and run it, and made that one to send a command (###LOGOFF###)... voilá... it was sucessfull, so i'm guessing that the code you made it in your mobile isn't sending nothing to the server side.

Try to run that script that i posted and see for yourselve... ony one thing... i made this test in one computer only... if you can test it in 2 computers seperated... the better.

Cheers m8


Old Scriptology

Visual Ping 1.8 - Mass Ping Program with export to txt delimited.

Desktop 2 RGB and YMCK - Pick a color in the desktop and get the RGB and YMCK code.

Desktop 2 RGB - Pick a color in the desktop and get the RGB code.

ShootIT 1.0 - Screen Capture full and partial screen

[font="'Arial Black';"]Remember Remember The Fifth of November.[/font]

Share this post


Link to post
Share on other sites

The issue is not whether or not the AutoIT script receives the command, because I know it does, it works from the program I made for my phone. I know the receive on my phone works because I tested a simple send/receive without a switch statement and it works fine. The issue is that no decision is made in those conditional statements and nothing is sent back to my phone.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

The issue is not whether or not the AutoIT script receives the command, because I know it does, it works from the program I made for my phone. I know the receive on my phone works because I tested a simple send/receive without a switch statement and it works fine. The issue is that no decision is made in those conditional statements and nothing is sent back to my phone.

I see... so you want a callback to the sender!!!

That part is missing in the code.

You have to have the ip of the sender and send the response to the sender.

The two peers have to be client and server at the same time :)

Basicaly you'll have to get the sender's IP and a TCPsend to him.

In your code you are sending a package to nowhere.

I hope i made my self clear :lmao:

Edited by November

Old Scriptology

Visual Ping 1.8 - Mass Ping Program with export to txt delimited.

Desktop 2 RGB and YMCK - Pick a color in the desktop and get the RGB and YMCK code.

Desktop 2 RGB - Pick a color in the desktop and get the RGB code.

ShootIT 1.0 - Screen Capture full and partial screen

[font="'Arial Black';"]Remember Remember The Fifth of November.[/font]

Share this post


Link to post
Share on other sites

You are still not listening. There is code to send a message back to the sender, it's in the switch and select statements. And you don't have to have a specific IP address, you just use the connected socket. I have no issue sending a reply when the script doesn't have to make a decision. When I try and use a switch or select statement to determine what command was sent and what to do with that command, nothing is sent back and the script doesn't execute any statements within that condition.

Anything between one case and the next does not work. None of the cases are executed. I made three seperate tests one using switch, another using select, and last using if statements, none of them work. I don't know how much more simple I can make it.

I know for a fact that there is nothing wrong with the program on my phone, nothing wrong with the send, and nothing wrong with the receive, the ONLY issue is with the decisions.

Share this post


Link to post
Share on other sites

Ok... now i get the all picture.

I beliave that you have to use a different port to send back.

I made a little test with te same port and the error was 10054 (connection ended by error), after that i changed the second port to 63001 and all was ok.

Try that :)

Cheers


Old Scriptology

Visual Ping 1.8 - Mass Ping Program with export to txt delimited.

Desktop 2 RGB and YMCK - Pick a color in the desktop and get the RGB and YMCK code.

Desktop 2 RGB - Pick a color in the desktop and get the RGB code.

ShootIT 1.0 - Screen Capture full and partial screen

[font="'Arial Black';"]Remember Remember The Fifth of November.[/font]

Share this post


Link to post
Share on other sites

I appreciate you trying to help, but just please stop. You can't understand what my problem is and keep giving me these off the wall answers that have nothing to do with what I'm trying to accomplish.

Share this post


Link to post
Share on other sites

I appreciate you trying to help, but just please stop. You can't understand what my problem is and keep giving me these off the wall answers that have nothing to do with what I'm trying to accomplish.

LOL

Sure do friend :)

But it funny to have someone asking not to help :lmao:

Nevertheless, i tested all what i said... just for the record :think:

Good luck with your problem resolution!

Cheers m8


Old Scriptology

Visual Ping 1.8 - Mass Ping Program with export to txt delimited.

Desktop 2 RGB and YMCK - Pick a color in the desktop and get the RGB and YMCK code.

Desktop 2 RGB - Pick a color in the desktop and get the RGB code.

ShootIT 1.0 - Screen Capture full and partial screen

[font="'Arial Black';"]Remember Remember The Fifth of November.[/font]

Share this post


Link to post
Share on other sites

Got my problem fixed. Whenever I passed the $recv variable to a switch or select statement, nothing happened. I eliminated the need for a switch by just changing the values sent from the server to a 4 (Force Logoff), 5 (Force Shut Down), and 6 (Force Restart) and then passed the $recv variable straight to the AutoIT Shutdown command. Now everything works fine. The script sends a reply to my phone letting it know it received the command and then takes the appropriate action (Logoff, Shut Down, or Restart).

If $recv <> "" Then 
    TCPSend($ConnectedSocket, "###OK###")
    Shutdown($recv)
    If $ConnectedSocket <> -1 Then TCPCloseSocket($ConnectedSocket)
EndIf

Someone can close this thread.

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  
Followers 0