TCPListener
The TCPListener
class allows you to create and configure a TCP server in 4D. Once the TCP listener is instantiated, you can receive client TCP connections and communicate using any protocol supporting TCP.
The TCPListener
class is available from the 4D
class store. Puede crear un servidor TCP utilizando la función 4D.TCPListener.new(), que devuelve un objeto TCPListener.
All TCPListener
class functions are thread-safe.
Historia
Lanzamiento | Modificaciones |
---|---|
20 R9 | Clase añadida |
Ejemplo
property listener : 4D.TCPListener
Class constructor($port : Integer)
This.listener:=4D.TCPListener.new($port; This)
Function terminate()
This.listener.terminate()
Function onConnection($listener : 4D.TCPListener; $event : 4D.TCPEvent)->$result
//when connected, start a server to handle the communication
If($event.address # "192.168.@")
$result:=Null //in some cases you can reject the connection
Else
$result:=cs.MyAsyncTCPConnection.new(This) //see TCPConnection class
End if
Function onError($listener : 4D.TCPListener; $event : 4D.TCPEvent)
Function onTerminate($listener : 4D.TCPListener; $event : 4D.TCPEvent)
Ver ejemplo en la clase TCPConnection para una descripción de la clase de usuario MyAsyncTCPConnection.
Objeto TCPListener
A TCPListener object is a shared object.
TCPListener objects provide the following properties and functions:
errors : Collection una colección de objetos de error asociados con la conexión |
port : Number el número de puerto de la máquina |
.terminate() cierra el oyente y libera el puerto |
4D.TCPListener.new()
4D.TCPListener.new( port : Number ; options : Object ) : 4D.TCPListener
Parámetros | Tipo | Descripción | |
---|---|---|---|
port | Number | -> | Puerto TCP de escucha |
options | Object | -> | Configuration options for the listener |
Resultado | 4D.TCPListener | <- | New TCPListener object |
Descripción
La función 4D.TCPListener.new()
crea un nuevo servidor TCP escuchando el puerto especificado usando las options definidas, y devuelve un objeto 4D.TCPListener
.
Parámetro options
In the options parameter, pass an object to configure the listener and all the TCPConnections
it creates:
Propiedad | Tipo | Descripción | Por defecto |
---|---|---|---|
onConnection | Formula | Callback when a new connection is established. The Formula receives two parameters ($listener and $event, see below) and must return either null/undefined to prevent the connection or an option object that will be used to create the TCPConnection . | Indefinido |
onError | Formula | Callback triggered in case of an error. The Formula receives the TCPListener object in $listener | Indefinido |
onTerminate | Formula | Callback triggered just before the TCPListener is closed. The Formula receives the TCPListener object in $listener | Indefinido |
Función callback (retrollamada)
Callback functions receive up to two parameters:
Parámetros | Tipo | Descripción |
---|---|---|
$listener | TCPListener object | The current TCP listener instance. |
$event | objeto TCPEvent | Contiene información sobre el evento. |
Secuencia de retrollamadas:
onConnection
is triggered each time a connection is established.onError
se activa si se produce un error.onTerminate
is always triggered just before a connection is terminated.
Objeto TCPEvent
Un objeto TCPEvent
es devuelto cuando se llama una función de retrollamada.
.errors
errors : Collection
Descripción
La propiedad .errors
contiene una colección de objetos de error asociados con la conexión. Cada objeto de error incluye el código de error, una descripción y la firma del componente que causó el error.
Propiedad | Tipo | Descripción | |
---|---|---|---|
errors | Collection | Pila de error 4D en caso de error | |
[].errCode | Number | Código de error 4D | |
[].message | Text | Descripción del error 4D | |
[].componentSignature | Text | Firma del componente interno que ha devuelto el error |
.port
port : Number
Descripción
La propiedad .port
contiene el número de puerto de la máquina. Esta propiedad es de solo lectura.
.terminate()
.terminate()
Parámetros | Tipo | Descripción | |
---|---|---|---|
No requiere ningún parámetro |
Descripción
La función terminate()
cierra el oyente y libera el puerto.