Jump to content

TCPRecv() Loop


Recommended Posts

Hi,

Can someone please explain how to loop around TCPRecv(), as I can't see what I'm doing wrong.

CODE

Opt("TCPTimeout", 100)

$MythBackend = "someip"

$MythBackendPort = "6543"

$MythClientName = "client"

TCPStartup()

$MythSocket = TCPConnect($MythBackend, $MythBackendPort)

If $MythSocket = -1 Then

Exit

EndIf

$MaxLen = "1"

; Wan't see my loop working. Therefore $MaxLen is set to 1. Output from $MythSendProto is: '13 ACCEPT[]:[]31'.

; Using this loop $MythResponseProto only contains '1'

$MythSendProto = TCPSend($MythSocket, "21 MYTH_PROTO_VERSION 31")

$MythResponseProto = ""

While $MythResponseProto = ""

$MythResponseProto = TCPRecv($MythSocket, $MaxLen)

WEnd

MsgBox(0, "Protocol Response", $MythResponseProto)

TCPCloseSocket($MythSocket)

TCPShutdown()

Regards

Michael

Link to comment
Share on other sites

Hi,

Can someone please explain how to loop around TCPRecv(), as I can't see what I'm doing wrong.

that's why we have sample code in the help file. See TCPRecv() and you will find the correct way to "loop around" it.

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

that's why we have sample code in the help file. See TCPRecv() and you will find the correct way to "loop around" it.

Okay, took a second look at the sample, and I think I got it now.

But I do have one issue though. It doesn't seem like I'm getting all the data, unless I set TCPTimeout to a rather high value.

Is this the way to do it, or is there some other method, so that one is certain that all data is received?

CODE

Opt("TCPTimeout", 1000)

$MythBackend = "somehost"

$MythBackendPort = "6543"

$MythClientName = "someclient"

TCPStartup()

$MythSocket = TCPConnect($MythBackend, $MythBackendPort)

If $MythSocket = -1 Then

Exit

EndIf

$MaxLen = "64"

; MythTV Protocol

$MythProtoSend = TCPSend($MythSocket, "21 MYTH_PROTO_VERSION 31")

$MythProtoMessage = ""

While 1

$MythProtoResponse = TCPRecv($MythSocket, $MaxLen)

If @error Then

ExitLoop

EndIf

If $MythProtoResponse <> "" Then

$MythProtoMessage = $MythProtoMessage & $MythProtoResponse

EndIf

If $MythProtoResponse = "" Then

MsgBox(0, "Protocol Response", $MythProtoMessage)

ExitLoop

EndIf

WEnd

; MythTV Announce

$MythANNSend = TCPSend($MythSocket, "19 ANN Playback " & $MythClientName & " 0")

$MythANNMessage = ""

While 1

$MythANNResponse = TCPRecv($MythSocket, $MaxLen)

If @error Then

ExitLoop

EndIf

If $MythANNResponse <> "" Then

$MythANNMessage = $MythANNMessage & $MythANNResponse

EndIf

If $MythANNResponse = "" Then

MsgBox(0, "Announcement Response", $MythANNMessage)

ExitLoop

EndIf

WEnd

; MythTV GetAllScheduled

$MythGetAllScheduledSend = TCPSend($MythSocket, "21 QUERY_GETALLSCHEDULED")

$MythGetAllScheduledMessage = ""

While 1

$MythGetAllScheduledResponse = TCPRecv($MythSocket, $MaxLen)

If @error Then

ExitLoop

EndIf

If $MythGetAllScheduledResponse <> "" Then

$MythGetAllScheduledMessage = $MythGetAllScheduledMessage & $MythGetAllScheduledResponse

EndIf

If $MythGetAllScheduledResponse = "" Then

MsgBox(0, "All Scheduled Recordings", $MythGetAllScheduledMessage)

ExitLoop

EndIf

WEnd

If $MythSocket <> -1 Then

TCPCloseSocket($MythSocket)

EndIf

TCPShutdown()

Regards

Michael

Link to comment
Share on other sites

Can someone please explain how to loop around TCPRecv(), as I can't see what I'm doing wrong.

hi

it took me awhile too to get tcpip running.. i do it now with a little help from tcpfuncs..

CODE
#include-once

#include 'TCP Funcs.au3'

Global $ConnectedSocket = -1

Global $MainSocket = 0

$Port = 20000;

; start up server now .

$MainSocket = TCPStartServer ($Port)

While 1

$msg = GUIGetMsg()

If $msg = $GUI_EVENT_CLOSE Then

Exit

;If $ConnectedSocket = -1 Then

$ConnectedSocket = TCPAllowConnection ($MainSocket)

$Recv = TCPReceive ($ConnectedSocket)

; _DebugPrint ("TCPIP recv " & $Recv)

