TIdUDPBase
Specifies a UDP base class.
Unit
Description
TIdUDPBase specifies an encapsulation of the User Datagram Protocol (UDP), and provides properties and methods common to both UDP clients and UDP servers.
TIdUDPServer and
TIdUDPClient are descendants of TIdUDPBase.
For more information on the User Datagram Protocol (UDP), refer to the Internet Standards document:
- RFC 768 - User Datagram Protocol (UDP) (http://www.rfc-editor.org/rfc/rfc768.txt)
TIdUDPBase implements the common UDP facilities required to create a socket binding using the Socket Datagram protocol family, perform datagram Broadcasts, and handle Host and Port assignments for the connection-less protocol.
Indicates that the
socket binding has been allocated.
property Active: Boolean;
Description
Active is a Boolean property used to indicate that the
socket binding has been allocated. Note that Active will always return False when using the IDE.
Socket handle for the read or write transaction.
Description
Binding is a read-only
TIdSocketHandle property that represents the
socket binding used to
send and receive data. Unlike TCP, UDP does not create a permanent connection to the remote computer.
Indicates that the
socket binding can perform broadcast transmissions.
property BroadcastEnabled: Boolean;
Description
BroadcastEnabled is a Boolean property used to indicate when the protocol handler is transmitting a datagram destined for all computers on the network. BroadcastEnabled is updated by the
Broadcast method, and is used to notify the protocol stack that
Binding must include or exclude the
socket option for broadcast transmissions.
Specifies the largest UDP packet size.
property BufferSize: Integer;
Description
BufferSize is an Integer property used to specify the largest UDP packet that can be sent or received using
Binding. The default value for BufferSize is 8192, and is assigned in
Create.
An EIdSocketError exception will be raised if ReceiveString encounters a packet larger than BufferSize.
Timeout value for reading from a peer.
property ReceiveTimeout: Integer;
Description
ReceiveTimeout is an Integer property used to reflect the number of milliseconds to wait for the
socket Binding to become readable in the
ReceiveString method. The default value for ReceiveTimeout is TIdTimeoutInfinite as assigned in
Create.
Send data to all computers on the network.
procedure Broadcast(const AData: string; const APort: integer);
Parameters
const AData: string
Values to
send in the datagram.
const APort: integer
Port number for the transmission.
Description
Broadcast is a procedure used to
send the data specified in AData using the port number specified in APort. Broadcast sends the data to all computers on the network. Note: Many network routers will not forward UDP broadcasts beyond the boundaries of the local network.
Broadcast calls the Send method using the reserved network address '255.255.255.255' as the destination. Broadcast also maintains BroadcastEnabled to indicate the current transaction state.
Constructor for the object instance.
constructor Create(AOwner: TComponent); override;
Parameters
AOwner: TComponent
Owner of the object instance.
Description
Create is the constructor for the object instance, and relies on the inherited Create method to initialize object properties. Create sets the default value for
BufferSize to 8192, and
ReceiveTimeout to
IdTimeoutInfinite.
Frees an object instance.
destructor Destroy; override;
Description
Destroy is the destructor for the object instance. Destroy insures that the
Active property is set to False, and calls the inherited Destroy method to finalize object destruction.
Reads data from a remote connection.
function ReceiveBuffer(var ABuffer; const ABufferSize: Integer; const AMSec: Integer = IdTimeoutDefault): integer; overload;
function ReceiveBuffer(var ABuffer; const ABufferSize: Integer; var VPeerIP: string; var VPeerPort: integer; AMSec: Integer = IdTimeoutDefault): integer; overload;
Parameters
var ABuffer
Buffer where received data is stored.
const ABufferSize: Integer
Number of bytes to read from the remote connection.
const AMSec: Integer = IdTimeoutDefault
Milliseconds to wait for a readable connection. The default value is
IdTimeoutDefault.
var VPeerIP: string
IP address for the remote connection.
var VPeerPort: integer
Port number for the remote connection.
Return Value
Integer - Bytes read from the remote connection.
Description
ReceiveBuffer is an Integer function used to read data from the remote connection specified in VPeerIP and VPeerPort into the buffer ABuffer.
ABufferSize indicates the number of bytes to read from the remote connection.
AMSec indicates the number of milliseconds to delay while waiting for the socket Binding to become Readable. The default value for AMSec is IdTimeoutDefault, and indicates that ReceiveTimeOut will be used as the timeout value.
If a timeout occurs while waiting for the Binding to become ready for the read operation, RecvBuffer will set the following values before exiting the method:
- Result - zero (0),
- VPeerIP - empty string ('')
- VPeerPort - zero (0)
If RecvBuffer is unable to read data from the remote connection, an EIdUDPReceiveErrorZeroBytes exception will be raised.
Reads data from a remote connection.
Parameters
var ABuffer
Buffer where received data is stored.
const ABufferSize: Integer
Number of bytes to read from the remote connection.
const AMSec: Integer = IdTimeoutDefault
Milliseconds to wait for a readable connection. The default value is
IdTimeoutDefault.
var VPeerIP: string
IP address for the remote connection.
var VPeerPort: integer
Port number for the remote connection.
Return Value
Integer - Bytes read from the remote connection.
Description
ReceiveBuffer is an Integer function used to read data from the remote connection specified in VPeerIP and VPeerPort into the buffer ABuffer.
ABufferSize indicates the number of bytes to read from the remote connection.
AMSec indicates the number of milliseconds to delay while waiting for the socket Binding to become Readable. The default value for AMSec is IdTimeoutDefault, and indicates that ReceiveTimeOut will be used as the timeout value.
If a timeout occurs while waiting for the Binding to become ready for the read operation, RecvBuffer will set the following values before exiting the method:
- Result - zero (0),
- VPeerIP - empty string ('')
- VPeerPort - zero (0)
If RecvBuffer is unable to read data from the remote connection, an EIdUDPReceiveErrorZeroBytes exception will be raised.
Reads data from the remote computer.
function ReceiveString(const AMSec: Integer = IdTimeoutDefault): string; overload;
function ReceiveString(var VPeerIP: string; var VPeerPort: integer; const AMSec: Integer = IdTimeoutDefault): string; overload;
Parameters
const AMSec: Integer = IdTimeoutDefault
var VPeerIP: string
Address of the remote computer.
var VPeerPort: integer
Port number of the remote computer.
Return Value
String - Data received from the remote computer.
Description
ReceiveString is a String function used to receive data from a remote computer system.
VPeerIP is updated to reflect the IP address on the remote computer where the data originated. VPeerPort is updated to reflect the port number on the remote computer where the data originated.
If AMSec is omitted, the default timeout value used is ReceiveTimeout. Use TIdTimeoutInfinite to disable read timeout. If a timeout occurs while waiting for the socket Binding to become readable, VPeerIP is set to '' and VPeerPort is set to 0 before exiting from the method.
ReceiveString will read up to BufferSize bytes from the remote computer.
ReceiveString will raise an EIdException exception if the number of bytes read from the remote computer is 0.
Reads data from the remote computer.
Parameters
const AMSec: Integer = IdTimeoutDefault
var VPeerIP: string
Address of the remote computer.
var VPeerPort: integer
Port number of the remote computer.
Return Value
String - Data received from the remote computer.
Description
ReceiveString is a String function used to receive data from a remote computer system.
VPeerIP is updated to reflect the IP address on the remote computer where the data originated. VPeerPort is updated to reflect the port number on the remote computer where the data originated.
If AMSec is omitted, the default timeout value used is ReceiveTimeout. Use TIdTimeoutInfinite to disable read timeout. If a timeout occurs while waiting for the socket Binding to become readable, VPeerIP is set to '' and VPeerPort is set to 0 before exiting from the method.
ReceiveString will read up to BufferSize bytes from the remote computer.
ReceiveString will raise an EIdException exception if the number of bytes read from the remote computer is 0.
Sends data to a remote computer system.
procedure Send(AHost: string; const APort: Integer; const AData: string);
Parameters
AHost: string
Address of the remote computer.
const APort: Integer
Port number on the remote computer.
const AData: string
Value to
send to the remote computer.
Description
Send is a procedure used to
send the data specified in AData to the remote computer system specified in AHost and APort. Send will resolve the address in AHost to a host name prior to sending AData to the remote system.
Send calls Binding.SendTo to transmit AData to the remote system. UDP does not guarantee delivery of data packets, so the values in AData may not arrive at the remote computer system.
Sends data to a remote host.
procedure SendBuffer(AHost: string; const APort: Integer; var ABuffer; const AByteCount: integer);
Parameters
AHost: string
Address for the remote connection.
const APort: Integer
Port number for the remote connection.
var ABuffer
Data to be sent to the remote connection.
const AByteCount: integer
Number of bytes to
send to the remote connection.
Description
SendBuffer is a procedure used to transmit data to a remote connection. AHost and APort identify the remote system that is the endpoint for the connection. Before transmitting data, SendBuffer will use the protocol stack to resolve the host name in AHost to an IP address.
ABuffer contains the data to be transmitted to the remote host.
SendBuffer uses the TIdSocketHandle.SendTo method for the socket binding to transmit data.