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.
History
| Release | Changes |
|---|---|
| 20 R9 | Class added |
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.
Aynchronous programming
This class supports asynchronous programming in 4D as described in the Asynchronous Execution page.
Example
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 a collection of error objects associated with the connection |
| port : Number the port number of the machine |
| .terminate() closes the listener and releases the port |
4D.TCPListener.new()
4D.TCPListener.new( port : Number ; options : Object ) : 4D.TCPListener
| Parameter | Type | Description | |
|---|---|---|---|
| port | Real | -> | TCP port to listen |
| options | Object | -> | Configuration options for the listener |
| Result | 4D.TCPListener | <- | New TCPListener object |
Description
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:
| Property | Type | Description | Default |
|---|---|---|---|
| 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. | Undefined |
| onError | Formula | Callback triggered in case of an error. The Formula receives the TCPListener object in $listener | Undefined |
| onTerminate | Formula | Callback triggered just before the TCPListener is closed. The Formula receives the TCPListener object in $listener | Undefined |
Callback functions
Callback functions receive up to two parameters:
| Parameter | Type | Description |
|---|---|---|
| $listener | TCPListener object | The current TCP listener instance. |
| $event | TCPEvent object | Contains information about the event. |
Sequence of Callback Calls:
onConnectionis triggered each time a connection is established.onErroris triggered if an error occurs.onTerminateis always triggered just before a connection is terminated.
TCPEvent object
A TCPEvent object is returned when a callback function is called.
.errors
errors : Collection
Description
The .errors property contains a collection of error objects associated with the connection. Each error object includes the error code, a description, and the signature of the component that caused the error.
| Property | Type | Description | |
|---|---|---|---|
| errors | Collection | 4D error stack in case of error | |
| [].errCode | Number | 4D error code | |
| [].message | Text | Description of the 4D error | |
| [].componentSignature | Text | Signature of the internal component which returned the error |
.port
port : Number
Description
The .port property contains the port number of the machine. This property is read-only.
.terminate()
.terminate()
| Parameter | Type | Description | |
|---|---|---|---|
| Does not require any parameters |
Description
The terminate() function closes the listener and releases the port.