Jump to content
Sign in to follow this  
Giz

Server reconnecting to client on connection loss

Recommended Posts

Giz

I have 2 questions

1.My server code can connect to the android client only once and if the connection is lost it does not reconnect and therefore it is lost forever

This is my code

#include <GUIConstants.au3>
Run("C:\Program Files (x86)\Garmin\GTN Trainer Lite\GTN\sys\sim\GTN Simulator.exe")
WinWaitActive("GTN Trainer Lite")
WinMove("GTN Trainer Lite", "", 500, 150, 550, 550)
Send("!p")
Sleep(10000)
MouseClick("left", 760, 600, 10)
Sleep(2000)
MouseClick("left", 760, 600, 10)
Sleep(2000)
MouseClick("left", 750, 595, 10)
Sleep(5000)
$ServerIP = @IPAddress1
$Port = 5000
TCPStartup()
$MainSocket = TCPListen($ServerIP, $Port)
If $MainSocket = -1 Then Exit
$ConnectedSocket = -1
Do
$ConnectedSocket = TCPAccept($MainSocket)
Until $ConnectedSocket <> -1
TrayTip("Info", "Connected", 2)
While 1
$recv = TCPRecv($ConnectedSocket, 100000)
;If @error Then ExitLoop
If $recv <> "" Then
     TrayTip("Incoming Command", $recv, 5)
     _CommandToAction($recv)
EndIf
;~   ;If $recv = 3 Then Exit
WEnd
TrayTip("info", "Closing socket", 5)
TCPCloseSocket($ConnectedSocket)
TrayTip("info", "TCP shutdown", 5)
TCPShutdown()
Exit
Func _CommandToAction($command)
If StringInStr($command, "left") Then
     TrayTip("info", $command, 5)
     MouseClick("left", 990, 620,1, 10)
EndIf
If StringInStr($command, "right") Then
     TrayTip("info", $command, 5)
     MouseClick("left", 1010, 620,1, 10)
EndIf
EndFunc ;==>_CommandToAction

How do I modify the program to reconnect to the client even on loss of connection?

2. The received data from the client has a new line character, how do i remove it? I tried using

stringReplace($command,@CRLF,"")

but it doesn't work

Any help is appreciated. Very new to AutoIt and to this forum

Any help please?

Edited by Giz

Share this post


Link to post
Share on other sites
water

You already asked this on the GUI forum.

Please don't open a second thread, just ask a Mod to move the thread if it's on the wrong forum.

  • Like 1

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Giz

Ok, Thank you. I just wrote to them. I am so sorry. Very new here, so I couldn't figure it out. I have few modifications in the question though. I have asked an additional question here

Edited by Giz

Share this post


Link to post
Share on other sites
Giz

Answer for the second question

Since the client was android and it is linux operations i had to use

$command=stringReplace($command,@LF,"") instead of stringReplace($command,@CRLF,"")

Share this post


Link to post
Share on other sites
Giz

@ Water, Thank you for correcting me on not opening new threads of the same question. The moderator deleted my question in GUI forum. :) Thanks again

Share this post


Link to post
Share on other sites
Giz

Answer to the first question

;Start the TCP Services
TCPStartup()
$ConnectedSocket = -1
TrayTip("info", "Connecting", 5)
;Create a socket listening for an incoming connection and return main socket identifier
$MainSocket = TCPListen($ServerIP, $Port)
;On failure return -1 and exit the program
If $MainSocket = -1 Then Exit
While 1
    TrayTip("info","Listening to port",2)
    ;Accept the connection from the client
    Do
        $ConnectedSocket = TCPAccept($MainSocket)
    Until $ConnectedSocket <> -1
    ;When connected to client receive the data from the client until the connection is lost
    While 1
        ;Receive the data from teh connected client
        $recv = TCPRecv($ConnectedSocket, 2048)
        ;If there is an error on loss of connection exit the loop to reconnect
        If @error Then ExitLoop
        TrayTip("info","Client Connected",2)
        ;If the received data is not null call the function _CommandToAction
        If $recv <> "" Then
            TrayTip("Incoming Command", $recv, 5)
            ;check if the command is to disconnect client
            If StringInStr($recv, "Nocommand") Then ExitLoop
            ;This function call converts the command received to the necessary actions
            _CommandToAction($recv)
        EndIf

    WEnd
    TrayTip("info", "Closing socket", 5)
    If $ConnectedSocket <> -1 Then
        ;Close the existing connection to reconnect to client
        TCPCloseSocket($ConnectedSocket)
    EndIf
    ;Assign -1 to the variable
    $ConnectedSocket = -1
WEnd
TrayTip("info", "TCP shutdown", 2)
;Close the server socket
TCPCloseSocket($MainSocket)
;Shutdown the TCP connection
TCPShutdown()
;Exit the application
Exit

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  

×