问题
I've got a silverlight client, using SL4, and a C# server using .NET 4. The server opens a port on 4525, to which the client is intended to connect. Now, if I can ask that you simply read the following, we can probably avoid me having to post any further code:
The client attempts to make the connection to the server, opens up the connection for the policy file, requests the policy file, and gets returned the policy file. What is not happening, is the onConnect event in the client. Before I implemented a policy server, the client would connect on 4525, and execute the onConnect event, but immediately fail because there was no policy server. Now that I have implemented the policy server, it appears that it never detects the finalization of its delivery.
Here is the transmission summary:
Client:
<policy-file-request/>
Server:
<?xml version="1.0" encoding ="utf-8"?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from>
<domain uri="*:4525" />
</allow-from>
<grant-to>
<socket-resource port="4525" protocol="tcp" />
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
Effectively; the only question I am trying to answer is this: Am I missing something from my policy file, or do I need to conclude its submission to the client in any particular manner, in order for my client to detect its delivery?
Things of note:
The Client successfully establishes its connection to the policy server
The policy server successfully writes the policy back to the client
The 'real' servers port is successfully opened
The client 'can' connect to the 'real' server
Thank you all for your time, I am sure someone will be able to help.
Code Request Client Connection
Not sure that it's of any further consequence toward finding an answer; but:
private static DnsEndPoint IP_END_POINT = new DnsEndPoint("192.168.0.36", 4525, AddressFamily.InterNetwork);
public MainPage()
{
try
{
InitializeComponent();
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
SocketAsyncEventArgs connectEventArgs = new SocketAsyncEventArgs();
connectEventArgs.RemoteEndPoint = IP_END_POINT;
connectEventArgs.Completed += new EventHandler<SocketAsyncEventArgs>(ConnectionEstablished);
socket.ConnectAsync(connectEventArgs);
}
catch (Exception e)
{
}
}
New Development 'Connection Established' fires!
So it turns out that if I want a minute or so, my 'ConnectionEstablished' event does eventually fire. What I get from it, however, is not so encouraging.
I ended up with a SocketError of 'AccessDenied', and a message of 'An attempt was made to access a socket in a way forbidden by its access permissions.', and as can be expected, my server never accepts the connection. I know that the server has the port open, and can accept a connection, so this is quite the perplexing scenario.
Thanks again for anyone's help.
回答1:
Try:
private static DnsEndPoint IP_END_POINT = new DnsEndPoint("192.168.0.36", 4525, AddressFamily.InterNetwork);
public MainPage()
{
try
{
InitializeComponent();
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
SocketAsyncEventArgs connectEventArgs = new SocketAsyncEventArgs();
connectEventArgs.RemoteEndPoint = IP_END_POINT;
connectEventArgs.Completed += new EventHandler<SocketAsyncEventArgs>(ConnectionEstablished);
if(!socket.ConnectAsync(connectEventArgs))
{
ConnectionEstablished(/*Insert Args*/); //Handle real time execution
}
}
catch (Exception e)
{
//write error somewhere
}
}
The documentation indicates if ConnectAsync
returns false then "The SocketAsyncEventArgs.Completed event on the System.Net.Sockets.SocketAsyncEventArgs object passed in the e parameter will not be raised."
Also, make sure you are logging the exception somewhere. There might be an exception which you are just breezing past.
Update:
- Did you check the ClientAccessPolicy.xml file? You would need to replace all the “ and ” characters with ".
- Have you tried adding
connectEventArgs.UserToken = socket;
before theConnectAsync(connectEventArgs)
call? - Is the server receiving the request and sending a reply (may require the use of Wireshark on the client)?
- While debugging does
connectEventArgs.Completed
point to the correct method? - While debugging, what happens if you "Step Over" (F10) until the method returns? Are other methods called?
- While debugging, if you "Step Over" (F10)
socket.ConnectAsync(connectEventArgs)
(but are still in the method) are there other threads in the Debug>Windows>Threads window? What happens if you switch to the other thread (right click on thread and select "Switch To Thread" or double click the thread)?
Let me know how things turn out.
来源:https://stackoverflow.com/questions/10541699/why-does-my-silverlight-socket-policy-file-not-work