saneeha.nust 0 Light Poster

I want to make a sniffer that can capture all inward and outward traffic...
I am using the following code, but it captures only inward traffic..

 if (!bContinueCapturing)
                {
                    bContinueCapturing = true;

                    mainSocket = new Socket(AddressFamily.InterNetwork,
                        SocketType.Raw, ProtocolType.IP);

                    //Bind the socket to the selected IP address
                    mainSocket.Bind(new IPEndPoint(IPAddress.Parse(strIP), 0));

                    mainSocket.SetSocketOption(SocketOptionLevel.IP,            
                                               SocketOptionName.HeaderIncluded, 
                                               true);                           

                    byte[] byTrue = new byte[4] { 1, 0, 0, 0 };
                    byte[] byOut = new byte[4] { 1, 0, 0, 0 }; 


                    mainSocket.IOControl(IOControlCode.ReceiveAll,              
                                         byTrue,
                                         byOut);


                    mainSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None,
                        new AsyncCallback(OnReceive), null);
                }

private void OnReceive(IAsyncResult ar)
        {
            try
            {
                int nReceived = mainSocket.EndReceive(ar);

                //Analyze the bytes received...

                ParseData(byteData, nReceived);

                if (bContinueCapturing)
                {
                    byteData = new byte[4096];


                   mainSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None,
                        new AsyncCallback(OnReceive), null);
                }
            }

        }

Can someone plz guide what can be the problem...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.