개요
Silverlight 2 Beta 2에서 Socket을 사용할 때 Cross-Domain에 있는 소켓 서버에
접속을 하려면 서버 측의 정책을 확인하는 과정이 필요하게 된 것 아시죠?
다시말해, 실버라이트 런타임이 943번 포트를 통해 실버라이트 정책 파일을 요청하면,
서버 쪽에서는 정책파일을 보내줘야 하는데요.
며칠 전 공도씨가 [MSDN] 네트워크 보안 접근 제약란 제목의 장문의 번역글을 포스팅했습니다.
그 내용의 하단에 보시면 그런 역할을 하는 서버를 만드는 방법이 소개되어 있습니다.
[소켓을 위한 정책 파일 예제]와 [소켓을 위한 정책 서버의 샘플 코드]란 내용이 있구요.
Mike Snows의 Tip of the Day #12 - Full Implementation of a Silverlight Policy Server.를
참고하면 정책 파일 서버를 만들 수 있다고 설명해 놓았습니다.
다운로드
아래는 그것을 그대로 따라서 만들어 놓은 프로젝트구요.
제가 조금 코드를 바꿔서 정책 파일 요청시 Socket Client의 IP를 화면에서 알 수 있도록
하였습니다.
PolicyConnection.cs
public PolicyConnection(Socket client, byte[] policy)
{
_connection = client;
_policy = policy;
_buffer = new byte[_policyRequestString.Length];
_received = 0;
try
{
// receive the request from the client
//_connection.BeginReceive(_buffer, 0, _policyRequestString.Length, SocketFlags.None,
// new AsyncCallback(OnReceive), null);
_connection.BeginReceive(_buffer, 0, _policyRequestString.Length, SocketFlags.None,
new AsyncCallback(OnReceive), client.RemoteEndPoint.ToString());
}
catch (SocketException)
{
_connection.Close();
}
}
...
private void OnReceive(IAsyncResult res)
{
try
{
_received += _connection.EndReceive(res);
// if we haven't gotten enough for a full request yet, receive again
if (_received < _policyRequestString.Length)
{
_connection.BeginReceive(_buffer, _received, _policyRequestString.Length - _received,
SocketFlags.None, new AsyncCallback(OnReceive), null);
return;
}
// make sure the request is valid
string request = System.Text.Encoding.UTF8.GetString(_buffer, 0, _received);
if (StringComparer.InvariantCultureIgnoreCase.Compare(request, _policyRequestString) != 0)
{
_connection.Close();
return;
}
// send the policy
Console.Write(string.Format("Sending policy to {0}\n", res.AsyncState));
_connection.BeginSend(_policy, 0, _policy.Length, SocketFlags.None,
new AsyncCallback(OnSend), null);
}
catch (SocketException)
{
_connection.Close();
}
}
미리보기
주의사항
컴파일 전에 Program.cs의 한부분을 수정하셔야 합니다.
색깔 있는 부분을 실제 clientaccesspolicy.xml 파일이 위치한 곳의 경로로 변경하셔야 합니다.
Program.cs
static void Main(string[] args)
{
Console.Write("Starting...\n");
PolicyServer ps =
new PolicyServer(@"D:\clientaccesspolicy.xml");
System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
}
감사합니다.
보너스
Beta1 시절 포스팅 되었던 스캇구 형님의 소켓 예제를 오늘 다시 한 번 찾아보니!
Beta2 버전으로 포팅이 되었습니다. 여기에도 Policy Server를 포함하고 있습니다.
한 번 훑어 보시면 도움이 되실 거라고 생각합니다.
Pushing Data to a Silverlight Client with Sockets: Part I
Pushing Data to a Silverlight Client with Sockets: Part II
Part I에서는 Socket Server를 Part II에서는 Socket Client를 설명하고 있습니다.

PolicyServer943.zip
이올린에 북마크하기
이올린에 추천하기
Prev

Rss Feed