2

I am using the following C# code for getting network packets.

int len_receive_buf = 4096;
int len_send_buf = 4096;
byte[] receive_buf = new byte[len_receive_buf];
byte[] send_buf = new byte[len_send_buf];
int cout_receive_bytes;
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
socket.Blocking = false;
IPHostEntry IPHost = Dns.GetHostByName(Dns.GetHostName());
socket.Bind(new IPEndPoint(IPAddress.Parse(IPHost.AddressList[0].ToString()), 0));
socket.SetSocketOption(SocketOptionLevel.IP , SocketOptionName.HeaderIncluded, 1);

byte []IN = new byte[4]{1, 0, 0, 0};
byte []OUT = new byte[4];            
int ret_code = socket.IOControl(IOControlCode.ReceiveAll, IN, OUT);           
while (true)
{
    IAsyncResult ar = socket.BeginReceive(receive_buf, 0, len_receive_buf, SocketFlags.None, null, this);
    cout_receive_bytes = socket.EndReceive(ar);
    Receive(receive_buf, cout_receive_bytes, countPckts);
}

I get network packets too which are starting from 'IP Header'. But I need network packets including 'Frame Header'/'Ethernet Header'. Where i must need to have the Ethernet header portion too.

Please help me, how can i get network packet including 'Ethernet Header' in C#.

Matt Ellen
  • 11,268
  • 4
  • 68
  • 90
Saiful Islam
  • 124
  • 2
  • 14
  • 2
    It seems you cannot do it with .Net framework. Here is the answer: http://stackoverflow.com/questions/3964013/how-send-raw-ethernet-packet-with-c – Eugen Nov 20 '11 at 13:44

0 Answers0