Flash/Flex also supports Socket-based network connection, and the server side can be developed in any language such as C++, VB, C#, Java, etc. By listening to a network port, you can receive connections from clients developed by Flash/Flex.
ActionScript 3.0 provides communication with the server through Socket connection. This is an important feature that surpasses the traditional B/S structure. This allows network communication to be connected instantly and avoids the shortcomings of stateless connections in the HTTP protocol. ActionScript 3.0 uses the XMLSocket class for connection. It should be noted that when using the XMLSocket class for Socket connection, it cannot automatically pass through the firewall. To connect through the firewall, you need to use the RTMP protocol based on the HTTP protocol.
By consulting the API documentation provided by Adobe, we can learn that XMLSocket provides four public methods:
1. XMLSocket(host:String=null,port:int=0)--Create a new XMLSocket object.
2. close():void--Close an XMLSocket.
3. connect(host:String,port:int):void--Connect to the specified TCP port.
4. send(object:*):void--Send data to the connection server.
OK, after understanding this, we can use XMLSocket to develop Socket-based network instant communication applications. The following provides a Socket server through C# and listens to port 8888. The sample program is as follows:
1privatefunctionconnectionServer():void
2{
3xmlConn=newXMLSocket();
5}
Then, you can send a message to the Socket server through the instance method send() of XMLSocket. The following code definition:
1
2
4backgroundGradientColors="[#CDCAE6,#FFFFFF]">
5
67importmx.controls.Alert;
8
9privatevarxmlConn:XMLSocket;
10
11privatefunctionconnectionServer():void
12{
13xmlConn=newXMLSocket();
14xmlConn.connect("127.0.0.1",8888);
15}
16
17privatefunctiononSend():void
18{
19xmlConn.send(txtData.text+"n");
20}
21]]>
22
23
24
25
26