C # Use fluorinefx to implement amf3 protocol and Flash Communication

Source: Internet
Author: User

In general, the use of flash to call. NET components requires remote calls. In fact, we can also directly communicate through socket.

The specific format is flash amf3 or afm0, and popularFluorinefxThe specific code of the server component is as follows:

Based on this usage, you can develop a. Net socket server for web games or other flash applications.

Using system;
Using system. Collections. Generic;
Using system. Collections. Specialized;
Using system. text;
Using system. Threading;
Using system. net. Sockets;
Using system. net;
Using system. runtime. serialization;
Using system. runtime. serialization. formatters. Binary;
Using system. IO;
Using fluorinefx;

Namespace stcserver
{
Class Monitor
{
Private int Port = 8888;
Private const int maxpksize = 4096;
Private tcplistener tcpls = NULL;
Private list <client> clients = new list <client> ();

Public void close ()
{
If (tcpls! = NULL)
{
Tcpls. Stop ();
}
If (clients. Count! = 0)
{
Foreach (client C in clients)
{
C. Sock. Shutdown (socketshutdown. Both );
C. thread. Abort ();
}
Clients. Clear ();
Clients = NULL;
}
}

Private void threadfunc (Object client)
{
Client Ct = client as client;
Socket Sk = CT. Sock;
Byte [] cmdbuff = new byte [maxpksize];

While (true)
{
Try
{
Cmdbuff. initialize ();
Int n = Sk. Receive (cmdbuff );
Console. writeline (datetime. Now. tostring () + ": Data received successfully. Length:" + n );

System. Io. memorystream MS = new system. Io. memorystream (cmdbuff, 0, N );
Fluorinefx. amf3.bytearray BA = new fluorinefx. amf3.bytearray (MS );
Node o = (node) Ba. readobject ();
Console. writeline (O. ID );
Console. writeline (O. Name );
Console. writeline (O. Address );
O. Name = "James ";
Node node = new node ();
Node. ID = O. ID;
Node. Name = O. Name;
Node. Address = O. address;

Fluorinefx. amf3.bytearray result = new fluorinefx. amf3.bytearray ();
Result. writeobject (node );
Byte [] Buf = new byte [result. Length];
Result. Position = 0;
Result. readbytes (BUF, (uint) 0, (uint) BUF. Length );
Foreach (client CL in clients)
{
Cl. Sock. Send (BUF, Buf. length, socketflags. None );
}
}
Catch (exception E)
{
Console. writeline ("Network Termination:" + E. Message );
Ct. Sock. Shutdown (socketshutdown. Both );
Clients. Remove (CT );
Thread. currentthread. Abort ();
Ct = NULL;
}
}
}
Public void startup ()
{
IPaddress IP = DNS. gethostaddresses (DNS. gethostname () [0];
Tcpls = new tcplistener (IP, Port );
Tcpls. Start ();
While (true)
{
Byte [] packetbuff = new byte [maxpksize];
Console. writeline ("the server is started and listening.../N ");
Console. writeline (string. Format ("Server IP: {0}/T port: {1}/N", IP, Port ));

Socket Sk = tcpls. acceptsocket ();
Console. writeline ("connected, receiving data.../N ");
Thread CTH = new thread (New parameterizedthreadstart (threadfunc ));
Client Cl = new client (SK, Cth );
Clients. Add (CL );
CTH. Start (CL );
}
}
}
}

Namespace stcserver
{
Class Client
{
Public socket sock = NULL;
Public thread = NULL;
Public client (socket S, thread th)
{
Sock = s;
Thread = th;
}

}
}

Namespace stcserver
{
Public class Node
{
Public uint ID;
Public string name;
Public String address;
}
}

Flash (Flex) client:

Package
{
Import flash. Events .*;
Import flash.net. objectencoding;
Import flash.net. Socket;
Import flash.net. registerclassalias;
Import flash. utils. bytearray;
Public class greeter
{
Private var socket: socket;
Public Function sayhello (): void

{
Registerclassalias ("stcserver. node", node );
Socket = new socket ();
Socket. addeventlistener (event. Connect, onhandler );
Socket. addeventlistener (progressevent. socket_data, onhandler );

Socket. objectencoding = objectencoding. amf3;
Socket. Connect ("192.168.1.14", 8888 );
VaR node: node = new node ();
Node. ID = 122;
Node. Name = "User Name ";
Node. Address = "kkkkkkk ";
VaR num: number;
Trace (Num );
VaR arr: bytearray = new bytearray;
Arr. writeobject (node );
Arr. Compress ();
Socket. writebytes (ARR );
Socket. Flush ();
}

Public Function onhandler (Event: Event): void
{
Switch (event. Type ){
Case progressevent. socket_data:
Trace (new date ());
Trace ("data accepted ");
VaR Bytes: bytearray = new bytearray ();
Socket. readbytes (bytes );
Bytes. uncompress ();
VaR node: node;
Node = node (bytes. readobject ());
Trace (node. Name );
Trace (node. ID );
Trace (node. Address );
Break;
Case event. close:
Trace ("Connection closed ");
Break;
Case event. Connect:
Trace ("Connect connector ..");
Break;
Case ioerrorevent. io_error:
Case securityerrorevent. security_error:
Trace ("connection failed ");
Break;
}
}
}
}

Package
{
Public class Node
{
Public var ID: uint;
Public var name: string;
Public var address: string;
Public Function node ()
{
}
}
}

<? XML version = "1.0" encoding = "UTF-8"?>
<Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" xmlns = "*"
Layout = "vertical"
Creationcomplete = "initapp ()">

<Mx: SCRIPT>
<! [CDATA [
Private var mygreeter: greeter = new greeter ();

Public Function initapp (): void
{
// Says hello at the start, and asks for the user's name
Maintxt. Text = mygreeter. sayhello ();
}
]>
</MX: SCRIPT>

<Mx: textarea id = "maintxt" width = "400"/>

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