TCP Plugin Overview

The TCP plugin enables the use of more advanced socket connections with SWF Studio applications. Using the TCP plugin, you can send and receive text messages using TCP streams.

The TCP plugin uses connected sockets, making two-way communication between machines possible.

Learning to use sockets is not hard, but you first have to understand the concept of a socket client and a socket server. Simply put, a server's responsibility is to first listen for an incoming connection from an unknown source. A client, on the other hand, opens a socket to a listening server. When the server gets notified that an incoming connection is being made, it creates the socket.

Once a connection is established (i.e. a socket is open), data can be sent in either direction, and the socket will remain connected until either the server or client closes the socket. Once the socket is closed, the server will again listen for another incoming connection.

It's important to understand that the "client" and "server" concepts described above apply to a given socket. Since an application can create multiple sockets simultaneously, it may be acting as both a server and a client at the same time.

To create a TCP client socket (the typical use of the TCP plugin), follow these steps:

 In order to receive messages, you must register to receive notifications regarding incoming messages. See TCP.setNotify, specifically the 'onReceive' and 'onReceiveBinary' notifications.

 Open a socket on a port of your choice using TCP.open, specifying the destination machine and port number. It is important to note that some ports have special significance, and should not be picked purely randomly. In most cases, it is safest to choose a port number between 49152 and 65535. For more information, refer to this IANA web page.

 To send a message on an open socket, use TCP.sendMsg or TCP.sendMsgBinary.

 When finished, call TCP.close to clean up. This will also clear all notifications.

To create a TCP server socket, follow these steps:

 To know when a client is connecting to your socket, you must register to receive the 'onConnect' notifications. See TCP.setNotify.

 In order to receive messages, you must register to receive notifications regarding incoming messages. See TCP.setNotify, specifically the 'onReceive' and 'onReceiveBinary' notifications.

 Start the server listening on a port of your choice using TCP.listen, specifying the port number. It is important to note that some ports have special significance, and should not be picked purely randomly. In most cases, it is safest to choose a port number between 49152 and 65535. For more information, refer to this IANA web page.

 Once a client connects, you will receive the 'onConnect' notification. At this point, you can start sending messages, using TCP.sendMsg or TCP.sendMsgBinary.

 If and when the client disconnects, you will receive an 'onDisconnect' notification, at which point the server will automatically return to the listening stage.

 When finished, and to stop the server from listening for other inbound socket requests, call TCP.close to clean up. This will also clear all notifications.