Handle HTTP running Flex on a C # server

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

Why is this and how can I handle this?

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 just for testing Console.WriteLine("Client accepted " + client.Connected + " " + client.Available); SocketHandler handler = new SocketHandler(); ThreadPool.QueueUserWorkItem(handler.handleSocket, client); } 

this is 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 flexible code:

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

I always get 2 calls. Line:

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

called twice. What am I missing?

Edit 1: I can tell you that the second call is empty, it doesn’t even appear in the Chrome Chrome console, it is like flex, it opens the connection and is waiting for an answer, or I don’t know what ... but it does not send any data.

Edit 2:

I am trying to send a true HTTP response message to another, the second call goes without waiting for the first call, if I put the response flow in a short sleep (100 milliseconds in my test), then I receive the second call before I can answer the first.

PS Using Flex 4.6, Visual Studio 2010

+6
source share
3 answers

In the end, he turned around that it was a crossdomain request. If you do not add the crossdomain file to your www folder, flex will send you an empty message (if you try to reply to this message with crossdomain, it wants to work).

0
source

As @Spender pointed out, it’s hard to understand what the reason for calls is, not knowing what calls are.

However, given that you are using URLRequest for communication, it is possible that the first call is a request for the crossdomain.xml file that must be present so that the flash player can communicate with your server.

Other protocols have different initialization calls (for example, RemoteObject or NetConnection will send an internal call to configure the local FlexClientId, and SocketServer will call crossdomain.xml on a specific port.)

The moral of the story: you need to check what the contents of the request are before you can respond.

+3
source

I see two points:

  • crossdomain.xml must be present server-side for this to work on the client side

  • use HttpListener -side TcpListener instead of TcpListener , this will make your code more reliable, since it has all the details of the http protocol implemented and allows you to focus only on "real work"

Ask if you need additional data / samples, etc.

+1
source

Source: https://habr.com/ru/post/906606/


All Articles