Jump to content

TCPRecv is possible max len set infinity ?


lucio69f
 Share

Recommended Posts

Hi i have  create  a script for  catch a stream of  data , but  i don't know  how many large is  a stream that recive, also because some time i have many data  , some time  less , is possible set this parameter in manner i could sure  that arrive all data ?

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIError.au3>

_StartStream()

Func _StartStream()
    TCPStartup() ; Start the TCP service.

    ; Register OnAutoItExit to be called when the script is closed.
    OnAutoItExitRegister("OnAutoItExit")

    ; Assign Local variables the loopback IP Address and the Port.
    Local $sIPAddress = "127.0.0.1" ; This IP Address only works for testing on your own computer.
    Local $iPort = 23456 ; Port used for the connection.

    Local $iListenSocket = TCPListen($sIPAddress, $iPort, 100)
    Local $iError = 0

    If @error Then
        ; Someone is probably already listening on this IP Address and Port (script already running?).
        $iError = @error
        MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Server:" & @CRLF & "Could not listen, Error code: " & $iError & @CRLF & _WinAPI_GetErrorMessage($iError))
        Return False
    EndIf

    While True
        Local $iConnectedSocket = TCPAccept($iListenSocket)

        If $iConnectedSocket <> -1 Then
            ; Connection received, now receive data.
            Local $sReceived = TCPRecv($iConnectedSocket, 10224) ; Adjust buffer size as needed.
            ConsoleWrite($sReceived & @CRLF)

            ; Close the connected socket.
            TCPCloseSocket($iConnectedSocket)
        EndIf
    WEnd
EndFunc

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

 

Link to comment
Share on other sites

Maximum TCP packet size is 65535 bytes in theory, but in practice it's restricted by your network's Maximum Transmission Unit (MTU, ethernet frame; do not confuse this with a UDP datagram, which is much smaller), 1500 bytes is common for MTU. Both TCP (reliable, slower) and UDP (unreliable = packets may be dropped, faster ) can be used for streaming.

Link to comment
Share on other sites

As RTFC already said there are many things involved so there is no guarantee that you get all the data in a single packet. A good approach is to get your data in a loop until there is no more data to receive and the function returns empty string and set @extended = 1.

When the words fail... music speaks.

Link to comment
Share on other sites

is  possible in mean time that i receve data upload a listview of my data and in mean time do somting in GUI  ?  i tell this  beacause  if  arrive data the GUI not move and  if  i  receve a stream of  data  the GUI will be blocked ... i suppose , right? 

thanks

 

Link to comment
Share on other sites

Here is an example. Start your server and then your client. As you will be able to see, you can do your stuff like clicking buttons or other things while the messages from the server are received and you can process them by your will. Just one note: don't use blocking functions like MsgBox() in these functions that are registered by TCP  UDF because it won't work.

Server.au3

#include "TCP.au3"

$hServer = _TCP_Server_Create(1222, @IPAddress1)

_TCP_RegisterEvent($hServer, $TCP_NEWCLIENT, 'NewClient')
_TCP_RegisterEvent($hServer, $TCP_DISCONNECT, 'Disconnect')

While True
    Sleep(10)
WEnd

Func NewClient($hSocket, $iError)
    _TCP_Send($hSocket, 'Hello from server. You just connected to the server.')
    ;~ Send a random message to the clients every 3-5 seconds
    AdlibRegister('SendRandomData', Random(3000, 5000, 1))
EndFunc

Func Disconnect($hSocket, $iError)
    ; If the client disconnect from the server, we close the server also.
    Exit
EndFunc

Func SendRandomData()
    ; Send a random message to the clients
    Local $sData
    For $Index = 1 To Random(10, 100, 1)
        $sData &= Chr(Random(65, 90, 1))
    Next
    _TCP_Server_Broadcast($sData)
    AdlibRegister('SendRandomData', Random(3000, 5000, 1))
EndFunc

Client.au3

#include <TCP.au3>

Global $hMain, $cData

$hClient = _TCP_Client_Create(@IPAddress1, 1222)
_TCP_RegisterEvent($hClient, $TCP_CONNECT, 'Connected')
_TCP_RegisterEvent($hClient, $TCP_RECEIVE, 'Received')

$hMain = GUICreate('Example', 400, 400)
$cLabel = GUICtrlCreateLabel('Data received from server', 20, 20, 360, 20)
$cData = GUICtrlCreateLabel('', 20, 50, 360, 60, 0x1000)
$cButton1 = GUICtrlCreateButton('Click me', 20, 150, 100, 30)
$cButton2 = GUICtrlCreateButton('Or click me', 220, 150, 100, 30)
GUISetState(@SW_SHOW, $hMain)

While True
    Switch GUIGetMsg()
        Case -3 ; GUI_EVENT_CLOSE
            Exit
        Case $cButton1
            MsgBox(0, '', 'You can do some work here instead.')
        Case $cButton2
            MsgBox(0, '', 'You can also do some work here.')
    EndSwitch
WEnd

 Func Connected($hSocket, $iError)
    If $iError Then
        ConsoleWrite('Error: Could not connect. Are you sure the server is running?' & @CRLF)
    Else
        ConsoleWrite('Successfully connected to server.' & @CRLF)
    EndIf
 EndFunc

 Func Received($hSocket, $sReceived, $iError)
    GUICtrlSetData($cData, $sReceived)
    ; You can do some processing here but don't use blocking functions
 EndFunc

 

Edited by Andreik

When the words fail... music speaks.

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