I am frequently asked why NetCF applications encounter a SocketException when attempting to use some socket options, when applications written to the full .NET Framework do not. The companion question is invariably "Is this a bug in NetCF?".
If the result is WSAENOPROTOOPT (10042), what we are seeing is limitations of the operating system -- in this case, Windows CE -- so the answer is “no, this is not a bug, it is a limiation of the underlying operating system.“
Since the beginning, the .NET Compact Framework was designed to be portable -- run on more than one operating system. Because of this, we did not limit the available options in the SocketOptionNames enum. When calling Socket.GetSocketOption() or Socket.SetSocketOption(), the request is forwarded on to the operating system to do the work. If the result is a failure, the return code is wrapped into a SocketException and thrown.
The list of socket options which you can expect to throw, as described above, can be seen below (taken from the Windows CE API reference).
GetSocketOptionBSD options not supported for getsockopt are as follows.
SetSocketOptionThe following table shows BSD options not supported for setsockopt .
If you are writing portable code (runs on both the .NET Framework and the .NET Compact Framework), you will need to handle this exception and change your application behavior as needed. For example, you may wish to disable controls related to expert socket features (ex: Receive Timeout). The code fragment below shows
using System.Net;using System.Net.Sockets;
class SocketHelpers{ bool ReceiveTimeoutSupported(Socket socket) { try { // try to get ReceiveTimeout option (this will get the default value) Int32 timeout = (Int32)socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout);
// try to set what we received (we don't want to change the default value) socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, timeout);
// if we get here, ReceiveTimeout is supported return true; } catch(SocketException e) { // check to see if the selected option is supported if(10042 != e.ErrorCode) // WSAENOPROTOOPT { // ReceiveTimeout not supported by operating system return false; } else { // no, let the caller handle the exception throw; } } }}
I hope this helps clear up some of the confusion around socket options and the .Net Compact Framework.
-- DK
Disclaimer:This posting is provided "AS IS" with no warranties, and confers no rights.