Preview article: socket-based network connection between flex and. net

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 communication with the server through socket connection is provided. This is an important feature that surpasses the traditional B/S structure. In this way, network communication can be instantly connected, avoiding the stateless HTTP protocol.
Connection disadvantages. ActionScript
3.0 use 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. Http://www.my400800.cn

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:

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
{< br> listener = new tcplistener (8888);
}< br> catch (exception ex)
{< br> 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, actionscript 3.0 provides a socket-based network connection class xmlsocket,

We can directly use it 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:

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. As follows:CodeDefinition:

Private function onsend (): void
{
Xmlconn. Send (txtdata. Text + "\ n ");
}
Complete user code:

<? 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>

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.