Socket通信:服务端发送安全策略到flash(c#)
转载
在Adobe Flash Player升级到9.0.124后,由于安全策略的更改,使得在socket或xmlsocket的应用里,原先如用http方式加载安全策略的手段不能继续使用了,类似此类应用必须使用xmlsocket://方式来提供安全策略。flashplayer的安全策略检测过程如下:
1,首先检测目标服务器的843端口是否提供安全策略
2,如果1没有检测到策略,则检测actionscript是否使用了
Security.loadPolicyFile(xmlsocket://)
手段提供安全策略,如果还没检测到,则使用第3步检测
3,检测目标服务器目标端口是否提供安全策略
C#代码如下,编译环境VS2008 + .NET framework 3.5
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Collections;
namespace serverSocket
{
class Program
{
private Thread Beginthread, receivethread;
private Socket serverSocket, ClientSocket;
static void Main(string[] args)
{
Program program = new Program();
IPEndPoint remotePoint = new IPEndPoint(IPAddress.Any, 843);
program.serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
program.serverSocket.Bind(remotePoint);
program.serverSocket.Listen(10);
Console.WriteLine("Listening...");
program.Beginthread = new Thread(new ThreadStart(program.BeginListen));
program.Beginthread.Start();
}
private void BeginListen()
{
while (true)
{
ClientSocket = serverSocket.Accept();
receivethread = new Thread(new ThreadStart(receive));
receivethread.Start();
}
}
private void receive()
{
IPEndPoint clienTep = (IPEndPoint)ClientSocket.RemoteEndPoint;
int bytes = 0;
while (true)
{
Byte[] buffer = new Byte[24];
bytes = ClientSocket.Receive(buffer);
if (bytes > 0)
{
string clientCommand = Encoding.UTF8.GetString(buffer, 0, bytes);
String policy = "<cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"2999\" /></cross-domain-policy>\0";
buffer = System.Text.Encoding.UTF8.GetBytes(policy.ToCharArray());
ClientSocket.Send(buffer, buffer.Length, 0);
}
else
{
break;
}
}
ClientSocket.Close();
receivethread.Abort();
}
}
}
端口可以修改为843,或者是你指定连接的任意端口。