The XMLSocket class implements client sockets, so that the computer running Flash Player can communicate with the server computer identified by the IP address or domain name. XMLSocket is useful for client/server applications that require a short lag time, such as real-time chat systems. The traditional HTTP-based chat solution frequently polls servers and uses HTTP requests to download new messages. In contrast, the XMLSocket chat solution maintains an open connection with the server, which allows the server to instantly send incoming messages without sending requests from the client. To use the XMLSocket class, the server computer must run a daemon that identifies the protocol used by the XMLSocket class. XMLSocket must comply with the following protocols:
- XML messages are sent through full-duplex TCP/IP stream socket connection.
- Each XML message is a complete XML document, ending with a zero (0) byte.
- There is no limit on the number of XML messages sent and received through the XMLSocket connection.
XMLSocket.connect()You can only connect to a TCP port with a port number greater than or equal to 1024. One consequence of this restriction is that the port number allocated to the server daemon that communicates with the XMLSocket object must be greater than or equal to 1024. Ports with a port less than 1024 are usually used for system services (such as FTP, Telnet, and HTTP). Therefore, XMLSocket objects are prohibited from using these ports for security reasons. Such port restrictions can reduce the possibility of improper access and abuse of these resources.
Flash Security needs to be considered when XMLSocket is used:
- If the SWF file to be called is in a sandbox that can only interact with the content of the local file system, and the target resource is from the network sandbox, data cannot be sent or received.
- If the SWF file to be called comes from the network sandbox and the target resource is local, data cannot be sent or received.
- The SWF file to be called and the accessed network resource must be in the same domain. For example, the SWF file at www.cnblogs.com can only send (or receive) data to the source at www.cnblogs.com.
- A website can use a cross-origin policy file to allow cross-origin access to resources.