Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle http fired by Flex in C# server

Hi I am trying to send a simple HTTP message from Flex to C# server, but it seems that I am getting tow calls, first is the real one and the second is an empty one.

Why is that and how can I handle it?

This is my C# code:

TcpListener listener = new TcpListener(IPAddress.Any, 9400);
listener.Start();
Console.WriteLine("Server started");
Socket client;
while (true)
{
    client = listener.AcceptSocket();
    // client.Available is an expensive call so it's just for testing
    Console.WriteLine("Client accepted " + client.Connected + " " + client.Available);
    SocketHandler handler = new SocketHandler();
    ThreadPool.QueueUserWorkItem(handler.handleSocket, client);
} 

this is the SocketHandler:

public void handleSocket(object socketObjeck)
{
    try
    {
        socket = (Socket)socketObjeck;
        byte[] buffer = new byte[1024];
        SocketSettings.setSocket(socket);
        //blocker...
        try
        {
            socket.Receive(buffer);
        }
        catch (Exception e)
        {
            Console.WriteLine("Error\nFaild reading from socket\n" + e.Message);
            socket.Close();
            return;
        }
        parseData(buffer);

        socket.Close(3);
    }
    catch (Exception e)
    {
        Console.WriteLine("Error\nError \n" + e.Message + "\n" + e.StackTrace);
    }
}

And this is my flex code:

var request:URLRequest = new URLRequest();
request.data = "Hello from flex";
request.url = URL;
request.method = URLRequestMethod.POST;
loader.load(request);

I am always getting 2 calls. The line:

Console.WriteLine("Client accepted " + client.Connected + " " + client.Available);

called twice. What am I missing?

Edit 1: I can tell you for sure that the second call is empty, it's not even seen in chrome JavaScript console, it's like flex opening a connection, and waiting for some response or I don't know what... but it sending no data.

Edit 2:

I been trying to send a true HTTP response a notice another thing, the second call is coming without waiting for the first call, if I am putting the response thread to short sleep(100 milliseconds in my test) then I am getting the second call before I been able to response for the first one.

P.S Using Flex 4.6, Visual Studio 2010

like image 954
Ilya Gazman Avatar asked Jan 22 '12 21:01

Ilya Gazman


2 Answers

As @Spender mentioned, it' difficult to know what the reason for the calls is, without knowing what the calls are.

However, given that you're using a URLRequest to communicate, it's possible that the first call is a request for the crossdomain.xml file, which must be present in order for the flash player to communicate with your server.

Other protocols have different initialisation calls (eg., RemoteObject or NetConnection will send an intial call to setup the local FlexClientId, and a SocketServer will call for a crossdomain.xml on a specific port.)

The moral of the story : You need to check what the content is of the request before you can respond.

like image 112
Marty Pitt Avatar answered Oct 14 '22 23:10

Marty Pitt


I see two points:

  • crossdomain.xml has to be present server-side for this to work from the client-side

  • use HttpListener on the server-side instead TcpListener, it will make your code much more robust since it has all the http protocol details implemented and allows you to concentrate on just the "real work"

Please ask if you need further details/samples etc.

like image 26
Yahia Avatar answered Oct 14 '22 22:10

Yahia