Network connection based on socket Flex and. NET Interop (i) _flex

Source: Internet
Author: User
Tags instance method
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:
, Xmlsocket (host:string=null,port:int=0)--Creates a new Xmlsocket object.
, Close (): void--closes a xmlsocket.
, Connect (host:string,port:int): void--connected to the specified TCP port.
, Send (object:*): void--sends 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:
Copy Code code as 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
{
Listener = new TcpListener (8888);
}
catch (Exception ex)
{
Console.WriteLine (ex. message);
Return
}

Listener. Start ();
Console.WriteLine ("Server started, waiting for client to connect.");
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 server socket is ready to go, let's look at the client's ActionScript Xmlsocket how to establish a socket connection and 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 traffic. Create a Xmlsocket network connection based on ActionScript 3.0:
Copy Code code as follows:

Private Function Connectionserver (): void
{
Xmlconn = new Xmlsocket ();
Xmlconn.connect ("127.0.0.1", 8888);
}

You can then send a message to the socket server via Xmlsocket instance method Send (). The following code definition:
Copy Code code as follows:

Private Function OnSend (): void
{
Xmlconn.send (Txtdata.text + "\ n");
}

Client complete code:
Copy code code as follows:

<?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= "y=" "height=" width= "263" id= "Txtdata"/>
<mx:button x= "y=" 180 "label=" Connection "fontsize=" "click=" "Connectionserver ()"/>
<mx:button x= "190" y= "180" label= "Send" fontsize= "" click= "OnSend ()"/>
</mx:Application>

The following is a sample program test result diagram for this 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.