Packet sniffer socket exception
On the following code
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
s.Bind(new System.Net.IPEndPoint(IPAddress.Parse("127.0.0.1"),0));
s.SetSocketOption(SocketOptionLeve开发者_Go百科l.IP, SocketOptionName.HeaderIncluded, true);
byte[] bin = new byte[4]{1, 0, 0, 0};
byte[] bout = new byte[4];
s.IOControl(IOControlCode.ReceiveAll, bin, bout);
I get the following exception:
Socket Exception: An invalid argument was supplied
On the following line
s.IOControl(IOControlCode.ReceiveAll, in, out);
But I dont understand why I've supplied a wrong argument.
Okay I've found it.
127.0.0.1 is not a proper IP adress annotation for this. I have to use my local one (192.168.0.100);
Darn, it's always so easy.
See the MSDN article and the IOControl doc, specifically for .ReceiveAll:
Enable receiving all IPv4 packets on the network. The socket must have address family InterNetwork, the socket type must be Raw, and the protocol type must be IP. The current user must belong to the Administrators group on the local computer, and the socket must be bound to a specific port. This control code is supported on Windows 2000 and later operating systems. This value is equal to the Winsock 2 SIO_RCVALL constant.Winsock 2 SIO_RCVALL constant.
Is it possible you aren't in the Administrators group, or you bound to an incorrect port? It seems like you set the other options correctly. Although, having said all that, your code looks identical to this example so it should work? Can you dig further into the exception to read possibly more detailed inner exceptions?
精彩评论