frank10 Posted March 2, 2019 Posted March 2, 2019 (edited) I'm testing TCPConnect, TCPSend, TCPListen with server client through Internet. Server has a WAN Ip and is connected to a router with the correct Port forward rule. Windows Firewall has incoming rule on that port. Client is a laptop tethered with smartphone (so NATted, I can't ping on its WAN IP), but I use it only to send, so it should work. So, I launch the server au3 and then I launch the client script that tries to connect and send something: sometimes it works, but most of the times TCPConnect can't get a socket, it receives -1, but @error remains =0. I tried also from the laptop browser (with 87.15.110.xx:1342) and it always works, so the Internet connection is working: it's a problem with autoit script. If I test with the laptop connected to the LAN, it always works. server: expandcollapse popup#include <GUIConstantsEx.au3> #include <MsgBoxConstants.au3> #include <Array.au3> ; SERVER TCPStartup() Dim $Socket_Data[1] $Socket_Data[0] = 0 Local $iPort = 1342 ; Port used for the connection. Local $sIPAddress = "192.168.1.2" ; This IP Address only works for testing on your own computer. $Listen = TCPListen($sIPAddress, 1342) If @error Then ConsoleWrite('!-->['&@Hour&":"&@MIN&":"&@SEC&'] TCPListen error. ( ' & @error & ' )' & @CRLF) Exit EndIf While 1 For $x = $Socket_Data[0] To 1 Step -1 $Recv = TCPRecv($Socket_Data[$x], 100000) If $Recv Then MsgBox(0, 'Client Number ' & $x & ' with connected socket identifier ' & $Socket_Data[$x], "["&@Hour&":"&@MIN&":"&@SEC&"]"&' Received msg from Client: ' & @CRLF & $Recv, 2) TCPSend($Socket_Data[$x], 'bye') TCPCloseSocket($Socket_Data[$x]) _ArrayDelete($Socket_Data, $x) $Socket_Data[0] -= 1 EndIf Next _Accept() WEnd Func _Accept() Local $Accept = TCPAccept($Listen) If $Accept <> -1 Then _ArrayAdd($Socket_Data, $Accept) $Socket_Data[0] += 1 EndIf EndFunc ;==>_Accept TCPShutdown() ; Close the TCP service. client: #include <GUIConstantsEx.au3> #include <MsgBoxConstants.au3> TCPStartup() ; Start the TCP service. ; Register OnAutoItExit to be called when the script is closed. OnAutoItExitRegister("OnAutoItExit") Local $iPort = 1342 ; Port used for the connection. Local $sIPAddress = "87.15.110.xx" ; my server WAN IP computer. MyTCP_Client($sIPAddress, $iPort) Exit Func MyTCP_Client($sIPAddress, $iPort) ; Assign a Local variable the socket and connect to a listening socket with the IP Address and Port specified. Local $iSocket = TCPConnect($sIPAddress, $iPort) ConsoleWrite("no Socket err:" & @error & " ex:" & @extended) TCPSend($iSocket, "test") ; If an error occurred display the error code and return False. If @error Then $iError = @error ConsoleWrite("no Send") Return False EndIf ; Close the socket. TCPCloseSocket($iSocket) EndFunc ;==>MyTCP_Client Func OnAutoItExit() TCPShutdown() ; Close the TCP service. EndFunc ;==>OnAutoItExit Edited March 2, 2019 by frank10
frank10 Posted March 3, 2019 Author Posted March 3, 2019 (edited) I found the solution: in the client side it is needed this timeout: AutoItSetOption('TCPTimeout', 200) Fixed. I would add this into Help file, or better change the default that is 100... With 150 still it doesn't work, at least 200! Edited March 3, 2019 by frank10
Nine Posted March 3, 2019 Posted March 3, 2019 On 3/2/2019 at 12:32 PM, frank10 said: it receives -1, but @error remains =0. Well if it was a timeout issue then @error should have been set to 10060 (not 0). You were probably reading the @error incorrectly... “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
frank10 Posted March 4, 2019 Author Posted March 4, 2019 That's exactly why I didn't found the problem easily. And also in the help file, when there is an error, it says it put the error code into @error: "-1 or 0 and sets the @error flag to non-zero." Instead in this case it does not set it! I put my code above. I did: Local $iSocket = TCPConnect($sIPAddress, $iPort) ConsoleWrite("no Socket err:" & @error & " ex:" & @extended) And to me it seems the correct way to check the @error. And the result was socket=-1, @error=0
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