Jump to content

Recommended Posts

Posted (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:

#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 by frank10
Posted (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 by frank10
Posted
Posted

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

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...