Skip to end of metadata
Go to start of metadata


Support for listening and connecting sockets via TCP will allow application developers to incorporate lower level network logic into their applications.

Sockets explained

Titanium.Network.Socket.TCP implements the Titanium.Stream interface and therefore relies on Titanium.Buffer.

Socket operations are generally asynchronous and therefore socket callbacks are core to how one operation flows into the next. For example, when connecting to a remote host, you wait until the connected callback is invoked and initiate read and write operations from inside that callback.

Once a socket is connected, IO operations leave the scope of Ti.Network.Socket and move into the realm of general Stream IO. Again, because sockets are naturally asynchronous, IO interactions with them should generally be conducted through the Ti.Stream module. Lastly, while sockets can be an extremely powerful part of your developer tool box, network programming is non-trivial and investigation into the basics of how BSD sockets work is suggested.

Usage examples

The following code excerpt creates a socket and connects to a remote server. When the socket connects, a callback function is invoked to interact with the socket. The callback function is shown in the next excerpt.

The following code excerpt shows the postConnect function used in the previous excerpt. It writes a message to the socket and then enters a read loop, reading data from the socket until the socket is closed.

The following sample creates a listening socket – in this case, listening on the loopback address, which can be used for communicating with other processes on the same device. iOS also supports Ti.Platform.address (the address of the WiFi interface). Android supports only the loopback address.

  • No labels