Flex vs. NET Interop (i): Network connectivity based on sockets

Source: Internet
Author: User
Tags socket firewall

Flash/flex also supports network connections based on sockets, and server-side can be developed in any language, such as C++,vb,c#,java. Listening on a network port allows you to receive connections from Flash/flex-developed clients.

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:

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

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

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

4, Send (object:*): void--to send 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:

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 {
Class Program
11 {
static void Main (string[] args)
13 {
TcpListener Listener;
15
Try
17 {
Listener = new TcpListener (8888);
19}
catch (Exception ex)
21 {
Console.WriteLine (ex. message);
return;
24}
25
Listener. Start ();
Console.WriteLine ("Server started, waiting for client to connect.");
BOOL loop = true;
29
while (Loop)
31 {
Socket s = listener. AcceptSocket ();
NetworkStream ns = new NetworkStream (s);
StreamReader reader = new StreamReader (NS);
string result = String. Empty;
A try
37 {
result = reader. ReadLine ();
Console.WriteLine (result);
40}
catch (Exception ex)
42 {
Console.WriteLine (ex. message);
44}
45}
46}
47}
48}

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.