Sign in to follow this  
Followers 0
mark089

TCPSend or TCPrecv problem

5 posts in this topic

Hi all,

I want to make a remote control for my desktop computer. A client GUI on my laptop should control a server on my desktop.

Now I found a nice script online and spent a whole day trying to make it work, but I'm stuck at a certain point. The connection is being established fine, just the TCPSend command in the client or the TCPRecv command on the server does not work.

Would be great, if someone could look at the code and find the bug, that is causing the malfunctioning.

So here is the code:

;### Server
TCPStartup()
$ipAddress = @IPAddress1
$portAddress = 666
MsgBox(4096, "", "Listening on IP: " & $ipAddress & " and PORT: " & $portAddress)

$MainSocket = TCPListen($ipAddress, $portAddress)

While 1
    Do
        $ConnectedSocket = TCPAccept($MainSocket)
    Until $ConnectedSocket > 0
Beep(500, 200) ; connected!

    $err = 0
    Do
        $msg = TCPRecv($ConnectedSocket, 512)
        $err = @error
        If StringLen($msg) Then ;If we receive a message
            Beep(300, 200)
            If StringInStr($msg, "SEND FUNCTION:") Then             ;If the message contains the text "SEND FUNCTION:"
                $function = StringReplace($msg, "SEND FUNCTION: ", "");Delete the "SEND FUNCTION: " part so we $function
                Call($function)                                       ;"run" the function that was received
            EndIf
            If StringInStr($msg, "EXIT:") THEN                        ;If the message contains "EXIT:"
                TCPShutdown()                                         ;Stop connection
                Exit                                                  ;Exit script.
            EndIf
        EndIf
    Until $err ;We basically keep on "receiving" information until TCPShutdown is called, which only happens if
    TCPCloseSocket($ConnectedSocket) ;the other user presses "End Session"
WEnd

Func DefaultFunction()
    MsgBox(0, "", "hello!")
EndFunc


;### Client
#include <GUIConstants.au3>
#include <GUIConstantsEx.au3>
Local $input = 0, $function = 0, $exit = 0, $msg = 0, $message = 0, $iError

TCPStartup()
$ipAddress = InputBox("","Enter ip address to connect with:",@IPAddress1)
If $ipAddress = "" Then $ipAddress = @IPAddress1
$portAddress = 666
$connectedSocket = TCPConnect($ipAddress, $portAddress) ;Connect with the other computer
If @error OR $connectedSocket < 1 Then                  ;If there was a problem connecting, notify the user.
    MsgBox(0, "GUI Event", "Could not connect to IP: " & $ipAddress & " and PORT: " & $portAddress)
    Exit
EndIf

GUICreate("Test", 120, 60)                                         ;Create a GUI
$input    = GUICtrlCreateInput("DefaultFunction", 0, 0, 120, 20)   ;Create Controls within the GUI
$function = GUICtrlCreateButton("Send The Function", 0, 20, 120, 20)
$exit     = GUICtrlCreateButton("End Session", 0, 40, 120, 20)
GUISetState(@SW_SHOW)

While 1
    $msg = GUIGetMsg()
    ;$portAddress = 13013
    Select
        Case $msg = $function                                 ;If the user clicks on "Send The Function" button,
            $message = "SEND FUNCTION: " & GUICtrlRead($input);Send to the other computer text
            TCPSend($connectedSocket, $message)                ;See other script to see what happens once it receives the text
        Case $msg = $exit        ;If "End Session" button is clicked
            $message = "EXIT:"   ;Send the other computer text, this will exit the other computers script
            TCPSend($connectedSocket, $message)
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
    EndSelect
WEnd
TCPShutdown()

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

your ConnectedSocket shud be compared to some type of  diffrent than - 1

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

and your "do $err loop" shud be formed diffrently, it exits loop to early, note that TCPRecv if recived nothing will return empty string and set an @error flag

maybe TCPRecv error isnt a good way for you to tell server that client dcced

Look @ my signature if you wanna read some more about tcp

Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 

Share this post


Link to post
Share on other sites

Thank

your ConnectedSocket shud be compared to some type of  diffrent than - 1

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

and your "do $err loop" shud be formed diffrently, it exits loop to early, note that TCPRecv if recived nothing will return empty string and set an @error flag

maybe TCPRecv error isnt a good way for you to tell server that client dcced

Look @ my signature if you wanna read some more about tcp

 

Thank you for the info. I changed it to

Until $ConnectedSocket > -1

The thing about the error loop, I did not really understand. I thought the $err loop already exits on the very end. Can you explain please?

 

Look @ my signature if you wanna read some more about tcp

I already found that one and read it. That's some grerat info.

The problem with my code doesn't seem to be the "Until $ConnectedSocket > 0", as the connection is being properly established. This is, what the "BEEP" proofs to me. Also the error thing, as there appears no error message. Must be something different. Can you check?

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

remove your While 1 loop on server and you will see that your script is exiting "do untill" loop to early

you can try to do this to recive one (probably onlu one) msg (coz of badly formated server loop dont think you can recive on server more than one msg correclty in this testing situation)

so instead

Until $err ;We basically keep on "receiving" information until TCPShutdown is called, which only happens if

Try to change to

Until $err > 0 ;We basically keep on "receiving" information until TCPShutdown is called, which only happens if

 

Explanation for your loop and whats wrong with it,

TCPRecv will geave out error if it cant pull anyting, so your $err is under @error from the start on first loop right after client is connected (coz you still did not send msg from clinet and your server first loop for trying to recive msg started), so after first loop script will get out from reciving "do untill" loop automaticly and itl go back to your "TCPAccept do untill" loop cos of influance of yout "While 1" main loop.

  

So tellme do you understand now about what im telling you is wrong in that loop? ;)

Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 

Share this post


Link to post
Share on other sites

remove your While 1 loop on server and you will see that your script is exiting "do untill" loop to early

you can try to do this to recive one (probably onlu one) msg (coz of badly formated server loop dont think you can recive on server more than one msg correclty in this testing situation)

so instead

Until $err ;We basically keep on "receiving" information until TCPShutdown is called, which only happens if

Try to change to

Until $err > 0 ;We basically keep on "receiving" information until TCPShutdown is called, which only happens if

 

Explanation for your loop and whats wrong with it,

TCPRecv will geave out error if it cant pull anyting, so your $err is under @error from the start on first loop right after client is connected (coz you still did not send msg from clinet and your server first loop for trying to recive msg started), so after first loop script will get out from reciving "do untill" loop automaticly and itl go back to your "TCPAccept do untill" loop cos of influance of yout "While 1" main loop.

  

So tellme do you understand now about what im telling you is wrong in that loop? ;)

 

Great! Thanks a lot, now I understand, what you meant! :-)

Meanwhile I've tried to change the loop to "until 1 = 2" instead of "until $err = 0" and this also worked, but of course the loop did not make much sence. But now the solution is perfect. So thanks again! :-)

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