我正在實現一個模擬的B/S股票數據系統。我爲客戶端和服務器端使用flex和c#。我發現閃存有一個安全策略,我在我的服務器代碼中處理策略文件請求。但似乎它不起作用,因爲代碼在連接後跳出「socket.Receive(b)」。我試過在連接處理程序中的客戶端上發送消息,在這種情況下,服務器可以收到正確的消息。但是自動生成的「策略文件請求」永遠不會被接收,並且客戶端不能從服務器獲得數據發送。在這裏,我把我的代碼片段。C#和Flex之間的套接字通信問題
我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;
}
}
}
很抱歉的長碼,請別人幫忙吧,萬分感謝
我使用wireshark來封裝包,發現沒有 已經發送到服務器。我在connectHandler中調用send(「begin」)方法。現在服務器響應它併發回數據,但dataHandler仍然沒有被觸發,儘管我可以在wireshark中看到響應數據。它是有線的:[ –
2010-05-18 09:33:04