System.Net.Sockets Namespace


The majority of networking functionality provided by the .NET Micro Framework resides in the Sockets subnamespace. Specifically, the functionality resides in the Socket class, although the remainder of the namespace consists primarily of enumerated types.

Socket Class

The Socket class is the cornerstone of all the networking functionality. It is quite versatile in that it can function as either a network client or server using either TCP or UDP. The full .NET Framework is even more capable, with support for a wide variety of address types, socket types, and protocols (as evidenced by the respective enumerations) that are not available in the .NET Micro Framework.

Rather than simply provide a method-by-method reference to the Socket class, which you will find in the software development kit (SDK) documentation, we offer the "Building a Connected Flashlight" section later in this chapter, which will walk through a complete example that will provide an opportunity to discuss each of the features of this class in context. However, the following sections do describe the various configuration options available for controlling socket behavior, as well as general error handling.

Socket Options

A variety of configuration information and control is provided through the socket option instance methods GetSocketOption and SetSocketOption. Two associated enumerations are used when calling these methods: SocketOptionLevel, which scopes the operation, and SocketOptionName, which is the identifier of the option to get or set. Not all values specified in these enumerations are valid for the .NET Micro Framework for every implementation. Please refer to the vendor-specific documentation for details.

Socket Option Levels

The value of this enumeration determines which protocol the option operation (get/set) should apply to the socket. The possible values are: IP, IPv6, Socket, Tcp, and Udp. Of these, IPv6 is not supported at all. Tcp and Udp will be supported on most platforms, but IP may not. A value of Socket indicates that the operation should apply to the socket as a whole (that is, not scoped to a specific protocol) and is the most commonly specified value. If you specify either Tcp or Udp, then the socket must have been configured to use the corresponding protocol type (that is, you can't specify SocketOptionLevel.Udp on a ProtocolType.Tcp socket).

Socket Option Names

The SocketOptionNames enumeration contains the various values that are potentially available to you through the GetSocketOption or SetSocketOption methods. With one exception, these are internal identifiers used by the underlying implementation for accessing the corresponding configuration item. (The lone exception in this approach is the MaxConnections value, which is simply a constant. This is discussed more fully in the section titled "Listening for Clients" later in this chapter, in the description of the Listen method.)

Getting a Socket Option

There are two flavors of GetSocketOption: one that returns a generic object and one that accepts a buffer (byte[]). For simple values, it is easiest to use the first approach and coerce the return type as needed:

 int sendTimeout= (int)listenSocket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout); 

The second approach of passing a buffer is rarely used because most values are either integral or Boolean:

 byte[] sendTimeoutBuffer = new byte[4]; listenSocket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Type, sendTimeoutBuffer); 

Most options, even if they are logically Boolean, return an int, so you'll need to further coerce the return value:

 bool noDelay = (int)socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay) == 1; 

Setting a Socket Option

Setting socket options is very similar to getting them, with the exception that you have an additional convenience method that accepts a Boolean value:

 listenSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, true); 

SocketException

In the course of developing your networked applications, you will inevitably receive an exception from the runtime. A socket-specific exception has been defined that you can catch and check for error codes using the ErrorCode property, which is the only extension made by this class to the base Exception class. As an example, running within the emulator, the following code incorrectly uses the MaxConnections constant as an option name, resulting in a Socket Exception exception being thrown:

 try {    int maxConnections = (int)listenSocket.GetSocketOption(SocketOptionLevel.Socket,    SocketOptionName.MaxConnections); } catch (SocketException ex) {    Debug.Print("Error code: " + ex.ErrorCode.ToString()); } 

The output of which is:

 Error code: 10042 

Searching MSDN for Winsock error codes, I discovered that this error code translates to WSAENOPROTOOPT, or, in English, a "bad protocol option" was specified. As usual, different vendors may use different error codes, so please refer to the documentation supplied with your hardware.




Embedded Programming with the Microsoft .Net Micro Framework
Embedded Programming with the Microsoft .NET Micro Framework
ISBN: 0735623651
EAN: 2147483647
Year: 2007
Pages: 118

Similar book on Amazon
Getting Started with the Internet of Things: Connecting Sensors and Microcontrollers to the Cloud (Make: Projects)
Getting Started with the Internet of Things: Connecting Sensors and Microcontrollers to the Cloud (Make: Projects)
Expert .NET Micro Framework (Expert's Voice in .NET)
Expert .NET Micro Framework (Expert's Voice in .NET)
Netduino Plus
Netduino Plus
Getting Started with Netduino
Getting Started with Netduino

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net