Silverlight實用竅門系列:67.Silverlight下的Socket通訊

科技小能手發表於2017-11-13

<p>  在Silverlight中進行通訊,只能使用4502-4534之間的埠進行資料傳輸,另外Silverlight客戶端會自動向943埠的伺服器端傳送一個&ldquo;&lt;policy-file-request/&gt;&rdquo;的語句請求,然後伺服器端943埠回發以下檔案以許可Socket通訊。</p>

<div class=”cnblogs_code”>

<pre>&lt;?xml version=<span style=”color: #800000;”>”</span><span style=”color: #800000;”>1.0</span><span style=”color: #800000;”>”</span> encoding=<span style=”color: #800000;”>”</span><span style=”color: #800000;”>utf-8</span><span style=”color: #800000;”>”</span> ?&gt;

&lt;access-policy&gt;

  &lt;cross-domain-access&gt;

    &lt;policy&gt;

      &lt;allow-<span style=”color: #0000ff;”>from</span>&gt;

        &lt;domain uri=<span style=”color: #800000;”>”</span><span style=”color: #800000;”>*</span><span style=”color: #800000;”>”</span>/&gt;

      &lt;/allow-<span style=”color: #0000ff;”>from</span>&gt;

      &lt;grant-to&gt;

        &lt;socket-resource port=<span style=”color: #800000;”>”</span><span style=”color: #800000;”>4502-4534</span><span style=”color: #800000;”>”</span> protocol=<span style=”color: #800000;”>”</span><span style=”color: #800000;”>tcp</span><span style=”color: #800000;”>”</span>/&gt;

      &lt;/grant-to&gt;

    &lt;/policy&gt;

  &lt;/cross-domain-access&gt;

&lt;/access-policy&gt;</pre>

</div>

<p>  A.現在我們首先來看伺服器端的程式碼,主要分為策略響應步驟和服務響應步驟。</p>

<p>  <strong>策略步驟一</strong>:啟動監聽943埠是否有需要安全策略檔案請求</p>

<p>  <strong>策略步驟二</strong>:如果客戶端請求是&lt;policy-file-request/&gt;,則將安全策略檔案作為bytes傳送給客戶端</p>

<p>  <strong>服務步驟一</strong>:啟動伺服器端,監聽4525埠,是否有Socket對話請求</p>

<p>  <strong>服務步驟二</strong>:如果有客戶端請求的連線,則傳送訊息告知客戶端</p>

<p>  程式碼如下:</p>

<div class=”cnblogs_code”>

<pre>    <span style=”color: #0000ff;”>class</span><span style=”color: #000000;”> Program

    {

        </span><span style=”color: #0000ff;”>static</span> <span style=”color: #0000ff;”>void</span> Main(<span style=”color: #0000ff;”>string</span><span style=”color: #000000;”>[] args)

        {

            </span><span style=”color: #008000;”>//</span><span style=”color: #008000;”>策略步驟一:啟動監聽943埠是否有需要安全策略檔案請求</span>

            Thread access = <span style=”color: #0000ff;”>new</span> Thread(<span style=”color: #0000ff;”>new</span><span style=”color: #000000;”> ThreadStart(accessThread));

            access.Start();

            </span><span style=”color: #008000;”>//</span><span style=”color: #008000;”>服務步驟一:啟動伺服器端,監聽4525埠,是否有Socket對話請求</span>

            Thread server = <span style=”color: #0000ff;”>new</span> Thread(<span style=”color: #0000ff;”>new</span><span style=”color: #000000;”> ThreadStart(ServerThread));

            server.Start();

        }

        </span><span style=”color: #008000;”>//</span><span style=”color: #008000;”>策略請求監聽</span>

        <span style=”color: #0000ff;”>static</span> <span style=”color: #0000ff;”>void</span><span style=”color: #000000;”> accessThread()

        {

            </span><span style=”color: #008000;”>//</span><span style=”color: #008000;”>獲取943埠監聽的Socket服務端</span>

            Socket socket = GetSocketServer(<span style=”color: #800080;”>943</span><span style=”color: #000000;”>);

            </span><span style=”color: #0000ff;”>while</span> (<span style=”color: #0000ff;”>true</span><span style=”color: #000000;”>)

            {

                Socket new_access </span>=<span style=”color: #000000;”> socket.Accept();

                </span><span style=”color: #0000ff;”>string</span> clientPolicyString = <span style=”color: #800000;”>”</span><span style=”color: #800000;”>&lt;policy-file-request/&gt;</span><span style=”color: #800000;”>”</span><span style=”color: #000000;”>;

                </span><span style=”color: #0000ff;”>byte</span>[] requestbytes = <span style=”color: #0000ff;”>new</span> <span style=”color: #0000ff;”>byte</span><span style=”color: #000000;”>[clientPolicyString.Length];

                new_access.Receive(requestbytes);

                </span><span style=”color: #0000ff;”>string</span> requeststring = System.Text.Encoding.UTF8.GetString(requestbytes, <span style=”color: #800080;”>0</span><span style=”color: #000000;”>, requestbytes.Length);

                </span><span style=”color: #0000ff;”>if</span> (requeststring ==<span style=”color: #000000;”> clientPolicyString)

                {

                    </span><span style=”color: #008000;”>//</span><span style=”color: #008000;”>策略步驟二:如果客戶端請求是&lt;policy-file-request/&gt;,則將安全策略檔案作為bytes傳送給客戶端</span>

                    <span style=”color: #0000ff;”>byte</span>[] accessbytes =<span style=”color: #000000;”> GetPolicyToClient();

                    new_access.Send(accessbytes, accessbytes.Length, SocketFlags.None);

                    new_access.Close();

                }

                Thread.Sleep(</span><span style=”color: #800080;”>100</span><span style=”color: #000000;”>);

            }

        }

        </span><span style=”color: #0000ff;”>static</span> <span style=”color: #0000ff;”>void</span><span style=”color: #000000;”> ServerThread()

        {

            </span><span style=”color: #008000;”>//</span><span style=”color: #008000;”>獲取4525埠監聽的Socket服務端</span>

            Socket socket = GetSocketServer(<span style=”color: #800080;”>4525</span><span style=”color: #000000;”>);

            </span><span style=”color: #0000ff;”>while</span> (<span style=”color: #0000ff;”>true</span><span style=”color: #000000;”>)

            {

                Socket _socket </span>=<span style=”color: #000000;”> socket.Accept();

                </span><span style=”color: #008000;”>//</span><span style=”color: #008000;”>服務步驟二:如果有客戶端請求的連線,則傳送訊息告知客戶端</span>

                <span style=”color: #0000ff;”>byte</span>[] b2 = <span style=”color: #0000ff;”>new</span> <span style=”color: #0000ff;”>byte</span>[<span style=”color: #800080;”>1024</span><span style=”color: #000000;”>];

                _socket.Receive(b2);

                Console.WriteLine(Encoding.UTF8.GetString(b2).Replace(</span><span style=”color: #800000;”>”</span><span style=”color: #800000;”>

相關文章