Saltar al contenido principal
Versión: Siguiente

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. Esto ocurre típicamente, por ejemplo, al final de una ejecución de un método para variables locales. 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.

4DTCPUDPLog.txt file

For debugging and monitoring, you can use the [4DTCPUDPLog.txt log file] that records events related to UDP sockets. Los eventos incluyen transmisión de datos, errores e información del ciclo de vida de la conexión.

Historia
LanzamientoModificaciones
20 R10Clase añadida

Ejemplo

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ámetrosTipoDescripción
portInteger->Local port used for UDP socket (0 or omitted = find any unused port to use)
optionsObject->Configuration options for the socket
ResultadoUDPSocket<-New UDPSocket object

Descripción

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.

Parámetro options

En el parámetro options, pase un objeto que puede contener las siguientes propiedades:

PropiedadTipoDescripciónPor defecto
onDataFormulaRetrollamada activada cuando se reciben datosIndefinido
onErrorFormulaRetrollamada en caso de errorIndefinido
onTerminateFormulaCallback triggered when the port is releasedIndefinido

Función callback (retrollamada)

Todas las funciones de retrollamada reciben dos parámetros:

ParámetrosTipoDescripción
$socketUDPSocket objectThe current UDPSocket instance.
$eventUDPEvent objectContiene información sobre el evento.

Secuencia de retrollamadas:

  1. onData se activa cada vez que se reciben datos.
  2. onError se activa si se produce un error.
  3. 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

Descripción

The .errors property contains a collection of error objects associated with the socket. Cada objeto de error incluye el código de error, una descripción y la firma del componente que causó el error.

PropiedadTipoDescripción
errorsCollectionPila de error 4D en caso de error
[].errCodeNumberCódigo de error 4D
[].messageTextDescripción del error 4D
[].componentSignatureTextFirma del componente interno que ha devuelto el error

.port

port : Number

Descripción

The .port property contains the port number to listen to. Esta propiedad es de solo lectura.

.send()

.send( data : Blob ; hostName : Text ; remotePort : Integer )

ParámetrosTipoDescripción
dataBlob->Datos a enviar
hostNameText->Name or IP address of server
remotePortInteger->Remote port to connect to (0=any)

Descripción

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.

Ver también

UDPEvent