gimite / web-socket-js

HTML5 Web Socket implementation powered by Flash
BSD 3-Clause "New" or "Revised" License
2.74k stars 490 forks source link

Can not connect to flash policy server in some cases #156

Closed DavidCzq closed 9 years ago

DavidCzq commented 9 years ago

I wrote 2 socket server with c#, one for flash policy server(port at 843), one for websocket(port at 8013) It works well in Chrome, IE8 at my office, home.

But, one day, I remote connect to server in another IDC, I found swf file (IE8) can not connect to policy server. Logs here: (SecurityError: Error #2048) make sure the server is running and Flash socket policy file is correctly placed. To test flash policy server, I wrote a console program and run at server, flash policy server is ok. 843 port return policy xml. So, Why in the same computer, My console program can connect the policy server, but not swf ?

gimite commented 9 years ago

Can you point me the socket policy server code? I remember that I need to be careful about a few points implementing socket policy server, and it doesn't work reliably otherwise.

DavidCzq commented 9 years ago

Sorry, I'm late.

Flash policy server code:

using System; using System.IO; using System.Net; using System.Net.Sockets; using System.Text; using System.Xml.Linq;

namespace PolicyService { public class FlashPolicyServer { public static void Main() { string Ip = "xxx.xxx.xxx.xxx"; int Port = 843; Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint localEP = new IPEndPoint(IPAddress.Parse(Ip), Port); socket.Bind(localEP); socket.Listen(10); while (true) { Socket hostSocket = socket.Accept(); FlashPolicyServer.WorkMethod(hostSocket); hostSocket.Shutdown(SocketShutdown.Both); hostSocket.Close(); } } private static void WorkMethod(Socket hostSocket) { byte[] array = new byte[23]; int len = 0; while (len < array.Length) { len += hostSocket.Receive(array, len, array.Length - len, SocketFlags.None); } string key = Encoding.UTF8.GetString(array); if (key.StartsWith("<policy-file-request/>")) { byte[] bytes = Encoding.UTF8.GetBytes("<?xml version=\"1.0\"?><cross-domain-policy><site-control permitted-cross-domain-policies=\"all\"/><allow-access-from domain=\"\" to-ports=\"\"/><allow-http-request-headers-from domain=\"\" headers=\"\"/></cross-domain-policy>"); MemoryStream Ms = new MemoryStream(); Ms.Write(bytes, 0, bytes.Length); Ms.WriteByte(array[array.Length - 1]); Ms.WriteByte(13); //Very important byte[] ReturnData = Ms.ToArray(); Ms.Close(); len = 0; while (len < ReturnData.Length) { len += hostSocket.Send(ReturnData, len, ReturnData.Length - len, SocketFlags.None); } }
} }

}

After google, I found that appending \n to the end of the returning policy xml, Websocket connect success!

Ms.WriteByte(13);

Above code helps!