carloselectro

Cannot get consistent data with TCPRecv???

5 posts in this topic

#1 ·  Posted (edited)

I am connecting to a server with TCP and I can never get the same response from the server with TCPRecv.

I know for sure my commands are fine because it's sending me the right response with another software (Hercules).

I am including the response I should get from the server.(It's about 180 lines)

TCPRecv returns only a couple of lines and sometime many. I can never get everything.

Here is part of my code:

 

Func Receive_data()

    Opt("TCPTimeout", 100) ;100 milliseconds
    TCPStartup()

    Local $szIPADDRESS = "192.168.11.36"
    Local  $nPORT = 5038

    $ConnectedSocket = -1

    $ConnectedSocket = TCPConnect($szIPADDRESS, $nPORT)
    ; If there is an error... show it and exit
    If @error Then
        MsgBox(4112, "Error", "Connect error: " & @error)
        Exit

    EndIf

    ;Sending data
    TCPSend($ConnectedSocket,"Action: PJSIPShowEndpoint" & @CRLF))
    TCPSend($ConnectedSocket,"ActionID: 1" & @CRLF)
    TCPSend($ConnectedSocket,"Endpoint: U_8374" & @CRLF & @CRLF)

    ;Receiving data
    Do
        $response = TCPRecv($ConnectedSocket, 2048)
        
    Until $response <> ""

    ConsoleWrite("Data received:  " & $response)
    TCPShutdown()
    Exit

EndFunc   ;==>Receive_data

 

Any help will be greatly appreciated.

 

 

Right_Response.txt

Edited by carloselectro

Share this post


Link to post
Share on other sites



#3 ·  Posted (edited)

I have just tried, and it's not returning anything. It loops endlessly.

The best I got so far is with this code:

But I only get part of the data.

 

Do
    $response &= TCPRecv($ConnectedSocket, 2048)
Until StringRegExp($response,@CRLF & @CRLF)

 

Edited by carloselectro

Share this post


Link to post
Share on other sites

I finally got it with this regex:

I match everything between the first string with the last string.

 

local  $response = ""
    Do
    $response &= TCPRecv($ConnectedSocket, 2048)
    Until  StringRegExp($response, '(?s)Response(.*)ListItems')
    ConsoleWrite("Data received:  " & $response & @CRLF)

 

Share this post


Link to post
Share on other sites
Func Receive_data()

    TCPStartup()

    Local $szIPADDRESS = "192.168.11.36"
    Local $nPORT = "5038"

    Local $Socket = TCPConnect($szIPADDRESS, $nPORT)
    Local $error = @error
    If $error Then; If there is an error... show it and exit
        MsgBox(4112, "Error", "Connect error: " & $error)
        Exit
    EndIf

    ;Sending data
    Local $str = ""
    $str &= "Action: PJSIPShowEndpoint" & @CRLF
    $str &= "ActionID: 1" & @CRLF
    $str &= "Endpoint: U_8374" & @CRLF & @CRLF

    TCPSend($Socket, $str)
    $error = @error
    If $error Then; If there is an error... show it and exit
        MsgBox(4112, "Error", "Send error: " & $error)
        Exit
    EndIf

    Local $sRecv, $sData = ""

    ;Receiving data
    For $i = 1 To 200
        $sRecv = TCPRecv($Socket, 2048)
        If @error Then
            ExitLoop
        ElseIf StringLen($sRecv) Then
            $sData &= $sRecv
            $i = 0
        EndIf
        Sleep(10)
    Next

    ConsoleWrite("Data received:" & @CRLF & $sData & @CRLF)

    TCPCloseSocket($Socket)
    TCPShutDown()

EndFunc

 


"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

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