Skullcode 0 Posted November 19, 2020 i use Twsocket as udp Client that is communicating with udpserver the server sends data in Bytes and the client should Receive This Data in Bytes as well i have tried the following based on what i see in the Demo Sample of OverbyteIcsUdpLstn Demo procedure TMainForm.WSocketDataAvailable(Sender: TObject; Error: Word); var aBytes : Tbytes; begin wsocket.Receive(aBytes, length(aBytes)); //... end; but this freezes the application and immediately crashed Share this post Link to post
Lajos Juhász 293 Posted November 19, 2020 First you've to initialize aBytes. Try something like this: setLength(aBytes, 1024); wsocket.Receive(aBytes, length(aBytes)); 1 Share this post Link to post
FPiette 383 Posted November 19, 2020 (edited) Response of Lajos is correct. But anyway, it is better to use a fixed size array as buffer to avoid allocation freeing a dynamic array as each data packet is coming. So write something like this instead: procedure TMainForm.WSocketDataAvailable(Sender: TObject; Error: Word); const BUF_SIZE = 1000; // Or whatever size fits your needs var ABytes : array [0..BUF_SIZE] of bytes; Len : Integer; begin Len := WSocket.Receive(@ABytes[0], BUF_SIZE); if Len = 0 then Exit; if Len < 0 then begin ShowMessage('Error receiving data'); Exit; end; // Process data... end; Edited November 20, 2020 by FPiette 1 Share this post Link to post
aehimself 396 Posted November 23, 2020 (edited) Just a small heads-up; binary transfer requires different handling than text. See Edited November 23, 2020 by aehimself Share this post Link to post
Angus Robertson 574 Posted November 23, 2020 The onDataAvailable event has knowledge of the data being received, unless you set LineMode to true, you just read all data into a buffer and process it later. If binary data is not received, that is an application error. Angus Share this post Link to post