UDPSocket
The UDPSocket
class allows you to send and receive UDP packets. UDP (User Datagram Protocol) is an easy-to-implement protocol for sending data. It is faster and simpler than TCP (only 8 bytes of header as opposed to at least 20 bytes in TCP), but it does not offer the same level of reliability. It is useful for applications where data must arrive at their destination quickly. However, it does not allow verification of delivery, nor does it allow error-checking or recovery of data that was not delivered correctly.
The UDPSocket
class is available from the 4D
class store. You can create a UDP connection using the 4D.UDPSocket.new() function, which returns a UDPSocket object.
Thanks to the standard 4D object refcounting, a UDPSocket is automatically released when it is no longer referenced, i.e. when no more references to them exist in memory. Isso geralmente ocorre, por exemplo, no final da execução de um método para variáveis locais. Consequently, the associated resources are properly cleaned up without requiring explicit closure. However, if you want to "force" the closure of a socket at any moment, nullify its references by setting them to Null.
For debugging and monitoring, you can use the [4DTCPUDPLog.txt log file] that records events related to UDP sockets. Events include data transmission, errors, and connection lifecycle information.
História
Release | Mudanças |
---|---|
20 R10 | Classe adicionada |
Exemplo
UDPSocket Object
A UDPSocket object is immutable, non streamable.
UDPSocket objects provide the following properties and functions:
errors : Collection a collection of error objects associated with the socket |
port : Number the port number to listen to |
.send( data : Blob ; hostName : Text ; remotePort : Integer ) sends data to the remote hostName server on the specified remotePort |
4D.UDPSocket.new()
4D.UDPSocket.new( options : Object ) : 4D.UDPSocket
4D.UDPSocket.new( port : Integer ; options : Object ) : 4D.UDPSocket
Parâmetro | Tipo | Descrição | |
---|---|---|---|
port | Integer | -> | Local port used for UDP socket (0 or omitted = find any unused port to use) |
options | Object | -> | Configuration options for the socket |
Resultados | UDPSocket | <- | New UDPSocket object |
Descrição
The 4D.UDPSocket.new()
function creates a new UDP socket using the defined options on the specified port (if any) or on a random unused port, and returns a 4D.UDPSocket
object.
options
parameter
No parâmetro options, passe um objeto que possa conter as seguintes propriedades:
Propriedade | Tipo | Descrição | Por padrão |
---|---|---|---|
onData | Formula | Callback acionado quando os dados forem recebidos | Indefinido |
onError | Formula | Callback acionado no caso de um erro | Indefinido |
onTerminate | Formula | Callback triggered when the port is released | Indefinido |
Funções Callback
Todas as funções de chamada de retorno recebem dois parâmetros de objectos:
Parâmetro | Tipo | Descrição |
---|---|---|
$socket | UDPSocket object | The current UDPSocket instance. |
$event | UDPEvent object | Contém informações sobre o evento. |
Sequência de chamadas de retorno:
onData
é acionado cada vez que os dados são recebidos.onError
é acionado se ocorrer um erro.onTerminate
is always triggered just before the port is released (socket is closed or an error occured).
UDPEvent object
A UDPEvent
object is returned when a callback function is called.
.errors
errors : Collection
Descrição
The .errors
property contains a collection of error objects associated with the socket. Cada objeto de erro inclui o código de erro, uma descrição e a assinatura do componente que causou o erro.
Propriedade | Tipo | Descrição | |
---|---|---|---|
errors | Collection | pilha de erros 4D em caso de erro | |
[].errCode | Number | Código de erro 4D | |
[].message | Text | Descrição do erro 4D | |
[].componentSignature | Text | Assinatura da componente interna que devolveu o erro |
.port
port : Number
Descrição
The .port
property contains the port number to listen to. Essa propriedade é somente leitura.
.send()
.send( data : Blob ; hostName : Text ; remotePort : Integer )
Parâmetro | Tipo | Descrição | |
---|---|---|---|
data | Blob | -> | Dados a serem enviados |
hostName | Text | -> | Name or IP address of server |
remotePort | Integer | -> | Remote port to connect to (0=any) |
Descrição
The send()
function sends data to the remote hostName server on the specified remotePort.
hostName is the name or IP address of the server where the data will be sent.
remotePort is the number of the port to be connected to. If you pass 0, any available port will be used.