DrAhmed Posted March 21, 2016 Share Posted March 21, 2016 Hi all I've been working on a code of Client-Server Connection for a while (Like Teamviewer) , It is working good except I am having this error after few minutes : My code : expandcollapse popupTCPStartup() Local $ConnectedSocket Local $My_IP = "127.0.0.1" Local $My_PORT = '5000' Local $UserID = "MyID" ; Connect Connect($My_IP, $My_PORT) While 1 If TCPConnect($My_IP, $My_PORT) <> -1 Then $recv = TCPRecv($ConnectedSocket, 2048) $RecvSpl = StringSplit($recv, "|") Switch $RecvSpl[1] Case "order1" ; Do something MsgBox(64,"Success","You are now connected to the client") AddLogToClinet("My server is Online", $UserID, "Green") EndSwitch Else $Connected = False Connect($IP, $My_PORT) EndIf Sleep(100) WEnd ; Connect To the Client Func Connect($IP, $Port) TCPStartup() $ConnectedSocket = TCPConnect($IP, $Port) If @error Then Connect($IP, $My_PORT) ; <<<<<<<<<<<<<<< I think this is the problem Else $Connected = True EndIf EndFunc ;==>Connect ; Send To Client Func SockSend($Cmd, $Text) TCPSend($ConnectedSocket, $Cmd & $SockSpl & $Text & $PocketSpl, 4) EndFunc ;==>SockSend ; Add Log To Server Func AddLogToClinet($Data, $UserID, $Color) SockSend("AddLog", $Data & $SockSpl & $UserID & $SockSpl & $Color) EndFunc ;==>AddLogToClinet I've read the article about Recursion in wiki https://www.autoitscript.com/wiki/Recursion and tried to make my connection function like thins ; Connect To the Client Func Connect($IP, $Port) TCPStartup() $ConnectedSocket = TCPConnect($IP, $Port) If @error Then Return False Else $Connected = True EndIf EndFunc ;==>Connect but in this way after few minutes I loose the connection between server and client , I have also read other posts about this problem and I've edited my code several times trying to fix it. However I didn't find the solution yet what do you suggest to me ? Link to comment Share on other sites More sharing options...
InunoTaishou Posted March 21, 2016 Share Posted March 21, 2016 (edited) Func Connect($IP, $Port) Local $iConnectAttempts = 0 TCPStartup() Do $ConnectedSocket = TCPConnect($IP, $Port) Sleep(1000) Until ($ConnectedSocket > 0 or $iConnectAttempts > 10) Return ($ConnectedSocket > 0 ? True : SetError(1, 0, 0)) EndFunc ;==>Connect Edited March 21, 2016 by InunoTaishou Changed the return DrAhmed 1 Link to comment Share on other sites More sharing options...
DrAhmed Posted March 21, 2016 Author Share Posted March 21, 2016 34 minutes ago, InunoTaishou said: Func Connect($IP, $Port) Local $iConnectAttempts = 0 TCPStartup() Do $ConnectedSocket = TCPConnect($IP, $Port) Sleep(1000) Until ($ConnectedSocket > 0 or $iConnectAttempts > 10) Return (@Error ? SetError(@Error, 0, 0) : True) EndFunc ;==>Connect I will try it out and see if it fixes the problem Link to comment Share on other sites More sharing options...
DrAhmed Posted March 21, 2016 Author Share Posted March 21, 2016 @nunoTaishou Thank you very much , your code fixed the error I had but, It loose the connection between server and Client after few minutes Link to comment Share on other sites More sharing options...
DrAhmed Posted March 21, 2016 Author Share Posted March 21, 2016 any help is welcomed even if it is another method for connection I winder also if we can use WCF instead of sockets for server-client connections in Autoit because I read that WCF is more efficient and might be a better option as its very flexible Link to comment Share on other sites More sharing options...
InunoTaishou Posted March 21, 2016 Share Posted March 21, 2016 There is a tcp timeout set by default in autoit Quote Opt("TCPTimeout", 100) Defines the time before TCP functions stop if no communication. Time in milliseconds before timeout (default=100). Could try increasing the delay. And periodically check that your server and client are still connected. If not then reconnect. Maybe send a packet of data to each other just to keep the connection up? I haven't really used TCP so I don't know much about it. Link to comment Share on other sites More sharing options...
DrAhmed Posted March 21, 2016 Author Share Posted March 21, 2016 49 minutes ago, InunoTaishou said: There is a tcp timeout set by default in autoit Could try increasing the delay. And periodically check that your server and client are still connected. If not then reconnect. Maybe send a packet of data to each other just to keep the connection up? I haven't really used TCP so I don't know much about it. Thank very much you tried at least Link to comment Share on other sites More sharing options...
DrAhmed Posted March 22, 2016 Author Share Posted March 22, 2016 recursion problem is solved . you can close topic now admin . Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now