lucio69f Posted November 13, 2023 Posted November 13, 2023 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 ? expandcollapse popup#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
RTFC Posted November 13, 2023 Posted November 13, 2023 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. My Contributions and Wrappers Spoiler BitMaskSudokuSolver BuildPartitionTable CodeCrypter CodeScanner DigitalDisplay Eigen4AutoIt FAT Suite HighMem MetaCodeFileLibrary OSgrid Pool RdRand SecondDesktop SimulatedAnnealing Xbase I/O
Andreik Posted November 13, 2023 Posted November 13, 2023 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.
lucio69f Posted November 13, 2023 Author Posted November 13, 2023 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
Andreik Posted November 13, 2023 Posted November 13, 2023 You can do other things depending on how do you handle them. Maybe it will be more easy and interesting for you to use TCP UDF (event driven) where a function is fired just when you receive data, so you can process data accordingly.
lucio69f Posted November 13, 2023 Author Posted November 13, 2023 but i recive most frequently data cents of second usally , do you have some example?
Andreik Posted November 13, 2023 Posted November 13, 2023 (edited) 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 expandcollapse popup#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 November 13, 2023 by Andreik
lucio69f Posted November 13, 2023 Author Posted November 13, 2023 i just saw this example but is not good for my cases thanks so mcuh
lucio69f Posted November 13, 2023 Author Posted November 13, 2023 i suppose maybe i must use multithread i saw autoit can do it
lucio69f Posted November 13, 2023 Author Posted November 13, 2023 yes you can https://github.com/nomi-san/true-autoit-multi-threading
lucio69f Posted November 13, 2023 Author Posted November 13, 2023 https://github.com/jesobreira/authread
Andreik Posted November 13, 2023 Posted November 13, 2023 Native AutoIt it's now multithreaded. I can surely create new threads from AutoIt in several ways but this doesn't make it multithreaded. Have fun with multithreaded sockets.
lucio69f Posted November 13, 2023 Author Posted November 13, 2023 have fun i dont know >D but i try thanks
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now