I tried – for the first time – to control a socket by such a low level way
Walking through tutorial, I faced these lines :
byte[] byTrue = new byte [4] {1, 0, 0, 0};
byte[] byOut = new byte [4] {1, 0, 0, 0};
_Socket.IOControl(IOControlCode.ReceiveAll, byTrue, byOut);
I referred to the MSDN documentation, but it’s really very ambiguous.
Question:
What are these 2 bytes’ arrays? What are they used in?
The documentation says OPTIONS – what options?
The byte array arguments to
Socket.IOControl()are specific to theIOControlCodethat is specified as the first argument.The first array is for input data, the second array is for output data.
To find more information about the specific layout for these arrays, it is helpful to start by looking at the enumeration, which lists all the possible operations for you. From there, you should cross reference with the documentation listed for the C-operable functions that
Socket.IOControl()wraps. These areWSAIoctl()andioctlsocket().IOControlCode Enumeration @ MSDN
WSAIoctl() function @ MSDN
ioctlsocket() function @ MSDN
Per the documentation for
IOControlCode.ReceiveAll:Cross referencing
SIO_RCVALL, we find it has an entry in the winsock documentation.SIO_RCVALL control code @ MSDN
Reading through this entry, it mentions that the input buffer is required to select a mode of operation, with minimum size corresponding to a
RCVALL_VALUE. In your statement you are passing the value1in the input argument array. We can check header files to see what this should mean.You should double check this with C header files at hand, but the interface-compatible copy for wine (windows emulator) says the value for mode
RCVALL_ONis1. The .NET method should wrap the details of managing array size for you, so the code snippet you encountered is trying to enableRCVALL_ONfor the socket.Here is the link to the C header file at winehq.org:
mstcpip.h at winehq.org
If you’ve installed C/C++ support for visual studio, you may be able to locate mstcpip.h and winsock2.h locally at a path similar to the following:
My copy says:
I couldn’t find any documentation that specifically says what size byte array should be used to pass a
RCVALL_VALUE, but if you look at the the samples forIOControl(), they useBitConverter.GetBytes(0)for default parameters which would have a size of 4 bytes (corresponding to a .NET int) and matches your example. This is large enough to fit aRCVALL_VALUEin C operation as well.