Jump to content

TCPSend bug???


Recommended Posts

It seems that TCPSend cannot detect with the server has disconnected or closed the socket until it sends TWICE.

For this example, you need to have a TCP server running on port 5000 or whatever.


 

TCPStartup()

$socket = TCPConnect("127.0.0.1", "5000")

$result = TCPSend($socket,"Hello" & @CRLF)

$error = @error

MsgBox(0,0,"Result=" & $result & " Error=" & $error)

;The MsgBox shows a successful result and no error

;While the MsgBox is still open, close the server program, and then close the MsgBox to continue with the 2nd TCPSend run

$result = TCPSend($socket,"Hello" & @CRLF)

$error = @error

MsgBox(0,0,"Result=" & $result & " Error=" & $error)

;Although the server is closed, the MsgBox shows a successful result and no error!  How is this possible???  And how can I detect this so that I can reconnect and resend if it shows successful?

;Close the MsgBox and let the 3rd TCPSend run now

$result = TCPSend($socket,"Hello" & @CRLF)

$error = @error

MsgBox(0,0,"Result=" & $result & " Error=" & $error)

;This time, the MsgBox indicates a failed run and returns an error.

 

Edited by ferbfletcher
Link to comment
Share on other sites

  • Developers
2 hours ago, ferbfletcher said:

;While the MsgBox is still open, close the server program

which server program? :)

jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

I just tried it like this described below to make sure it is reproducible, and trying not to make this too much about the server:

In my example above, connect to google's ip address and port 80 in the tcpconnect.  In my case it was TCPConnect("172.217.10.142","80")

At the first msgbox, you should get a success message.  Now, unplug your computer's network cable or disconnect your computer's wifi, however you are connected to the network.  Close the msgbox so that it continues.  Even with the network disconnected on your computer, it still shows successful on the next connection (and actually on both).  Why does it think that it is successfully sending to a remote host (Google in this case) if the network is unplugged?

I'm using Windows 10 1809

Edited by ferbfletcher
Link to comment
Share on other sites

  • Developers

Ok, made this test to check and do not see the issue you report, the 2nd and 3th send fail, but the first one is successful:

Server:

TCPStartup() ; Start the TCP service.
OnAutoItExitRegister("OnAutoItExit")
Local $iListenSocket = TCPListen("127.0.0.1", 5000, 100)
; Assign Local variable to be used by Listening and Client sockets.
Local $iSocket = 0
Do ; Wait for someone to connect (Unlimited).
    $iSocket = TCPAccept($iListenSocket)
Until $iSocket <> -1 ;if different from -1 a client is connected.
; Close the Listening socket to allow afterward binds.
TCPCloseSocket($iListenSocket)
MsgBox(262144, 1, "Client Connected.",2)

; Assign a Local variable the data received.
Local $sReceived = TCPRecv($iSocket, 4) ;Wait for data
; Display the string received and close connection.
MsgBox(262144, 2, "Server received: " & $sReceived,2)

; Close the socket.
TCPCloseSocket($iSocket)

Func OnAutoItExit()
    TCPShutdown() ; Close the TCP service.
EndFunc   ;==>OnAutoItExit

Client:

$rc=TCPStartup()
$socket = TCPConnect("127.0.0.1", 5000)
$result = TCPSend($socket, "Hello1" & @CRLF)
MsgBox(262144, 1, "1.Result=" & $result & " Error=" & @error)
;The MsgBox shows a successful result and no error
;While the MsgBox is still open, close the server program, and then close the MsgBox to continue with the 2nd TCPSend run
$result = TCPSend($socket, "Hello2" & @CRLF)
$error = @error
MsgBox(262144, 2, "2.Result=" & $result & " Error=" & $error)
;Although the server is closed, the MsgBox shows a successful result and no error!  How is this possible???  And how can I detect this so that I can reconnect and resend if it shows successful?
;Close the MsgBox and let the 3rd TCPSend run now
$result = TCPSend($socket, "Hello3" & @CRLF)
$error = @error
MsgBox(262144, 3, "3.Result=" & $result & " Error=" & $error)
;This time, the MsgBox indicates a failed run and returns an error.
TCPShutdown()

Jos

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Edit: Disregard this example, I see that it is going to create a million tcpaccept sockets.  I'll try to come up with a better example.

Try this test server with your client:  The server will close the listen socket and close the accept socket after 15 seconds (but the server program does not exit), and your client still shows successful result after 15 seconds.  Just make sure to do the first client send before 15 seconds and then wait for the server 15-second message to appear before continuing to the client's 2nd send.  I am getting a successful message, no error, on all 3 sends this way, even after closing both sockets.

 

TCPStartup()

$socket = TCPListen("0.0.0.0",5000)

$timer = TimerInit()

While 1
    $mysocket = TCPAccept($socket)
    Sleep(10)

    If TimerDiff($timer) > 15000 Then ;wait 15 seconds before closing the sockets so you can do 1 client test before this point
        TCPCloseSocket($socket)
        TCPCloseSocket($mysocket)
        msgbox(0,0,"sockets closed now")
        While 1
            Sleep(10) ;loop just to keep the program running without doing anything
        WEnd

    EndIf

WEnd

 

Edited by ferbfletcher
Link to comment
Share on other sites

  • Developers

