Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you try to establish a TCP connection in port range 4502-4534, Silverlight will first post a request on port 943 to retrieve the client access policy file content - It won't read the file at <a href="http://localhost.myserivce.com/clientaccesspolicy.xml" rel="nofollow noreferrer">http://localhost.myserivce.com/clientaccesspolicy.xml</a>, because this is only for HTTP requests. You need to configure your server to listen on TCP port 943, expect a request string equal to <code>&lt;policy-file-request/&gt;</code> and reply with the xml file content.</p> <p>The code below shows a basic implementation, you need to pass it a local IPEndPoint using port 943:</p> <pre><code>public class SocketPolicyServer { private const string m_policyRequestString = "&lt;policy-file-request/&gt;"; private string m_policyResponseString; private TcpListener m_listener; bool _started = false; public SocketPolicyServer() { m_policyResponseString = File.ReadAllText("path/to/clientaccesspolicy.xml"); } public void Start(IPEndPoint endpoint) { m_listener = new TcpListener(endpoint); m_listener.Start(); _started = true; m_listener.BeginAcceptTcpClient(HandleClient, null); } public event EventHandler ClientConnected; public event EventHandler ClientDisconnected; private void HandleClient(IAsyncResult res) { if(_started) { try { TcpClient client = m_listener.EndAcceptTcpClient(res); m_listener.BeginAcceptTcpClient(HandleClient, null); this.ProcessClient(client); } catch(Exception ex) { Trace.TraceError("SocketPolicyServer : {0}", ex.Message); } } } public void Stop() { _started = false; m_listener.Stop(); } public void ProcessClient(TcpClient client) { try { if(this.ClientConnected != null) this.ClientConnected(this, EventArgs.Empty); StreamReader reader = new StreamReader(client.GetStream(), Encoding.UTF8); char[] buffer = new char[m_policyRequestString.Length]; int read = reader.Read(buffer, 0, buffer.Length); if(read == buffer.Length) { string request = new string(buffer); if(StringComparer.InvariantCultureIgnoreCase.Compare(request, m_policyRequestString) == 0) { StreamWriter writer = new StreamWriter(client.GetStream()); writer.Write(m_policyResponseString); writer.Flush(); } } } catch(Exception ex) { Trace.TraceError("SocketPolicyServer : {0}", ex.Message); } finally { client.GetStream().Close(); client.Close(); if(this.ClientDisconnected != null) this.ClientDisconnected(this, EventArgs.Empty); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload