Jump to content

Server reconnecting to client on connection loss


Giz
 Share

Recommended Posts

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
Link to comment
Share on other sites

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.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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,"")

Link to comment
Share on other sites

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