Basic Usage
TCP/UDP
Drop a TLTCPComponent or TLUDPComponent on a form. Assign OnAccept, OnError, OnReceive, OnConnect and OnDisconnect event handlers. Note that this is optional but in most cases required for functionality.
Client connection is initialized with the Connect call. Arguments are IP address/hostname and port respectivly. Non-blocking connect is used, this means that you DON’T know that you’re connected until OnConnect event is fired.
Server connection is initialized with the Listen* call. Argument is port.
Note: address is a string representation of IP eg: ‘127.0.0.1’ or a hostname like ‘localhost’.
Sending data to the other side is accomplished with the Send/SendMessage method.
Events of TLNetComponent class.
- OnError – this event is fired whenever a connection error occurs. If no handler is assigned an excepion with “msg” is raised. Arguments are: msg – contains string and numerical representation of error message formatted in native encoding or UTF8 (depending on ForceUTF8 property). aSocket is the socket at which the error occured, nil means that the error is not a socket specific one.
- OnConnect – This event is fired when the client succesfuly connects. aSocket is the socket which connected. It should serve as the “start” point for clients.
- OnAccept – this event is fired whenever a connection is accepted on a server. If no handler is assigned nothing is done. aSocket contains the socket which accepted the connection.
- OnDisconnect – this event is fired whenever a connection is lost. If no handler is assigned nothing is done. aSocket contains the socket which got disconnected.
- OnReceive – This event is fired whenever new data is ready to be received. If no handler is assigned nothing is done, but a system buffer fill may happen which will in turn fire an OnError event. aSocket is the socket at which the data got received. Data is read by Get/GetMessage method.
- OnCanSend – This event is fired whenever new data can be sent again on a socket, after a call to send function failed, returning 0. It can be used to automate sending of big chunks. aSocket is socket on which send can be called.