How to diagnose Silverlight socket applications

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.

Available Ports

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

CrossDomain Policy

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" ?>
 <domain uri="*" />
  <socket-resource port="4502-4532" protocol="tcp" />

Where to put Crossdomain.xml policy file?

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.

SocketAsyncEventArgs.SocketError = AccessDenied

Possible cause of this error could be:

  • Your target server is not listening on port 943. Check if the server application is even running and listening to any requests at all.
  • Your taret server's cross domain policy file does not allow connection on port range 4502-4534. Ask server administrator to modify corssdomain.xml file.

SocketAsyncEventArgs.SocketError = ConnectionRefused

  • Crossdomain policy does not allow access on port that silverlight application wants to connect to.
  • Server firewall does not allow any connection on port that silverlight application is trying to connect to. Use the following steps to open a port in firewall (Windows inbuilt firewall).

How to open a port in network firewall?

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.

  • Goto Startup > Settings > Control Panel
  • Towards the end of the icon list, you will see Windows Firewall, double click on it and bring up firewall configuration wizard. It will look like image below

  • Click on Exceptions tab and there click on Add Port button as shown below.

  • In Add Port dialog box, enter some name for this port so that later you can refer to it and know what this port was for. And enter a valid port number that you want allow access to. And hot OK to save your changes.


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.

Other Errors

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




Monthly Posts

Blog Tags