Захват IP-пакетов моим собственным BitReader (чтение бит по битам)
public class BitReader
{
int Index;
byte Current;
Stream Reader;
public BitReader ( Stream Memory )
{
Reader = Memory;
}
public bool? ReadBit ( )
{
if ( Index == 8 )
{
Index = 0;
Current = ( byte ) Reader . ReadByte ( );
}
if ( Current == -1 )
{
return null;
}
else
{
return ( Current & ( 1 << ( 7 - Index++ ) ) ) != 0;
// return ( Current & ( 1 << Index++ ) ) != 0;
}
}
}
Получение неизвестного байта ZERO перед пакетами IP v4, как показано ниже,
00000000 01000101 00000000 00000101 11000000 01001001 00100110 01000000 00000000 10000000 00000110 10110010 01111110 11000000 10101000 00000001 00001010 01000101 10101011 11110010 00110101
101 *
private byte [ ] Buffer = new byte [ 4096 ];
_Socket = new Socket ( AddressFamily . InterNetwork , SocketType . Raw , ProtocolType . IP );
_Socket . Bind ( new IPEndPoint ( IPAddress . Parse ( Interface . Text ) , 0 ) );
_Socket . SetSocketOption ( SocketOptionLevel . IP , SocketOptionName . HeaderIncluded , true );
_Socket . IOControl ( IOControlCode . ReceiveAll , BitConverter . GetBytes ( 1 ) , BitConverter . GetBytes ( 1 ) );
_Socket . BeginReceive ( Buffer , 0 , Buffer . Length , SocketFlags . None , new AsyncCallback ( OnReceive ) , null );
101 * *1014* 101 * *1014* 101 * *1014* 101 * *1014* 101 * *1014* 101 * *1014* 101 * *1014*
Редактировать 1:
MemoryStream _MemoryStream = new MemoryStream ( Buffer , 0 , Length );
BitReader _BitReader = new BitReader ( _MemoryStream );
for ( int Loop = 0 ; Loop < 21 ; Loop++ )
{
bool? Result = _BitReader . ReadBit ( );
if ( Loop % 8 == 0 && Loop != 0 )
{
myTextBox . Invoke ( new Action ( delegate { myTextBox . Text += " "; } ) );
}
if ( Result . HasValue )
{
myTextBox . Invoke ( new Action ( delegate { myTextBox . Text += Result . Value ? 1 : 0; } ) );
}
}
Редактировать 3:
MemoryStream _MemoryStream = new MemoryStream(Buffer, 0, Length);
BinaryReader _BinaryReader = new BinaryReader(_MemoryStream );
private byte VersionAndHeaderLength;
VersionAndHeaderLength = _BinaryReader.ReadByte();