seres

TCPSend connection reset by peer

4 posts in this topic

#1 ·  Posted

hello, i have and android client wich is connecting to an autoit server, after the connection is done i press a button to send data to the android client, but after some data sended i get a connection reset by peer in android studio. i have notice this happens when autoit server sends a bad string then the android client stops showing the received string and then it gets that error.

i dont know if it is possible to fix this problem. here is my code:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
$Form1 = GUICreate("Form1", 281, 257, 192, 124)
$Button1 = GUICtrlCreateButton("send", 32, 64, 75, 25)
$Button2 = GUICtrlCreateButton("kill", 32, 160, 75, 25)
$inpServer = GUICtrlCreateInput("10.121.119.64", 136, 25, 121, 21)
$Input1 = GUICtrlCreateInput("Input1", 136, 64, 121, 21)
$Button3 = GUICtrlCreateButton("send2", 32, 112, 75, 25)
$Button4 = GUICtrlCreateButton("start", 32, 24, 75, 25)
$btnCapture = GUICtrlCreateButton("capture", 136, 112, 75, 25)
GUISetState(@SW_SHOW)

$vFlag = 1

OnAutoItExitRegister('OnAutoItExit')


If Not TCPStartup() Then
    Exit MsgBox(0, 'AutoIt', 'Error: TCPStartup' & @TAB, 10)
EndIf


While 1

    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button4
            If $vFlag = 1 Then

                Local $MaxConnections = 4
                Local $MaxLength = 10240
                Local $Server = GUICtrlRead($inpServer)
                Local $Port = 65432
                Local $sData, $nError
                ;
                Global $ConnectedSocket
                Global $MainSocket = TCPListen($Server, $Port, $MaxConnections)
                If $MainSocket < 1 Then
                    Exit MsgBox(16, 'Error', 'Unable to intialize socket')
                EndIf
                $vFlag = 0

            EndIf

            Do
                Sleep(100)
                $ConnectedSocket = TCPAccept($MainSocket)

                $nMsg = GUIGetMsg()
                Switch $nMsg
                    Case $GUI_EVENT_CLOSE
                        Exit
                EndSwitch
            Until $ConnectedSocket > 0

            MsgBox(0,"hey", "in: " & $ConnectedSocket)
        Case $Button1
            $inputread = GUICtrlRead($Input1)
            TCPSend($ConnectedSocket, $inputread)
            $nError = @error
            If $nError Then
                MsgBox(0, 'TCPSend Error', 'Error Code: ' & $nError)
                ContinueLoop
            EndIf

            $sData = TCPRecv($ConnectedSocket, $MaxLength)
                IF $ConnectedSocket > 0 THEN
                    ConsoleWrite($sData & @CRLF)
                ENDIF

        Case $Button3
            For $i = 1 To 20
                TCPSend($ConnectedSocket, 'Connected ' & $i )
                $nError = @error
                If $nError Then
                    MsgBox(0, 'TCPSend Error', 'Error Code: ' & $nError)
                    ContinueLoop
                EndIf
                Sleep(1)

            Next

        Case $btnCapture
            $pos1check = 0
            $pos2check = 0
            While 1
                $varMousePos = MouseGetPos()
                $pos1 = $varMousePos[0]
                $pos2 = $varMousePos[1]
                If ($pos1 <> "") and ($pos2 <> "") Then
                    If ($pos1 <> $pos1check) AND ($pos2 <> $pos2check) Then
                        TCPSend($ConnectedSocket, $varMousePos[0]&","&$varMousePos[1])
                        $nError = @error
                        $pos1check = $pos1
                        $pos2check = $pos2
                    EndIf
                    If $nError Then
                        MsgBox(0, 'TCPSend Error', 'Error Code: ' & $nError)
                        ExitLoop
                    EndIf
                EndIf
                Sleep(100)


                $sData = TCPRecv($ConnectedSocket, $MaxLength)
                IF $ConnectedSocket > 0 THEN
                    ConsoleWrite($sData & @CRLF)
                ENDIF
            WEnd

        Case $Button2
            TCPCloseSocket($ConnectedSocket)
    EndSwitch

WEnd

Func OnAutoItExit()
    If $ConnectedSocket > 0 Then
        TCPSend($ConnectedSocket, '~bye')
        TCPCloseSocket($ConnectedSocket)
    EndIf
    TCPCloseSocket($MainSocket)
    TCPShutdown()
EndFunc
;

 

Share this post


Link to post
Share on other sites



#2 ·  Posted

A couple suggestions...
Not sure what the below statement is intended to do after TCPRecv.

$sData = TCPRecv($ConnectedSocket, $MaxLength)
IF $ConnectedSocket > 0 THEN

Should be:

$sData = TCPRecv($ConnectedSocket, $MaxLength)
If @error Then
TCPCloseSocket($ConnectedSocket)
ExitLoop; <-- or whatever action is desired
ElseIf StringLen($sData) > 0 Then

1. There needs to be an error check on every TCPSend() and TCPRecv().

2. It's a good Idea to close the socket after each Case has completed.

3. Even if it seems repetitive (when the script is not in listening mode),
the order needs to be TCPConnect() --> TCPSend() --> TCPCloseSocket().
-OR-
TCPConnect() --> TCPRecv() --> TCPCloseSocket().
Don't forget a For Loop with TCPRecv(), so you'll have a timeout scheme.
Otherwise, you WILL have troubles with it.

4. It's probably a good idea to rethink the Case logic.

1 person likes this

Make yourself sheep and the wolves will eat you - Benjamin Franklin

Share this post


Link to post
Share on other sites

#3 ·  Posted

21 hours ago, ripdad said:

A couple suggestions...
Not sure what the below statement is intended to do after TCPRecv.

$sData = TCPRecv($ConnectedSocket, $MaxLength)
IF $ConnectedSocket > 0 THEN

Should be:

$sData = TCPRecv($ConnectedSocket, $MaxLength)
If @error Then
TCPCloseSocket($ConnectedSocket)
ExitLoop; <-- or whatever action is desired
ElseIf StringLen($sData) > 0 Then

1. There needs to be an error check on every TCPSend() and TCPRecv().

2. It's a good Idea to close the socket after each Case has completed.

3. Even if it seems repetitive (when the script is not in listening mode),
the order needs to be TCPConnect() --> TCPSend() --> TCPCloseSocket().
-OR-
TCPConnect() --> TCPRecv() --> TCPCloseSocket().
Don't forget a For Loop with TCPRecv(), so you'll have a timeout scheme.
Otherwise, you WILL have troubles with it.

4. It's probably a good idea to rethink the Case logic.
 

Wow thanks a lot i did what you tell me and by closing the socket and listening to it everytime did the trick. I still feel kind of weird that i have to close is and reopen it everytime instead of leaving it open untill i finish sending data

Share this post


Link to post
Share on other sites

#4 ·  Posted

I probably would just send the data all at once and parse it out on the receiving end.
There's a few ways you could handle it. Whatever works best for you.

In any case -- glad you got it worked out.


Make yourself sheep and the wolves will eat you - Benjamin Franklin

Share this post


Link to post
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