Difference between revisions of "TWSocket.ReceiveFrom"

From Overbyte
Jump to navigation Jump to search
(Initial entry, still a bit incomplete)
 
Line 7: Line 7:
 
== Description ==
 
== Description ==
  
'''ReceiveFrom''' is used in the [[TWSocket.OnDataAvailable|OnDataAvailable]]-Event to actually fetch the received data and information about the sending party. The parameters needed are a buffer where the data will be placed and the size of that buffer. The return value of Receive specifies how many bytes where actually read and put in the buffer. If the buffer is too small, OnDataAvailable will occur again until all data is received.
+
'''ReceiveFrom''' is used in the [[TWSocket.OnDataAvailable|OnDataAvailable]]-Event to actually fetch the received data and information about the sending party. The parameters needed are a buffer where the data will be placed, the size of the buffer, a TSockAddr variable and the size of the TSockAddr. The return value of Receive specifies how many bytes where actually read and placed in the buffer and the information about the sender. If the buffer is too small, OnDataAvailable will occur again until all data is received.
  
 
If the return value is 0 the connection the data shall be read from has been closed and a value of less than 0 means that an error occured. The SOCKET_ERROR constant's value will be returned in this case. In order to retrieve the real error code you need to check the LastError property immediately, before any other call might lead to yet another error.
 
If the return value is 0 the connection the data shall be read from has been closed and a value of less than 0 means that an error occured. The SOCKET_ERROR constant's value will be returned in this case. In order to retrieve the real error code you need to check the LastError property immediately, before any other call might lead to yet another error.
Line 17: Line 17:
 
   procedure WSocket1OnDataAvailable(Sender: TObject; ErrCode: Word);
 
   procedure WSocket1OnDataAvailable(Sender: TObject; ErrCode: Word);
 
   var buf:array[1..64] of byte;
 
   var buf:array[1..64] of byte;
       len:Integer;
+
       len,fromSize:Integer;
 +
      from:TSockAddr;
 
   begin
 
   begin
 
     if ErrCode <> 0 then exit;
 
     if ErrCode <> 0 then exit;
     len:=Receive(@buf, sizeof(buf));
+
    fromSize:=sizeOf(from);
 +
     len:=ReceiveFrom(@buf, sizeof(buf), from, fromSize);
  
 
     if (len > 0) then
 
     if (len > 0) then

Revision as of 14:50, 5 March 2016

Main page -> ICS component reference -> TWSocket -> ReceiveFrom

Definition

function ReceiveFrom(Buffer:Pointer; BufferSize:Integer; ?, SrcLen: Integer): integer;

Description

ReceiveFrom is used in the OnDataAvailable-Event to actually fetch the received data and information about the sending party. The parameters needed are a buffer where the data will be placed, the size of the buffer, a TSockAddr variable and the size of the TSockAddr. The return value of Receive specifies how many bytes where actually read and placed in the buffer and the information about the sender. If the buffer is too small, OnDataAvailable will occur again until all data is received.

If the return value is 0 the connection the data shall be read from has been closed and a value of less than 0 means that an error occured. The SOCKET_ERROR constant's value will be returned in this case. In order to retrieve the real error code you need to check the LastError property immediately, before any other call might lead to yet another error.

If the LastError error code is WSAE_WOULD_BLOCK, it means that Receive has been called at a point in time where the system wasn't ready to process it. Most likely you can ignore this error condition.

Example

  procedure WSocket1OnDataAvailable(Sender: TObject; ErrCode: Word);
  var buf:array[1..64] of byte;
      len,fromSize:Integer;
      from:TSockAddr;
  begin
    if ErrCode <> 0 then exit;
    fromSize:=sizeOf(from); 
    len:=ReceiveFrom(@buf, sizeof(buf), from, fromSize);
    if (len > 0) then
      // here your procesing of the data will take place
  end;

Best practices

  • never do any message processing like Application.ProcessMessages or GetMessage inside a eventhandler, since it will raise the same event again for the very same data!
  • if the data received needs lengthy processing a separate thread for this might be a good idea since further data can only be received after the event has been processed completely. In most cases a separate thread is not necessary though.

How to