If (@error = 0) And ($Recv <> "-1>") Then

--> my data now in $recv

Else

- error...

EndIf

....

WEnd

Func OnAutoItExit()

TCPCloseSocket($MainSocket)

TCPShutdown()

EndFunc ;==>OnAutoItExit

;

; TCP Funcs.au3

;

#include-once

; ----------------------------------------------------------------------------

; AutoIt Version: 3.1.1.92

; Author: Max Gardner <AutoIt Smith;king.of.all@comcast.net>

; ----------------------------------------------------------------------------

; ----------------------------------------------------------------------------

; Function: TCPStartServer($Port[, $MaxConnection])

; $Port = Port To Start Server On

; $MaxConnection = (Optional) Maximum Connections Allowed; Default 1

;

; Returns:

; Success : Returns Socket and @Error = 0

; Failure : Returns 0 or 1 and sets @Error to Windows API WSAGetLasterror

; 0 = TCP Services Not Avialible On That Port

; -1 = TCP Services Would Not StartUp

; ----------------------------------------------------------------------------

Func TCPStartServer($Port, $MaxConnect = 1)

Local $Socket

$Socket = TCPStartup()

Select

Case $Socket = 0

SetError(@Error)

Return -1

EndSelect

$Socket = TCPListen(@IpAddress1, $Port, $MaxConnect)

Select

Case $Socket = -1

SetError(@Error)

Return 0

EndSelect

SetError(0)

Return $Socket

EndFunc

; ----------------------------------------------------------------------------

; Function: TCPAllowConnection($Socket)

; $Socket = The Main Socket As Returned By TCPStartServer

;

; Returns:

; Success : Returns ConnectedSocket and @Error = 0

; Failure : Returns -1 and sets @Error to Windows API WSAGetLasterror

; ----------------------------------------------------------------------------

Func TCPAllowConnection($Socket)

Local $ConnectedSocket

$ConnectedSocket = TCPAccept($Socket)

Select

Case $ConnectedSocket >= 0

SetError(0)

Return $ConnectedSocket

Case Else

SetError(@Error)

Return -1

EndSelect

EndFunc

; ----------------------------------------------------------------------------

; Function: TCPReceive($ConnectedSocket, $MaxChar)

; $ConnectedSocket = The Connected Socket As Returned By TCPAllowConnection

;

; Returns:

; Success : Returns Recieved String and @Error = 0

; Failure : Returns -1 and sets @Error to Windows API WSAGetLasterror

; ----------------------------------------------------------------------------

Func TCPReceive($ConnectedSocket, $MaxChar = 512)

Local $Data

$Data = TCPRecv($ConnectedSocket, $MaxChar)

Select

Case $Data = ""

SetError(@Error)

Return -1

Case Else

SetError(0)

Return $Data

EndSelect

EndFunc

; ----------------------------------------------------------------------------

; Function: TCPSendMessage($ConnectedSocket, $String)

; $ConnectedSocket = The Connected Socket As Returned By TCPAllowConnection

; $String = Data to be sent

;

; Returns:

; Success : Returns 1 and @Error = 0

; Failure : Returns -1 or 0 and sets @Error to Windows API WSAGetLasterror

;

; ----------------------------------------------------------------------------

Func TCPSendMessage($ConnectedSocket, $String)

Local $Sent

$Sent = TCPSend($ConnectedSocket, $String)

Select

Case $Sent = 0

SetError(@Error)

Return -1

Case Else

SetError(0)

Return 1

EndSelect

EndFunc

; ----------------------------------------------------------------------------

; Function: TCPStopServer($MainSocket, $ConnectedSocket)

; $MainSocket = The Socket As Returned By TCPStartServer

; $ConnectedSocket = The Connected Socket As Returned By TCPAllowConnection

;

; Returns:

; Success : Returns 1 and @Error = 0

; Failure : Returns -1 and sets @Error to Windows API WSAGetLasterror

; 0 = Unable To Shutdown TCP Services

; -1 = Unable To Close Socket/s

; ----------------------------------------------------------------------------

Func TCPStopServer($MainSocket, $ConnectedSocket)

Local $Shutdown

Select

Case $ConnectedSocket <> -1

$ConnectedSocket = TCPCloseSocket($ConnectedSocket)

Select

Case $ConnectedSocket = 0

SetError(@Error)

Return -1

EndSelect

EndSelect

Select

Case $MainSocket <> -1

$MainSocket = TCPCloseSocket($MainSocket)

Select

Case $MainSocket = 0

SetError(@Error)

Return -1

EndSelect

EndSelect

$Shutdown = TCPShutdown()

Select

Case $Shutdown = 0

SetError(@Error)

Return 0

EndSelect

SetError(0)

Return 1

EndFunc

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