TcpServer

Description

The TcpServer object provides a TCP server which listens for incoming TCP connections at a given port.

This object was introduced in InCore 2.3.

› Inherits:Object

Properties

connections

This property holds a list of open connections. Whenever a socket is closed, it is removed from the list and destroyed automatically.

› Type:List<TcpSocket>
› Signal:connectionsChanged()
› Attributes:Readonly

listening

This property holds whether the server should listen for incoming connections.

› Type:Boolean
› Default:true
› Signal:listeningChanged()
› Attributes:Writable

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

port

This property holds the network port number which to listen at for incoming connections.

› Type:SignedInteger
› Default:0
› Signal:portChanged()
› Attributes:Writable

Signals

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 IpSocket.Error.

connectionAccepted(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.

connectionsDataChanged(SignedInteger index)

This signal is emitted whenever the List.dataChanged() signal is emitted, i.e. the item at index in the connections list itself emitted the dataChanged() signal.

listenError(SignedInteger error)

This signal is emitted whenever an error occurs while trying to listen at the specified port. The error code is provided in the first argument and corresponds to IpSocket.Error.

This signal was introduced in InCore 2.6.

Example

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())
                                                               } );
                              }
    }
}