One of the most frustrating part of developing a networking application is dealing with network related errors and figuring out what the cause of the error is and how you can fix it. Avaiability of sockets in Silverlight opens doors for building applications that are connected to server all the time and can respond to server side events immediately. Responding to server side events does not mean that there is some kind of PostBack or things like that. In terms of sockets it means that there is a socket open between silverlight application and server. Server can send messages to silverlight application in connected mode and then silverlight application can receive the messages and act on them accordingly. I will write more about Silverlight Socket Programming. This article is dedicated to diagnostics of some very common errors you will run into.
Before digging deep into details, lets look at some basic facts. Silverlight applications can only connect to a very small range of ports. Microsoft has assigned port number range 4502-4534 for silverlight applications. So first thing you will always have to check is if your application is trying to use a port number that does not fall in this range. In my applications, I have a check before I make sockets connection.
If (port < LOWER_PORT Or port > UPPER_PORT) Then Throw New ArgumentException("Port number is out of range(4502-4534)") End If
By design and default, Silverlight applications can not connect to Uri that are not in their own domain. The target location has to explcitly allow cross domain access. This is no different than what you are used to doing for Flash applications. You need to make sure that target server has CrossDomain.xml policy file in place to allow access on one or more ports for silverlight applications. For example, to allow connection on the whole range, your policy file may look like below.
<?xml version="1.0" encoding="utf-8" ?> <access-policy> <cross-domain-access> <policy> <allow-from> <domain uri="*" /> </allow-from> <grant-to> <socket-resource port="4502-4532" protocol="tcp" /> </grant-to> </policy> </cross-domain-access> </access-policy>
You have to make sure that this file is present at root of your server URL. And for non web servers, it actually does not matter where this file is located. Only thing that this matters is that your server should be able to read this file and respond to silverlight application. The way this policy verification works is that, before Silverlight application can establish a connection with your server, it will connect on port 943 and will send a message that reads <policy-file-request/<. So if there is no application on your server listening on port 943, Silverlight application will not get any response. And if there is an application that is listening on port 943, it will read this message and sends respons which will be stream of your crosspolicy.xml file. Then Silverlight application looks at the response and figures out what ports it can connect to. Lets look at some of the errors that you may come across.
Possible cause of this error could be:
This is one of the most common problems in all networking applications that port that your application wants to use, is blocked by firewall. Following steps describe how you can punch a hole in Windows firewall. If you are using some third party firewall, please consult their documentation on how to open a port in firewall.
This error most commonly due to name resolution issues in your network. If you are accessing a server by name of URI and get this error, adk your network administrator to check on DNS issues in network.
There are other socket errors that you may run into while running application. Those are mostly related to working of sockets. I will explain those in another article