Flex and. NET Interop (i): socket-based network connectivity

Source: Internet
Author: User
Flash/flex also supports socket-based network connections, and the server can be developed in any language such as C++,vb,c#,java. Listening to a network port can receive connections from clients that are Flash/flex developed.

ActionScript 3.0 provides a way to communicate with the server side through a socket connection. This is an important feature beyond the traditional B/s structure. This makes the network communication can be connected instantly, avoiding the disadvantage of the HTTP protocol stateless connection. ActionScript 3.0 uses the Xmlsocket class to connect. It is important to note that when you use the Xmlsocket class for a socket connection, you cannot automatically pass through the firewall. To go through a firewall connection, you need to use the HTTP protocol-based RTMP protocol.

By reviewing the API documentation provided by Adobe, Xmlsocket provides four public methods:

1, XMLSocket (host:string=null,port:int=0)--Create a new XMLSocket object.

2, Close (): void--close a xmlsocket.

3. Connect (host:string,port:int): void--connect to the specified TCP port.

4. Send (object:*): void--sends data to the connection server.

OK, so we can use xmlsocket to develop a socket-based network for instant communication applications. The following provides a socket service side via C # and listens on port 8888. The sample program is as follows:

<!--<br/><br/>code highlighting produced by Actipro Codehighlighter (freeware) <br/>http://www. Codehighlighter.com/<br/><br/>-->1usingsystem;
2usingsystem.collections.generic;
3usingsystem.linq;
4usingsystem.text;
5usingsystem.net.sockets;
6usingsystem.io;
7
8namespaceFlashFlexDotNet
9{
10classProgram
11{
12staticvoidMain (String[]args)
13{
14TcpListenerlistener;
15
16try
17{
18listener=newtcplistener (8888);
19}
20catch (Exceptionex)
21{
22console.writeline (ex. Message);
23return;
24}
25
26listener. Start ();
27console.writeline ("Server starts, waits for client connection.");
28boolloop=true;
29
30while (Loop)
31{
32sockets=listener. AcceptSocket ();
33networkstreamns=newnetworkstream (s);
34streamreaderreader=newstreamreader (NS);
35stringresult=string. Empty;
36try
37{
38result=reader. ReadLine ();
39console.writeline (result);
40}
41catch (Exceptionex)
42{
43console.writeline (ex. Message);
44}
45}
46}
47}
48}
49

The server-side socket is ready, and below is a look at how the client's ActionScript Xmlsocket to establish a socket connection to communicate with the. NET Socket service.

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. Create a base with the Xmlsocket network connection provided by ActionScript 3.0 as follows:

<!--<br/><br/>code highlighting produced by Actipro Codehighlighter (freeware) <br/>http://www. Codehighlighter.com/<br/><br/>-->1privatefunctionconnectionserver (): void
2{
3xmlconn=newxmlsocket ();
4xmlconn.connect ("127.0.0.1", 8888);
5}

You can then send a message to the socket server via the Xmlsocket instance method send (). The following code defines:

<!--<br/><br/>code highlighting produced by Actipro Codehighlighter (freeware) <br/>http://www. Codehighlighter.com/<br/><br/>-->1privatefunctiononsend (): void
2{
3xmlconn.send (txtdata.text+ "\ n");
4}

Client complete code:

<!--<br/><br/>code highlighting produced by Actipro Codehighlighter (freeware) <br/>http://www. codehighlighter.com/<br/><br/>-->1<?xmlversion= "1.0" encoding= "Utf-8"?>
2<mx:applicationxmlns:mx= "Http://www.adobe.com/2006/mxml" layout= "Absolute"
3backgroundgradientalphas= "[1.0,1.0]"
4backgroundgradientcolors= "[#CDCAE6, #FFFFFF]" >
5<mx:script>
6<! [cdata[
7importmx.controls.alert;
8
9privatevarxmlconn:xmlsocket;
10
11privatefunctionconnectionServer (): void
12{
13xmlconn=newxmlsocket ();
14xmlconn.connect ("127.0.0.1", 8888);
15}
16
17privatefunctiononSend (): void
18{
19xmlconn.send (txtdata.text+ "\ n");
20}
21]]>
22</mx:script>
23<mx:textareax= "y=" "height=" width= "263" id= "Txtdata"/>
24<mx:buttonx= "y=" "label=" Connection "fontsize=" "click=" Connectionserver () "/>"
25<mx:buttonx= "y=" "label=" Send "fontsize=" "click=" OnSend () "/>"
26</mx:application>

  • 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.