I understand that when the TCPCloseSocket() hasn't been done by the server, that data still send successful. but I do get  an error after it is closed.
Here are the adapted scripts. 
Save this script as Sender.au3:

$rc=TCPStartup()
$socket = TCPConnect("127.0.0.1", 5000)

For $x = 1 to 7
    $result = TCPSend($socket, "Hello " & $x & @CRLF)
    ToolTip($x & ". Result=" & $result & " Error=" & @error,610,330)
    Sleep(2000)
Next
;This time, the MsgBox indicates a failed run and returns an error.
TCPShutdown()

Save this as Server.au3 in the same directory and run it:

TCPStartup() ; Start the TCP service.
OnAutoItExitRegister("OnAutoItExit")
Local $iListenSocket = TCPListen("127.0.0.1", 5000, 100)
; Assign Local variable to be used by Listening and Client sockets.
Local $iSocket = 0
ToolTip("Server waiting for connection.",600,300)
; start sender
run(@AutoItExe & ' "sender.au3"')
; wait for incomming connection
Do ; Wait for someone to connect (Unlimited).
    $iSocket = TCPAccept($iListenSocket)
Until $iSocket <> -1 ;if different from -1 a client is connected.
; Close the Listening socket to allow afterward binds.
TCPCloseSocket($iListenSocket)
ToolTip("Client Connected.",600,300)
Sleep(1000)
; Assign a Local variable the data received.
Local $sReceived = TCPRecv($iSocket, 100) ;Wait for data
; Display the string received and close connection.
ToolTip("Server received: " & $sReceived,600,300)
Sleep(4000)
;
; Close the socket.
TCPCloseSocket($iSocket)
ToolTip( "Socket closed.",600,300)
Sleep(4000)

Func OnAutoItExit()
    TCPShutdown() ; Close the TCP service.
    ToolTip( "TCPShutdown().",600,300)
    Sleep(5000)
EndFunc   ;==>OnAutoItExit

The results are displayed in 2 tooltip's underneath each other  for Server and sender.
So, still don't know how to replicate what you see, but try adapting these files so they do. :)

Jos

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

This might be a good example, at least to show the issue.  The issue is that there is no failure detected on the first attempt after the socket closed.

Look at this server.  It accepts a connection, and then it immediately closes that socket, and goes into that nested while loop (just so it doesn't exit the program) so the client should fail on the 1st attempt of sending something. However, the client does not show any failure until the second sending attempt.

TCPStartup()

$socket = TCPListen("0.0.0.0",5000)

While 1
    Sleep(10)

    $mysocket = TCPAccept($socket)
    If $mysocket = -1 Then ContinueLoop

    TCPCloseSocket($mysocket)

    While 1
        Sleep(10)
    WEnd

WEnd

 

And this client:

TCPStartup()

$socket = TCPConnect("127.0.0.1", 5000)
MsgBox(262144,1,"Connection Result=" & $socket)

$result = TCPSend($socket, "Hello1" & @CRLF)
MsgBox(262144, 1, "1.Result=" & $result & " Error=" & @error)
;This should fail, but it shows successful

$result = TCPSend($socket, "Hello2" & @CRLF)
MsgBox(262144, 2, "2.Result=" & $result & " Error=" & @error)
;This should fail, which it does

$result = TCPSend($socket, "Hello3" & @CRLF)
MsgBox(262144, 3, "3.Result=" & $result & " Error=" & @error)
;This should fail, which it does

TCPShutdown()

 

 

Edited by ferbfletcher
Link to comment
Share on other sites

  • Developers

I am sorry, but we seem to have issues aligning tests here. I have provided a simple example that show things work fine.
You now provide a total different answer without any confirmation you tried my example and agree it is working fine for you, and totally ignore the request to modify it in a way it fails.....    I am not going to modify your last version again and having to add the debugging tooltps, so please do yourself a favor and work with me by updating my latest scripts in a way to clearly demonstrates your issue. :)

Jos 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Ok, in your most recent example, in Sender.au3, change Sleep(2000) to Sleep(8000).  Now, if you watch the tooltips, you will see test 1 no errors (the server shows the data), then server closes socket, then test 2 no errors!  (but the server does not actually show the data), then test 3 error like you would expect.

You can even go up to Sleep(12000) and then the server will close the socket, and the server will do tcpshutdown, and the client will still show no error on that 2nd test.

 

 

Edited by ferbfletcher
Link to comment
Share on other sites

  • Developers

Looks indeed like the buffer isn't closed right away in some at the return of the TcpClosePort() function.
Not sure whether that is standard behavior of the windows calls used or something else.

Jos 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

"Right away" isn't really the best description.  Even if I wait for some time, the send after the socket close still shows success.  It's like it doesn't really close until it gets some input first.  I'm having to rewrite some of my scripts to open and close the TCP connection at every TCPSend since it can't detect when this happens.

 

 

Edited by ferbfletcher
Link to comment
Share on other sites

  • Developers

Looks like this is reported before and a request was posted for  proper reproducer script, which we have now. :)

Just add your experience and the last scripts that demonstrate the issue to this trac ticket:
https://www.autoitscript.com/trac/autoit/ticket/3575

EDIT: I have added a comment with a link to this thread so it can be looked at. 

Thanks, Jos

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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