Class TNetCom

DescriptionHierarchyInternal TypesFieldsMethodsProperties

Unit

Declaration

type TNetCom = class(TObject)

Description

A simple communication component that can transmit and receive messages through UDP protocol over local network and/or Internet.

Hierarchy

  • TObject
  • TNetCom

Overview

Internal Types

Published TReceiveEvent = procedure(const Sender: TObject; const Host: StdString; const Port: Integer; const Data: Pointer; const Size: Integer) of object;

Methods

Public function Initialize: Boolean;
Public function ResolveHost(const Host: StdString): StdString;
Public function ResolveIP(const IPAddress: StdString): StdString;
Public function Send(const Host: StdString; const Port: Integer; const Data: Pointer; const Size: Integer): Boolean;
Public procedure Finalize;
Public procedure ResetStatistics;
Public procedure Update;

Properties

Public property Broadcast: Boolean read FBroadcast write SetBroadcast;
Public property BytesPerSec: Integer read FBytesPerSec;
Public property BytesReceived: Integer read FBytesReceived;
Public property BytesSent: Integer read FBytesSent;
Public property Initialized: Boolean read FInitialized;
Public property LocalIP: StdString read GetLocalIP;
Public property LocalPort: Integer read FLocalPort write SetLocalPort;
Public property OnReceive: TReceiveEvent read FOnReceive write FOnReceive;
Public property ReceivedPackets: Integer read FReceivedPackets;
Public property SentPackets: Integer read FSentPackets;
Public property UpdateRefreshTime: Integer read FUpdateRefreshTime write SetUpdateRefreshTime;

Description

Internal Types

Published TReceiveEvent = procedure(const Sender: TObject; const Host: StdString; const Port: Integer; const Data: Pointer; const Size: Integer) of object;

Data reception event. In this event the incoming message should be interpreted and properly handled. After this event executes, memory referenced by provided pointers is lost; therefore, to preserve the message it is necessary to copy it somewhere within this event. Source host and port can be used to identify the receiver and for sending replies.

Parameters
Sender
Reference to the class that received the message, usually TNetCom.
Host
Source host that sent the message.
Port
Source port through which the message was sent.
Data
Pointer to the beginning of message block.
Size
Size of the message block.

Methods

Public function Initialize: Boolean;

Initializes the component and begins listening to the given port for incoming messages. LocalPort should be set before calling this function to set a specific listening port. If LocalPort remains zero before this call, the listening port will be selected by the system from among available ones and LocalPort will be updated to reflect this. True is returned when the operation was successful and False otherwise.

Public function ResolveHost(const Host: StdString): StdString;

Converts text containing host address into the corresponding IP address.

Public function ResolveIP(const IPAddress: StdString): StdString;

Converts text containing IP address into the corresponding host string.

Public function Send(const Host: StdString; const Port: Integer; const Data: Pointer; const Size: Integer): Boolean;

Sends the specified message data block to the destination.

Parameters
Host
Destination host or address where the message should be sent. Multicast and broadcast addresses are accepted, although should be used with care to not saturate the local network.
Port
Destination port where the receiver is currently listening at.
Data
Pointer to the message data block. The method copies the data to its internal structures, so it's not necessary to maintain the buffer after this call exits.
Size
Size of the message data block.
Returns

True when the packet was sent successfully and False when there were errors. It is important to note that since messages are sent through UDP protocol, True return value doesn't necessarily mean that the packet was actually received.

Public procedure Finalize;

Finalizes the component and closes the communication link.

Public procedure ResetStatistics;

Resets all statistic parameters related to the current session such as number of packets transmitted, bytes per second among others.

Public procedure Update;

Handles internal communication and receives incoming messages; in addition, internal structures and bandwidth usage are also updated. This method should be called as fast as possible and no less than once per second. During the call to this method, OnReceive event may occur to notify the reception of messages.

Properties

Public property Broadcast: Boolean read FBroadcast write SetBroadcast;

Determines whether the communication should support broadcast and multicast messages. This can be written to only before the component is initialized, but can be read from at any time.

Public property BytesPerSec: Integer read FBytesPerSec;

Indicates the current bandwidth usage in bytes per second. In order for this variable to have meaningful values, it is necessary to call Update method at least once per second.

Public property BytesReceived: Integer read FBytesReceived;

Indicates how many bytes were received during the entire session.

Public property BytesSent: Integer read FBytesSent;

Indicates how many bytes were sent during the entire session.

Public property Initialized: Boolean read FInitialized;

Indicates whether the component has been properly initialized.

Public property LocalIP: StdString read GetLocalIP;

Returns IP address of current machine. If several IP addresses are present, the last address in the list is returned.

Public property LocalPort: Integer read FLocalPort write SetLocalPort;

Local port that is used for listening and transmitting packets. This can be written to only before the component is initialized, but can be read from at any time.

Public property OnReceive: TReceiveEvent read FOnReceive write FOnReceive;

This event occurs when the data has been received. It should always be assigned to interpret any incoming messages.

Public property ReceivedPackets: Integer read FReceivedPackets;

Indicates how many packets in total were received during the entire session.

Public property SentPackets: Integer read FSentPackets;

Indicates how many packets were sent during the entire session.

Public property UpdateRefreshTime: Integer read FUpdateRefreshTime write SetUpdateRefreshTime;

Time interval (in milliseconds) to consider for "BytesPerSec" calculation.


Copyright © 2000 - 2017 Yuriy Kotsarenko. Help files generated by PasDoc.