dotnet-exception-handling

.NET Exceptions – System.Net.Sockets.SocketException

Making our way through our .NET Exception Handling series, today we’re going to take a look at the System.Net.Sockets.SocketException. Typically, a System.Net.Sockets.SocketException is thrown when an error occurs within a socket, such as a failure to connect to a remote network.

In this article we’ll examine the System.Net.Sockets.SocketException in more detail, including where it resides within the .NET exception hierarchy, along with some working code examples to illustrate how System.Net.Sockets.SocketExceptions might be thrown in real-world code. Let’s get this party started!

The Technical Rundown

When Should You Use It?

As with most of the .NET exceptions, it’s easiest to understand how System.Net.Sockets.SocketException works by just starting right out with a code example. The full code is directly below, after which we’ll take a bit of time to examine it:

We won’t get into the Utility namespace or Logging class because we’ve used that many times before and it’s just a means of simplifying our output needs during development. Instead, let’s look at the meat of our example, which resides in the Program class where we defined two simple methods to help us attempt to establish a socket connection. As you may recall, a socket (or network socket) is an endpoint on another system for sending/receiving network data. Most commonly, a socket is used to generate a socket address, which consists of an IP address and port number that is used to connect with another machine.

With that in mind we have our ParseIPEndPoint() method, which takes the provided server string value in the typical form of address:port, and attempts to parse a valid uri from that provided server IP and port. We aren’t covering anywhere near the full spectrum of possible Uris or connection types here, just the basics with and without http, but this should illustrate the point.

If a valid uri is created, we return a new IPEndPoint instance, which is a .NET class that allows us to easily store network endpoint information (ip, port, etc). If the uri parse fails, we throw an exception.

Now, we make use of the ParseIPEndPoint() method in the ConnectToSocket() method, which also expects a single string parameter representing our ip:port. We parse that and generate an IPEndPoint, then create a new Socket using the IPEndPoint that was generated. We’re connecting via TCP protocol (just like most web browsers), so that’s all fine. Finally, we attempt to establish a connection to our socket instance via the socket.Connect() method. If all goes well, we output a message indicating the connection was made, then Close() the connection.

To test this out and see how a System.Net.Sockets.SocketException might be thrown, we have three basic calls to our ConnectToSocket() method, one that works and two that will fail:

The first call works fine because my localhost machine (127.0.0.1) accepts a connection to the default http port (80):

However, the second attempt fails because my machine does not accept connections on port 4444 (I tried to pick a random port number here, though some machines may run services on this port). As a result, my computer refuses the connection, which causes our little application to throw a System.Net.Sockets.SocketException, as we expected:

Lastly, we try establishing a socket connection with a remote server, in this case one of Google’s DNS servers (8.8.8.8). Once again, our connection attempt fails, though for a slightly different reason this time:

In this case, while it is valid to issue a ping 8.8.8.8 request to Google’s DNS, it doesn’t allow a socket connection to be established, so the attempt fails and an exception is thrown.

To get the most out of your own applications and to fully manage any and all .NET Exceptions, check out the Airbrake .NET Bug Handler, offering real-time alerts and instantaneous insight into what went wrong with your .NET code, along with built-in support for a variety of popular development integrations including: JIRA, GitHub, Bitbucket, and much more.