ActionScript 3.0 provides socket connection to communicate with the server. This is an important feature that surpasses the traditional B/S structure. In this way, network communication can be instantly connected, avoiding the disadvantages of stateless HTTP connections. ActionScript 3.0 uses the xmlsocket class for connection. It should be noted that when the xmlsocket class is used for socket connection, it cannot automatically pass through the firewall. To connect through the firewall, you must use the HTTP-based rtmp protocol.
You can see from the API documentation provided by Adobe that xmlsocket provides four public methods:
, Xmlsocket (HOST: String = NULL, Port: Int = 0) -- create a new xmlsocket object.
Close (): void -- close an xmlsocket.
, Connect (HOST: String, Port: INT): void -- connect to the specified TCP port.
, Send (Object: *): void -- send data to the connection server.
OK. After understanding this, we can use xmlsocket to develop socket-based network communication applications in a timely manner. The following uses C # To provide a socket server and listen on port 8888. Example Program As follows:
CopyCode The Code is as follows: using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. net. Sockets;
Using system. IO;
Namespace flashflexdotnet
{
Class Program
{
Static void main (string [] ARGs)
{
Tcplistener listener;
Try
{
Listener = new tcplistener (8888 );
}
Catch (exception ex)
{
Console. writeline (ex. Message );
Return;
}
Listener. Start ();
Console. writeline ("server startup, waiting for client connection .");
Bool loop = true;
While (loop)
{
Socket S = listener. acceptsocket ();
Networkstream NS = new networkstream (s );
Streamreader reader = new streamreader (NS );
String result = string. empty;
Try
{
Result = reader. Readline ();
Console. writeline (result );
}
Catch (exception ex)
{
Console. writeline (ex. Message );
}
}
}
}
}
The socket on the server has been prepared. Next, let's take a look at how the xmlsocket of the client terminal establishes a socket connection to communicate with the. NET socket server.
As described earlier in this article, actionscript 3.0 provides a socket-based network connection class xmlsocket, which we can use directly to develop socket-based network communication. Follow these steps to establish a network connection between the base and the xmlsocket provided by ActionScript 3.0:Copy codeThe Code is as follows: Private function connectionserver (): void
{
Xmlconn = new xmlsocket ();
Xmlconn. Connect ("127.0.0.1", 8888 );
}
Then, you can use the xmlsocket instance method to send messages to the socket server. The following code defines:Copy codeThe Code is as follows: Private function onsend (): void
{
Xmlconn. Send (txtdata. Text + "\ n ");
}
Complete client code: Copy codeThe Code is as follows: <? XML version = "1.0" encoding = "UTF-8"?>
<Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" layout = "absolute"
Backgroundgradientalphas = "[1.0, 1.0]"
Backgroundgradientcolors = "[# cdcae6, # ffffff]">
<Mx: SCRIPT>
<! [CDATA [
Import MX. Controls. Alert;
Private var xmlconn: xmlsocket;
Private function connectionserver (): void
{
Xmlconn = new xmlsocket ();
Xmlconn. Connect ("127.0.0.1", 8888 );
}
Private function onsend (): void
{
Xmlconn. Send (txtdata. Text + "\ n ");
}
]>
</MX: SCRIPT>
<Mx: textarea x = "43" Y = "34" Height = "120" width = "263" id = "txtdata"/>
<Mx: button x = "93" Y = "180" label = "onserver ()"/>
<Mx: button x = "190" Y = "180" label = "send" fontsize = "12" Click = "onsend ()"/>
</MX: Application>
The following figure shows the test results of the sample program in this article: