Flex and Asp.net interoperability system, socket-based network connection

Source: Internet
Author: User

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. The example 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 {
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>
 

The following figure shows the test results of the sample program in this article:

Related Article

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.