0

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.

DigitalJedi805
  • 1,486
  • 4
  • 16
  • 41
  • Hard to say without knowing what system you are using to connect the Silverlight app with the server. – Trisped May 10 '12 at 21:07
  • What do you mean 'System'? I'm running in a Windows 7 Environment, client and server. Chrome is running the client. Server opens a TcpListener for both the Policy Server and the 'Real' Server. – DigitalJedi805 May 10 '12 at 21:08
  • What else would you like to know? – DigitalJedi805 May 10 '12 at 21:09
  • To the best of my knowledge there is only one way to connect a Silverlight Socket to any sort of server, and that is via the ConnectAsync method, which, as I mentioned, I know works. – DigitalJedi805 May 10 '12 at 21:11
  • How does the client connect to the TcpListener? The issue is probably there. I do not have my socket code with me, but if you can post your code (illustrating how the client starts the connection) I will look at it when I get home in ~6 hours. – Trisped May 10 '12 at 21:20
  • Edited my code to include this, but I'm not sure how posting code illustrating what I just verbalized in my last comment helps. Sorry to be frustrated, but I just got yelled at for asking that people not drag me through 'unnecessary mud'. Since you insist that the code is necessary, it's there, and I truly do appreciate your help; I just don't see how it's going to help any more than what I have already explained. :-/ – DigitalJedi805 May 10 '12 at 21:27
  • Cool, thanks. As a rule of thumb, provide the code which is having the issue. Not all of the code, usually the method will do. Also, try not to include commentary which is not directly related to the problem. I will look this over when I get home and let you know what I think. – Trisped May 10 '12 at 21:38
  • Thanks. I'll refrain from commenting; as all-consuming as the temptation is. – DigitalJedi805 May 10 '12 at 21:42

1 Answers1

2

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 the ConnectAsync(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.

Trisped
  • 5,705
  • 2
  • 45
  • 58
  • Modified to this specification, and in debugging, it hits the '!socket.ConnectAsync()', catches a 'false' evaluation ( of this logic ), and continues to return. ( ConnectAsync returns true ) – DigitalJedi805 May 11 '12 at 16:17
  • @DigitalJedi805 I added some additional trouble shooting options. – Trisped May 11 '12 at 18:14
  • @DigitalJedi805 Sorry, missed your update. I would suggest using Wireshark to verify the network configuration matches the ClientAccessPolicy you are serving. – Trisped May 11 '12 at 18:32
  • Thanks @Trisped, I actually managed to overcome this somehow. I'll likely add another answer. Thank you for your help. – DigitalJedi805 May 11 '12 at 19:38
  • What was your solution in the end? – ceebreenk Oct 20 '14 at 21:34
  • @honeycomb You need to add @ DigitalJedi805 (without the space) so he gets the alert that you asked a question (since he did not write what you are commenting on). I suggest commenting on his question since you are asking what he did rather then what I did. – Trisped Oct 20 '14 at 22:29
  • Thanks, my bad, it's turning into a late night over here. I managed to sort it out 5 minutes ago but for interest sake, @DigitalJedi805 how did you solve your problem? – ceebreenk Oct 20 '14 at 22:36