Flash/flex also supports network connections based on sockets, and server-side can be developed in any language, such as C++,vb,c#,java. Listening on a network port allows you to receive connections from Flash/flex-developed clients.
ActionScript 3.0 provides a way to communicate with the server side through a socket connection. This is an important feature that transcends the traditional B/s structure. In this way, network communication can be connected instantly, avoiding the disadvantage of stateless connection of HTTP protocol. ActionScript 3.0 uses the Xmlsocket class to connect. Note that when you use the Xmlsocket class for a socket connection, you cannot automatically traverse the firewall. To connect through a firewall, you need to use the RTMP protocol based on the HTTP protocol.
You can see from the API documentation provided by Adobe that Xmlsocket provides four public methods:
1, Xmlsocket (host:string=null,port:int=0)--Create a new Xmlsocket object.
2, Close (): void--closes a xmlsocket.
3, Connect (host:string,port:int): void--connected to the specified TCP port.
4, Send (object:*): void--to send data to the connection server.
OK, after this, we can use Xmlsocket to develop a network based on socket for timely communication applications. The following is provided by C # to provide a socket's service side and listen for port 8888. The sample program is as follows:
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Net.Sockets;
6 using System.IO;
7
8 namespace Flashflexdotnet
9 {
Class Program
11 {
static void Main (string[] args)
13 {
TcpListener Listener;
15
Try
17 {
Listener = new TcpListener (8888);
19}
catch (Exception ex)
21 {
Console.WriteLine (ex. message);
return;
24}
25
Listener. Start ();
Console.WriteLine ("Server started, waiting for client to connect.");
BOOL loop = true;
29
while (Loop)
31 {
Socket s = listener. AcceptSocket ();
NetworkStream ns = new NetworkStream (s);
StreamReader reader = new StreamReader (NS);
string result = String. Empty;
A try
37 {
result = reader. ReadLine ();
Console.WriteLine (result);
40}
catch (Exception ex)
42 {
Console.WriteLine (ex. message);
44}
45}
46}
47}
48}