silverlight套接字:silverlight应用程序中未处理的错误试图以其访问权限所禁止的方式访问套接字
我基本上尝试从这里复制套接字示例: 我只是在客户端做了一个小的改变,即silverlight套接字:silverlight应用程序中未处理的错误试图以其访问权限所禁止的方式访问套接字,silverlight,sockets,tcp,Silverlight,Sockets,Tcp,我基本上尝试从这里复制套接字示例: 我只是在客户端做了一个小的改变,即 String safeHost = "127.0.0.1"; int port = 4509; 然后我得到了这个权限错误?知道为什么吗 Silverlight应用程序中未经处理的错误试图以访问权限禁止的方式访问套接字。我认为套接字安全检查的工作方式需要使用应用程序使用的相同url字符串。为确保使用正确的字符串,我始终使用此字符串构造DNSEndPoint: int Port = 4509; Dns
String safeHost = "127.0.0.1";
int port = 4509;
然后我得到了这个权限错误?知道为什么吗
Silverlight应用程序中未经处理的错误试图以访问权限禁止的方式访问套接字。我认为套接字安全检查的工作方式需要使用应用程序使用的相同url字符串。为确保使用正确的字符串,我始终使用此字符串构造DNSEndPoint:
int Port = 4509;
DnsEndPoint ep = new DnsEndPoint(Application.Current.Host.Source.DnsSafeHost, Port, AddressFamily.InterNetwork);
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sock.NoDelay = true;
SocketAsyncEventArgs ea = new SocketAsyncEventArgs{RemoteEndPoint = ep};
//set up completed event handler et al.
sock.ConnectAsync(ea);
我在一个类似的聊天应用程序中使用了这段代码。通过使用Application.Current.Host.Source.DnsSafeHost
属性,您可以确保使用相同的Dns名称访问服务器,并使用浏览器用于HttpRequests的套接字
另外,如果您在端口943上提供访问策略文件,这是Silverlight中套接字支持的另一个要求
编辑
要确认您正在为策略文件提供服务,您可以执行许多操作
public abstract class Server
{
protected Socket Listener { get; set; }
protected int Port { get; private set; }
protected int Backlog { get; private set; }
protected bool isStopped { get; set; }
protected SocketAsyncEventArgs AcceptArgs {get;set;}
public Server(int port)
{
AcceptArgs = new SocketAsyncEventArgs();
AcceptArgs.Completed += new EventHandler<SocketAsyncEventArgs>(Accept_Completed);
isStopped = true;
Port = port;
Backlog = 100;
}
public Server(int port, int backlog)
{
isStopped = true;
Port = port;
Backlog = backlog;
}
public void Start()
{
isStopped = false;
Listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ep = new IPEndPoint(IPAddress.Any, Port);
Listener.ExclusiveAddressUse = true;
Listener.Bind(ep);
//Console.WriteLine("Listening on " + Port);
Listener.Listen(Backlog);
Listener.AcceptAsync(AcceptArgs);
}
void Accept_Completed(object sender, SocketAsyncEventArgs e)
{
if (isStopped) return;
Socket client = e.AcceptSocket;
//Console.WriteLine("Accepted Connection From: " + client.RemoteEndPoint.ToString());
e.AcceptSocket = null;
Listener.AcceptAsync(AcceptArgs);
HandleClient(client);
}
public virtual void Stop()
{
if (isStopped) throw new InvalidOperationException("Server already Stopped!");
isStopped = true;
try
{
Listener.Shutdown(SocketShutdown.Both);
Listener.Close();
}
catch (Exception)
{
}
}
protected abstract void HandleClient(Socket Client);
}
public class PolicyServer : Server
{
public const String policyStr = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from>
<domain uri=""*"" />
</allow-from>
<grant-to>
<socket-resource port=""4530"" protocol=""tcp"" />
</grant-to>
</policy>
</cross-domain-access>
</access-policy>";
private byte[] policy = Encoding.ASCII.GetBytes(policyStr);
private static string policyRequestString = "<policy-file-request/>";
public PolicyServer(): base(943)
{
}
protected override void HandleClient(Socket socket)
{
TcpClient client = new TcpClient { Client = socket };
Stream s = client.GetStream();
byte[] buffer = new byte[policyRequestString.Length];
client.ReceiveTimeout = 5000;
s.Read(buffer, 0, buffer.Length);//read in the request string, but don't do anything with it
//you could confirm that it is equal to the policyRequestString
s.Write(policy, 0, policy.Length);
s.Flush();
socket.Shutdown(SocketShutdown.Both);
socket.Close(1);
client.Close();
}
}
我在运行聊天服务器组件其余部分的同一进程中托管了这个“服务器”。在HandleClient中设置断点以确认它是否正在接收请求。我认为套接字安全检查的工作方式需要使用应用程序使用的相同url字符串。为确保使用正确的字符串,我始终使用此字符串构造DNSEndPoint:
int Port = 4509;
DnsEndPoint ep = new DnsEndPoint(Application.Current.Host.Source.DnsSafeHost, Port, AddressFamily.InterNetwork);
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sock.NoDelay = true;
SocketAsyncEventArgs ea = new SocketAsyncEventArgs{RemoteEndPoint = ep};
//set up completed event handler et al.
sock.ConnectAsync(ea);
我在一个类似的聊天应用程序中使用了这段代码。通过使用Application.Current.Host.Source.DnsSafeHost
属性,您可以确保使用相同的Dns名称访问服务器,并使用浏览器用于HttpRequests的套接字
另外,如果您在端口943上提供访问策略文件,这是Silverlight中套接字支持的另一个要求
编辑
要确认您正在为策略文件提供服务,您可以执行许多操作
public abstract class Server
{
protected Socket Listener { get; set; }
protected int Port { get; private set; }
protected int Backlog { get; private set; }
protected bool isStopped { get; set; }
protected SocketAsyncEventArgs AcceptArgs {get;set;}
public Server(int port)
{
AcceptArgs = new SocketAsyncEventArgs();
AcceptArgs.Completed += new EventHandler<SocketAsyncEventArgs>(Accept_Completed);
isStopped = true;
Port = port;
Backlog = 100;
}
public Server(int port, int backlog)
{
isStopped = true;
Port = port;
Backlog = backlog;
}
public void Start()
{
isStopped = false;
Listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ep = new IPEndPoint(IPAddress.Any, Port);
Listener.ExclusiveAddressUse = true;
Listener.Bind(ep);
//Console.WriteLine("Listening on " + Port);
Listener.Listen(Backlog);
Listener.AcceptAsync(AcceptArgs);
}
void Accept_Completed(object sender, SocketAsyncEventArgs e)
{
if (isStopped) return;
Socket client = e.AcceptSocket;
//Console.WriteLine("Accepted Connection From: " + client.RemoteEndPoint.ToString());
e.AcceptSocket = null;
Listener.AcceptAsync(AcceptArgs);
HandleClient(client);
}
public virtual void Stop()
{
if (isStopped) throw new InvalidOperationException("Server already Stopped!");
isStopped = true;
try
{
Listener.Shutdown(SocketShutdown.Both);
Listener.Close();
}
catch (Exception)
{
}
}
protected abstract void HandleClient(Socket Client);
}
public class PolicyServer : Server
{
public const String policyStr = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from>
<domain uri=""*"" />
</allow-from>
<grant-to>
<socket-resource port=""4530"" protocol=""tcp"" />
</grant-to>
</policy>
</cross-domain-access>
</access-policy>";
private byte[] policy = Encoding.ASCII.GetBytes(policyStr);
private static string policyRequestString = "<policy-file-request/>";
public PolicyServer(): base(943)
{
}
protected override void HandleClient(Socket socket)
{
TcpClient client = new TcpClient { Client = socket };
Stream s = client.GetStream();
byte[] buffer = new byte[policyRequestString.Length];
client.ReceiveTimeout = 5000;
s.Read(buffer, 0, buffer.Length);//read in the request string, but don't do anything with it
//you could confirm that it is equal to the policyRequestString
s.Write(policy, 0, policy.Length);
s.Flush();
socket.Shutdown(SocketShutdown.Both);
socket.Close(1);
client.Close();
}
}
我在运行聊天服务器组件其余部分的同一进程中托管了这个“服务器”。在HandleClient中设置断点,以确认它是否正在接收请求。我的代码与您的代码完全相同,但仍然给出相同的错误。我手动创建了一个clientaccesspolicy.xml并将其放入默认的IIS文件夹中,因此它在80端口下提供服务。如何将其移动到943端口?创建新站点?我手动创建了一个端口为943的站点,并将策略文件移到了那里。它仍然无法工作…您能否确认客户端正在使用网络调试器成功地接收策略文件?我的代码与您的代码完全相同,但仍然给出相同的错误。我手动创建了一个clientaccesspolicy.xml并将其放入默认的IIS文件夹中,因此它在80端口下提供服务。如何将其移动到943端口?创建新站点?我手动创建了一个端口为943的站点,并将策略文件移到了那里。它仍然无法工作…您能否确认客户端正在使用网络调试器成功接收策略文件?