Problem with socket communication between C# and Flex
Posted
by Chris Lee
on Stack Overflow
See other posts from Stack Overflow
or by Chris Lee
Published on 2010-05-18T06:49:00Z
Indexed on
2010/05/18
9:30 UTC
Read the original article
Hit count: 442
Hi all, I am implementing a simulated b/s stock data system. I am using flex and c# for client and server sides. I found flash has a security policy and I handled the policy-file-request in my server code. But seems it doesn't work, because the code jumped out at "socket.Receive(b)" after connection. I've tried sending message on client in the connection handler, in that case the server can receive correct message. But the auto-generated "policy-file-request" can never be received, and the client can get no data sending from server. Here I put my code snippet.
my ActionScript code:
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);
}
}
my C# code:
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;
}
}
}
Sorry for the long code, please someone help with it, thanks a million
© Stack Overflow or respective owner