メインコンテンツまでスキップ
バージョン: 20 R9 BETA

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.

履歴
リリース内容
20 R9クラスを追加

例題


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

引数説明
portNumber->TCP port to listen
optionsObject->Configuration options for the listener
戻り値4D.TCPListener<-New TCPListener object

説明

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 引数

In the options parameter, pass an object to configure the listener and all the TCPConnections it creates:

プロパティ説明デフォルト
onConnectionFormulaCallback 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.未定義
onErrorFormulaエラーの場合にトリガーされるコールバック. The Formula receives the TCPListener object in $listener未定義
onTerminateFormulaCallback triggered just before the TCPListener is closed. The Formula receives the TCPListener object in $listener未定義

コールバック関数

Callback functions receive up to two parameters:

引数説明
$listenerTCPListener objectThe current TCP listener instance.
$eventTCPEvent オブジェクトイベントに関する情報が含まれているオブジェクト

コールバックの呼び出しの順番:

  1. onConnection is triggered each time a connection is established.
  2. onError はエラーが発生した場合にトリガーされます。
  3. onTerminate is always triggered just before a connection is terminated.

TCPEvent オブジェクト

コールバック関数 が呼ばれた際にはTCPEvent オブジェクトが返されます。

.errors

errors : Collection

説明

The .errors property contains a collection of error objects associated with the connection. 各エラーオブジェクトにはエラーコード、エラーの詳細、そしてそのエラーを起こしたコンポーネントの署名が格納されています。

プロパティ説明
errorsCollectionエラー発生時の 4Dエラースタック
[].errCodeNumber4Dエラーコード
[].messageText4Dエラーの詳細
[].componentSignatureTextエラーを返した内部コンポーネントの署名

.port

port : Number

説明

The .port property contains the port number of the machine. このプロパティは 読み取り専用 です。

.terminate()

.terminate()

引数説明
引数を必要としません

説明

The terminate() function closes the listener and releases the port.