Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8511797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:06:23+00:00 2026-06-11T04:06:23+00:00

I have a book called Network Programming in .NET which has the following code

  • 0

I have a book called Network Programming in .NET which has the following code sample for sniffing IP packets on the network stack. I have copied the code sample WORD-FOR-WORD from the book so please excuse the lack of stylistic conventions etc.

List<string> packets = new List<string>();

public void Run()
{
    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 SIO_RCVALL = unchecked((int)0x98000001);
    int ret_code = socket.IOControl(SIO_RCVALL, 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);
    }
}

public void Receive(byte[] buf, int len)
{
    if(buf[9] == 6)
    {
        packets.Add(Encoding.ASCII.GetString(buf).Replace("\0", " "));
    }
}

The first time I tested this was a few years ago (before even Vista existed) and the machine I used was a 32bit Windows XP Proferssional platform with a NIC running on IPv4.

I am now trying to test on a 64bit Windows 7 platform with a NIC running IPv6 but its not working. I am assuming this is something to do with IPv6. Can anyone suggest how I might fix this efficiently?

EDIT: This is the exception I get when I try to run…

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T04:06:25+00:00Added an answer on June 11, 2026 at 4:06 am

    Headers are significantly different between IPv4 and IPv6.

    So the check of:

    if(buf[9] == 6)
    

    Which checks whether the packet is TCP for IPv4 is querying part of the source address field of an IPv6 packet. For IPv6, it ought to be checking “Next Header” which is at offset 6*. Of course, now you’ll also have to check the IP version first to know whether to check offset 6 or offset 9.


    For the exception message, it’s likely that you’re not running as Administrator, which you need to be to listen on raw sockets (native documentation, but still applicable):

    To use a socket of type SOCK_RAW requires administrative privileges. Users running Winsock applications that use raw sockets must be a member of the Administrators group on the local computer, otherwise raw socket calls will fail with an error code of WSAEACCES. On Windows Vista and later, access for raw sockets is enforced at socket creation. In earlier versions of Windows, access for raw sockets is enforced during other socket operations.


    (*) Of course, there may be multiple IPv6 headers to work through before you discover that it’s a TCP packet.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a book called Scientific Programming with Python (2009), in which example code
I am learning how to code through this book called Headfirst Programming, which I
I have a collection of Book objects called book. The Book class has a
I have a SQL Server 2008 database. This database has a Table called Book.
Let's say I have an ActiveRecord model called Book that has a has_many association
I have recently bought a book called The c# programming language 4th edition. On
this is a really basic question - I have a model called Book, which
I'm currently reading a great book called 'Programming Collective Intelligence' by Toby Segaran (which
I have a model, called Book , that has the fields title and filename
I have a book called Spring Recipes which seems very good so far, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.