Use of socket in flex

Source: Internet
Author: User

Example of socket interaction with other languages (such as Java) in flex.

After Adobe Flash Player is upgraded to 9.0.124, the original socket connection method cannot be used due to the modification of the security policy. Instead, the new security policy method must be used for verification, the specific process is as follows:

1. Check whether the security policy file is provided on port 843 of the server;

2. If Step 1 is not detected, theCodeWhether Security. loadpolicyfile (xmlsocket: //) is used for security verification. If not, perform Step 2 verification;

3. Check the security policy file on the target port

If all the above three steps fail to be detected, Flash Player rejects connection to the target server. Socket Connection Failed. The general idea on the socket side is to listen to port 843 and the target port. When Flash Player connects to port 843 for the first time, and send "<policy-file-Request/>" as the verification flag. If 843 provides a security policy, port 843 returns the policy file to Flash Player in string mode, if Flash Player is running in the security policy file to connect to the target port, port 843 is disabled, and Flash Player starts a socket to connect to the target port. So far, the socket connection is successful.

The security policy file format is as follows:

<? XML version = "1.0"?> <! Doctype cross-domain-Policy System "/XML/dtds/cross-domain-policy.dtd" & gt; <! -- Policy file for xmlsocket: // socks.example.com --> <cross-domain-Policy> <! -- This is a master-policy file --> <site-control permitted-Cross-Domain-policies = "Master-only"/> <! -- Instead of setting to-ports = "*", administrators can use ranges and commas --> <! -- This will allow access to ports 123,456,457, and 458 --> <allow-access-from domain = "*" to-ports = "10000"/> </Cross-Domain-Policy>

For more information, see Adobe's official document: http://www.adobe.com/devnet/flashplayer/articles/socket_policy_files.html

Flash/flex also supports socket-based network connections. The server can be developed in any language, such as C ++, VB, C #, and Java. Listening to a network port can receive the connection from the client developed by Flash/flex.

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:

1. xmlsocket (HOST: String = NULL, Port: Int = 0) -- creates an 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 communication applications in a timely manner. The following uses C # To provide a socket server and listen on port 8888. ExampleProgramAs 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 {

10 class Program

11 {

12 static void main (string [] ARGs)

13 {

14 tcplistener listener;

15

16 try

17 {

18 listener = new tcplistener (8888 );

19}

20 catch (exception ex)

21 {

22 console. writeline (ex. Message );

23 return;

24}

25

26 listener. Start ();

27 console. writeline ("server startup, waiting for client connection .");

28 bool loop = true;

29

30 While (loop)

31 {

32 socket S = listener. acceptsocket ();

33 networkstream NS = new networkstream (s );

34 streamreader reader = new streamreader (NS );

35 string result = string. empty;

36 try

37 {

38 result = reader. Readline ();

39 console. writeline (result );

40}

41 catch (exception ex)

42 {

43 console. writeline (ex. Message );

44}

45}

46}

47}

48}

49

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:

1 private function connectionserver (): void

2 {

3 xmlconn = new xmlsocket ();

4 xmlconn. Connect ("127.0.0.1", 8888 );

5}

Then, you can use the xmlsocket instance method to send messages to the socket server. The following code defines:

1 private function onsend (): void

2 {

3 xmlconn. Send (txtdata. Text + "\ n ");

4}

Complete client code:

1 <? XML version = "1.0" encoding = "UTF-8"?>

2 <mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" layout = "absolute"

3 backgroundgradientalphas = "[1.0, 1.0]"

4 backgroundgradientcolors = "[# cdcae6, # ffffff]">

5 <mx: SCRIPT>

6 <! [CDATA [

7 Import MX. Controls. Alert;

8

9 private var xmlconn: xmlsocket;

10

11 private function connectionserver (): void

12 {

13 xmlconn = new xmlsocket ();

14 xmlconn. Connect ("maid", 8888 );

15}

16

17 private function onsend (): void

18 {

19 xmlconn. Send (txtdata. Text + "\ n ");

20}

21]>

22 </MX: SCRIPT>

23 <mx: textarea x = "43" Y = "34" Height = "120" width = "263" id = "txtdata"/>

24 <mx: button x = "93" Y = "180" label = "onserver ()"/>

25 <mx: button x = "190" Y = "180" label = "send" fontsize = "12" Click = "onsend ()"/>

26 </MX: Application>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.