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. You can create a TCP server using the 4D.TCPListener.new() function, which returns a TCPListener object.
All TCPListener
class functions are thread-safe.
História
Release | Mudanças |
---|---|
20 R9 | Classe adicionada |
Exemplo
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)
See example in TCPConnection class for a description of the MyAsyncTCPConnection user class.
TCPListener Object
A TCPListener object is a shared object.
TCPListener objects provide the following properties and functions:
errors : Collection uma coleção de objetos de erros associados à conexão |
port : Number o número da porta da máquina |
.terminate() fecha o listener e libera a porta |
4D.TCPListener.new()
4D.TCPListener.new( port : Number ; options : Object ) : 4D.TCPListener
Parâmetro | Tipo | Descrição | |
---|---|---|---|
port | Number | -> | TCP port to listen |
options | Object | -> | Configuração opções para o ouvinte |
Resultados | 4D.TCPListener | <- | New TCPListener object |
Descrição
The 4D.TCPListener.new()
function creates a new TCP server listening to the specified port using the defined options, and returns a 4D.TCPListener
object.
options
parameter
In the options parameter, pass an object to configure the listener and all the TCPConnections
it creates:
Propriedade | Tipo | Descrição | Por padrão |
---|---|---|---|
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. A fórmula recebe o objeto TCPListener em $listener | Indefinido |
onTerminate | Formula | Callback triggered just before the TCPListener is closed. A fórmula recebe o objeto TCPListener em $listener | Indefinido |
Funções Callback
Callback functions receive up to two parameters:
Parâmetro | Tipo | Descrição |
---|---|---|
$listener | TCPListener object | The current TCP listener instance. |
$event | objeto TCPEvent | Contém informações sobre o evento. |
Sequência de chamadas de retorno:
onConnection
is triggered each time a connection is established.onError
é acionado se ocorrer um erro.onTerminate
is always triggered just before a connection is terminated.
Objeto TCPEvent
Um objeto TCPEvent
é retornado quando uma função de callback é chamada.
.errors
errors : Collection
Descrição
A propriedade .errors
contém uma coleção de objetos de erros associados à conexão. 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
A propriedade .port
contém o número da porta da máquina. Essa propriedade é somente leitura.
.terminate()
.terminate()
Parâmetro | Tipo | Descrição | |
---|---|---|---|
Não exige nenhum parâmetro |
Descrição
A função terminate()
fecha o listener e libera a porta.