.. _object_TcpServer: :index:`TcpServer` ------------------ Description *********** The TcpServer object provides a TCP server which listens for incoming TCP connections at a given :ref:`port `. This object was introduced in InCore 2.3. :**› Inherits**: :ref:`Object ` Overview ******** Properties ++++++++++ .. hlist:: :columns: 1 * :ref:`connections ` * :ref:`listening ` * :ref:`localHost ` * :ref:`port ` * :ref:`Object.objectId ` * :ref:`Object.parent ` Methods +++++++ .. hlist:: :columns: 1 * :ref:`Object.deserializeProperties() ` * :ref:`Object.fromJson() ` * :ref:`Object.serializeProperties() ` * :ref:`Object.toJson() ` Signals +++++++ .. hlist:: :columns: 1 * :ref:`acceptError() ` * :ref:`connectionAccepted() ` * :ref:`connectionsDataChanged() ` * :ref:`listenError() ` * :ref:`Object.completed() ` Properties ********** .. _property_TcpServer_connections: .. _signal_TcpServer_connectionsChanged: .. index:: single: connections connections +++++++++++ This property holds a list of open connections. Whenever a socket is closed, it is removed from the list and destroyed automatically. :**› Type**: :ref:`List `\<:ref:`TcpSocket `> :**› Signal**: connectionsChanged() :**› Attributes**: Readonly .. _property_TcpServer_listening: .. _signal_TcpServer_listeningChanged: .. index:: single: listening listening +++++++++ This property holds whether the server should listen for incoming connections. :**› Type**: Boolean :**› Default**: ``true`` :**› Signal**: listeningChanged() :**› Attributes**: Writable .. _property_TcpServer_localHost: .. _signal_TcpServer_localHostChanged: .. index:: single: localHost localHost +++++++++ This property holds whether the server should listen for incoming connections on the local loopback interface only. :**› Type**: Boolean :**› Default**: ``false`` :**› Signal**: localHostChanged() :**› Attributes**: Writable .. _property_TcpServer_port: .. _signal_TcpServer_portChanged: .. index:: single: port port ++++ This property holds the network port number which to listen at for incoming connections. :**› Type**: SignedInteger :**› Default**: ``0`` :**› Signal**: portChanged() :**› Attributes**: Writable Signals ******* .. _signal_TcpServer_acceptError: .. index:: single: acceptError acceptError(SignedInteger error) ++++++++++++++++++++++++++++++++ This signal is emitted whenever an error occurs while accepting a new incoming connection. The error code is provided in the first argument and corresponds to :ref:`IpSocket.Error `. .. _signal_TcpServer_connectionAccepted: .. index:: single: connectionAccepted connectionAccepted(:ref:`TcpSocket ` connection) ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ This signal is emitted whenever a connection has been accepted successfully. The connection is provided in the first argument and is ready to be read from or written to. .. _signal_TcpServer_connectionsDataChanged: .. index:: single: connectionsDataChanged connectionsDataChanged(SignedInteger index) +++++++++++++++++++++++++++++++++++++++++++ This signal is emitted whenever the :ref:`List.dataChanged() ` signal is emitted, i.e. the item at ``index`` in the :ref:`connections ` list itself emitted the dataChanged() signal. .. _signal_TcpServer_listenError: .. index:: single: listenError listenError(SignedInteger error) ++++++++++++++++++++++++++++++++ This signal is emitted whenever an error occurs while trying to listen at the specified :ref:`port `. The error code is provided in the first argument and corresponds to :ref:`IpSocket.Error `. This signal was introduced in InCore 2.6. .. _example_TcpServer: Example ******* .. code-block:: qml import InCore.Foundation 2.5 Application { TcpServer { port: 1234 onListenError: (error) => { if (error === IpSocket.AddressInUseError) { console.log("ERROR: port is already in use") } } onConnectionAccepted: (connection) => { connection.write("Hello world\n") connection.readyRead.connect( () => { console.log("Client sent:", connection.readAll()) } ); } } }