Socket Ioctls


Socket I/O control codes (ioctls) are somewhat related to socket options. Socket ioctls typically return information about the socket or some network characteristic, such as enumerating all the local IP addresses. The Socket method for calling an ioctl is IOControl . Unfortunately, the .NET Framework does not offer any predefined ioctls and their corresponding structures, so to call an ioctl, you might need to use the StructLayout class to define the data returned. For a complete listing of the Winsock ioctls, consult Network Programming for Microsoft Windows, Second Edition by Anthony Jones and Jim Ohlund (Microsoft Press, 2002), which devotes a chapter to Winsock ioctls. The Microsoft Platform SDK contains information about the most common ioctls under the WSAIoctl entry. The following code shows the prototype for the IOControl method:

C#

 publicintIOControl(intioControlCode, byte[]optionInValue, byte[]optionOutValue); 

Visual Basic .NET

 PublicFunctionIOControl(_ ByValioControlCodeAsInteger,_ ByValoptionInvValue()AsByte,_ ByValoptionOutValue()AsByte_) 

The ioControlCode value is the ioctl being called. These values can be found in the C header files winsock2.h and ws2tcpip.h. Note that the ioctls in the C header file are unsigned, which can lead to some annoying compilation errors in C# and Microsoft Visual Basic .NET because the ioControlCode is defined as a signed integer. In this case, the values simply have to be converted to the equivalent negative number, or the Unchecked operator may be used in C# to prevent integer overflow validation. The input value is the byte array of the expected value, and because few ioctl objects are defined in the .NET Framework, applications will have to build these byte arrays by hand to match the required ioctl objects layout. Likewise, the output is a byte array specific to each ioctl that applications will have to decode appropriately.

The RawSocket sample located at Chap08\rawsocket\cs illustrates calling the IOControl with the Winsock ioctl SIO_ROUTING_INTERFACE_QUERY . This ioctl takes the equivalent of a SocketAddress structure (the equivalent of the Winsock sockaddr structure for a particular address family) that indicates the address of a remote destination. On output, a SocketAddress structure is returned that describes the local interface on which the destination is reachable ; the SIO_ROUTING_INTERFACE_QUERY ioctl performs a route lookup.

Setting a Socket to Non-Blocking Mode

If you are familiar with the Winsock API, you know that the most common ioctl is the Winsock define FIONBIO , which is used to change a socket from blocking to non-blocking mode. All code samples in this chapter so far have shown blocking sockets. When a socket is put into the non-blocking mode, the operation will throw an exception if it cant be completed immediately. In the .NET Framework, however, the FIONBIO ioctl is not exposed through the IOControl method but as a property of the Socket class called Blocking .

Non-blocking sockets should be avoided if possible because theyre inefficient and prone to logic errors. The non-blocking mode is a carryover from BSD Sockets and Winsock 1.1, where it was the only alternative to blocking sockets; there were no asynchronous operations. Setting a socket to non-blocking mode allowed the application to service multiple sockets at once with the drawback of requiring the application to poll for events (such as read and write) on the socket. The Socket class exposes a Poll method that does this. However, polling for events leads to inefficient network usage because the application must spend time querying each socket for certain events. Its much simpler and more efficient to use asynchronous pattern method operations instead, as described in Chapter 3.




Network Programming for the Microsoft. NET Framework
Network Programming for the MicrosoftВ® .NET Framework (Pro-Developer)
ISBN: 073561959X
EAN: 2147483647
Year: 2003
Pages: 121

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