我正在实施一个模拟的b / s股票数据系统。我在客户端和服务器端使用flex和c#。我发现flash有一个安全策略,我在服务器代码中处理了policy-file-request。但似乎它不起作用,因为代码在连接后跳出“socket.Receive(b)”。我尝试在连接处理程序中的客户端上发送消息,在这种情况下,服务器可以接收正确的消息。但是永远不会收到自动生成的“策略文件请求”,客户端无法从服务器发送数据。在这里,我把我的代码片段。
我的ActionScript代码:
public class StockClient extends Sprite {
private var hostName:String = "192.168.84.103";
private var port:uint = 55555;
private var socket:XMLSocket;
public function StockClient() {
socket = new XMLSocket();
configureListeners(socket);
socket.connect(hostName, port);
}
public function send(data:Object) : void{
socket.send(data);
}
private function configureListeners(dispatcher:IEventDispatcher):void {
dispatcher.addEventListener(Event.CLOSE, closeHandler);
dispatcher.addEventListener(Event.CONNECT, connectHandler);
dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
dispatcher.addEventListener(ProgressEvent.SOCKET_DATA, dataHandler);
}
private function closeHandler(event:Event):void {
trace("closeHandler: " + event);
}
private function connectHandler(event:Event):void {
trace("connectHandler: " + event);
//following testing message can be received, but client can't invoke data handler
//send("<policy-file-request/>");
}
private function dataHandler(event:ProgressEvent):void {
//never fired
trace("dataHandler: " + event);
}
private function ioErrorHandler(event:IOErrorEvent):void {
trace("ioErrorHandler: " + event);
}
private function progressHandler(event:ProgressEvent):void {
trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
}
private function securityErrorHandler(event:SecurityErrorEvent):void {
trace("securityErrorHandler: " + event);
}
}
我的C#代码:
const int PORT_NUMBER = 55555;
const String BEGIN_REQUEST = "begin";
const String END_REQUEST = "end";
const String POLICY_REQUEST = "<policy-file-request/>\u0000";
const String POLICY_FILE = "<?xml version=\"1.0\"?>\n" +
"<!DOCTYPE cross-domain-policy SYSTEM \"http://www.adobe.com/xml/dtds/cross-domain-policy.dtd\">\n" +
"<cross-domain-policy> \n" +
" <allow-access-from domain=\"*\" to-ports=\"55555\"/> \n" +
"</cross-domain-policy>\u0000";
................
private void startListening()
{
provider = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
provider.Bind(new IPEndPoint(IPAddress.Parse("192.168.84.103"), PORT_NUMBER));
provider.Listen(10);
isListened = true;
while (isListened)
{
Socket socket = provider.Accept();
Console.WriteLine("connect!");
byte[] b = new byte[1024];
int receiveLength = 0;
try
{
// code jump out at this statement
receiveLength = socket.Receive(b);
}
catch (Exception e)
{
Debug.WriteLine(e.ToString());
}
String request = System.Text.Encoding.UTF8.GetString(b, 0, receiveLength);
Console.WriteLine("request:"+request);
if (request == POLICY_REQUEST)
{
socket.Send(Encoding.UTF8.GetBytes(POLICY_FILE));
Console.WriteLine("response:" + POLICY_FILE);
}
else if (request == END_REQUEST)
{
Dispose(socket);
}
else
{
StartSocket(socket); break;
}
}
}
对于长代码很抱歉,请有人帮忙,谢谢百万
答案 0 :(得分:1)
这是因为在您尝试加入的端口上未请求套接字策略文件,而是在静态端口843
上请求。
您应该收听端口843以提供策略请求。另外,在发送策略文件后立即关闭套接字时遇到了一些问题。在发送策略文件后,套接字似乎应该保持打开状态几秒钟,否则Flash可能会放弃答案。
请注意,通过这种方式,您可以从其他应用程序提供策略文件请求,而不必从主服务器提供。
this documentation中描述了整个内容。