public void Show_Bytes(PacketInfo pkgInfo)
        {
            //Display the raw packet data as ASCII characters
            if (showAscii == true)
            {
                textBlock.Inlines.Clear();

                textBlock.Inlines.Add(new Run("IP:")
                {
                    FontSize = 16, Foreground = Brushes.Green, TextDecorations = TextDecorations.Underline
                });
                textBlock.Inlines.Add(new Run("\n\n"));

                textBlock.Inlines.Add(new Run(pkgInfo.packetAscii.Substring(0, pkgInfo.IP._bHeaderLength)));

                textBlock.Inlines.Add(new Run("\n\n"));
                textBlock.Inlines.Add(new Run("TCP:")
                {
                    FontSize = 16, Foreground = Brushes.Blue, TextDecorations = TextDecorations.Underline
                });
                textBlock.Inlines.Add(new Run("\n\n"));

                textBlock.Inlines.Add(new Run(pkgInfo.packetAscii.Substring(pkgInfo.IP._bHeaderLength, pkgInfo.IP._usTotalLength - pkgInfo.IP._bHeaderLength)));
            }
            //Display the raw packet data as Hex
            else
            {
                textBlock.Inlines.Clear();

                textBlock.Inlines.Add(new Run("IP:")
                {
                    FontSize = 16, Foreground = Brushes.Green, TextDecorations = TextDecorations.Underline
                });
                textBlock.Inlines.Add(new Run("\n\n"));

                textBlock.Inlines.Add(new Run(pkgInfo.packetHex.Substring(0, pkgInfo.IP._bHeaderLength * 2)));

                textBlock.Inlines.Add(new Run("\n\n"));
                textBlock.Inlines.Add(new Run("TCP:")
                {
                    FontSize = 16, Foreground = Brushes.Blue, TextDecorations = TextDecorations.Underline
                });
                textBlock.Inlines.Add(new Run("\n\n"));

                textBlock.Inlines.Add(new Run(pkgInfo.packetHex.Substring(pkgInfo.IP._bHeaderLength * 2, (pkgInfo.IP._usTotalLength - pkgInfo.IP._bHeaderLength) * 2)));
            }
        }
        //Display the raw packet data as Hex
        private void hexButton_Checked(object sender, RoutedEventArgs e)
        {
            if (showAscii != false)
            {
                showAscii = false;

                if (dataGrid.SelectedItems.Count > 0)
                {
                    PacketData currentPacket = (PacketData)dataGrid.SelectedItem;

                    //Get the key of the packet that is currently selected in the datagrid
                    string index = currentPacket.Number;

                    PacketInfo pkgInfo = new PacketInfo();

                    //Get the packet from the buffer whose key is the index obtained above
                    if (pkgBuffer.TryGetValue(index, out pkgInfo))
                    {
                        textBlock.Inlines.Clear();

                        textBlock.Inlines.Add(new Run("IP:")
                        {
                            FontSize = 16, Foreground = Brushes.Green, TextDecorations = TextDecorations.Underline
                        });
                        textBlock.Inlines.Add(new Run("\n\n"));

                        textBlock.Inlines.Add(new Run(pkgInfo.packetHex.Substring(0, pkgInfo.IP._bHeaderLength * 2)));

                        textBlock.Inlines.Add(new Run("\n\n"));
                        textBlock.Inlines.Add(new Run("TCP:")
                        {
                            FontSize = 16, Foreground = Brushes.Blue, TextDecorations = TextDecorations.Underline
                        });
                        textBlock.Inlines.Add(new Run("\n\n"));

                        textBlock.Inlines.Add(new Run(pkgInfo.packetHex.Substring(pkgInfo.IP._bHeaderLength * 2, (pkgInfo.IP._usTotalLength - pkgInfo.IP._bHeaderLength) * 2)));
                    }
                }
            }
        }
        private void createPacketTree(object sender, SelectionChangedEventArgs e)
        {
            if (dataGrid.SelectedItems.Count > 0)
            {
                PacketData currentPacket = (PacketData)dataGrid.SelectedItem;

                //Get the key of the packet that is currently selected in the datagrid
                string index = currentPacket.Number;

                PacketInfo pkgInfo = new PacketInfo();

                //Get the packet from the buffer whose key is the index obtained above
                if (pkgBuffer.TryGetValue(index, out pkgInfo))
                {
                    //If the packet is a TCP packet, add it to the detailed tree view
                    if (pkgInfo.IP.Protocol == "TCP")
                    {
                        treeView.Items.Clear();

                        TreeViewItem IPnode = new TreeViewItem();
                        IPnode.Header     = "IP";
                        IPnode.Foreground = Brushes.Green;

                        IPnode.Items.Add(new TreeViewItem {
                            Header = "Protocol Version: " + pkgInfo.IP.Version
                        });
                        IPnode.Items.Add(new TreeViewItem {
                            Header = "Header Length: " + pkgInfo.IP.HeaderLength
                        });
                        IPnode.Items.Add(new TreeViewItem {
                            Header = "Type of Service: " + pkgInfo.IP.TypeOfService
                        });
                        IPnode.Items.Add(new TreeViewItem {
                            Header = "Total Length: " + pkgInfo.IP.TotalLength
                        });
                        IPnode.Items.Add(new TreeViewItem {
                            Header = "Identification No: " + pkgInfo.IP.Identification
                        });
                        IPnode.Items.Add(new TreeViewItem {
                            Header = "Flags: " + pkgInfo.IP.Flags
                        });
                        IPnode.Items.Add(new TreeViewItem {
                            Header = "Fragmentation Offset: " + pkgInfo.IP.FragmentationOffset
                        });
                        IPnode.Items.Add(new TreeViewItem {
                            Header = "TTL: " + pkgInfo.IP.TTL
                        });
                        IPnode.Items.Add(new TreeViewItem {
                            Header = "Checksum: " + pkgInfo.IP.Checksum
                        });
                        IPnode.Items.Add(new TreeViewItem
                        {
                            Header = String.Format("Source address: {0}: {1}", pkgInfo.IP.SourceAddress, pkgInfo.TCP.SourcePort)
                        });
                        IPnode.Items.Add(new TreeViewItem
                        {
                            Header = String.Format("Destination address: {0}: {1}", pkgInfo.IP.DestinationAddress, pkgInfo.TCP.DestinationPort)
                        });

                        TreeViewItem TCPnode = new TreeViewItem();
                        TCPnode.Header     = "TCP";
                        TCPnode.Foreground = Brushes.Blue;

                        TCPnode.Items.Add(new TreeViewItem {
                            Header = "Sequence No: " + pkgInfo.TCP.SequenceNumber
                        });
                        TCPnode.Items.Add(new TreeViewItem {
                            Header = "Acknowledgement Num: " + pkgInfo.TCP.AcknowledgementNumber
                        });
                        TCPnode.Items.Add(new TreeViewItem {
                            Header = "Header Length: " + pkgInfo.TCP.HeaderLength
                        });
                        TCPnode.Items.Add(new TreeViewItem {
                            Header = "Flags: " + pkgInfo.TCP.Flags
                        });
                        TCPnode.Items.Add(new TreeViewItem {
                            Header = "Window size: " + pkgInfo.TCP.WindowSize
                        });
                        TCPnode.Items.Add(new TreeViewItem {
                            Header = "Checksum: " + pkgInfo.TCP.Checksum
                        });
                        TCPnode.Items.Add(new TreeViewItem {
                            Header = "Message Length: " + pkgInfo.TCP.MessageLength
                        });

                        IPnode.Items.Add(TCPnode);

                        treeView.Items.Add(IPnode);
                    }

                    //Show the raw packet data in the text box to the right of the detailed tree view
                    Show_Bytes(pkgInfo);
                }
            }
        }
        private void ParseData(byte[] data, int numReceived)
        {
            string packetHex;
            string packetAscii;

            if (data.Length > 0 && numReceived != 0)
            {
                //Parse the IP packet
                PacketIP ipPacket = new PacketIP(data, numReceived);

                if (ipPacket.Protocol == "TCP")
                {
                    //Make the key of the packet equal to the number of packets that have been received thus far
                    string strKey = (numPacketsReceived + 1).ToString();

                    //Parse the TCP packet
                    PacketTcp tcpPacket = new PacketTcp(ipPacket.Data, ipPacket.MessageLength);

                    //Convert the packet's bytes to Hex and ASCII
                    packetHex   = BitConverter.ToString(byteData).Replace("-", String.Empty).Substring(0, numReceived * 2);
                    packetAscii = Encoding.ASCII.GetString(byteData).Substring(0, numReceived);

                    //Create a PacketInfo object to store in the dictionary
                    PacketInfo pkgInfo = new PacketInfo(ipPacket, tcpPacket, packetHex, packetAscii);

                    //Create a PacketData object to populate the datagrid
                    PacketData packet = new PacketData
                    {
                        Number      = (numPacketsReceived + 1).ToString(),
                        Time_Stamp  = DateTime.Now.ToString("HH:mm:ss:") + DateTime.Now.Millisecond.ToString(),
                        Source      = ipPacket.SourceAddress.ToString() + ":" + tcpPacket.SourcePort,
                        Destination = ipPacket.DestinationAddress.ToString() + ":" + tcpPacket.DestinationPort,
                        Protocol    = ipPacket.Protocol,
                        Length      = ipPacket.TotalLength,
                        Info        = tcpPacket.Flags
                    };

                    //If the buffer is not full, add the packet to the buffer and display it in the datagrid
                    if (pkgBuffer.Count < maxBufferSize)
                    {
                        pkgBuffer.Add(strKey, pkgInfo);
                        numPacketsReceived++;

                        Dispatcher.Invoke((() =>
                        {
                            dataGrid.Items.Add(packet);
                            bufferProgress.Value = (double)numPacketsReceived;
                            percentLabel.Content = (Math.Round(((double)numPacketsReceived / (double)maxBufferSize), 2) * 100).ToString() + "%";
                        }), DispatcherPriority.ContextIdle);
                    }
                    else
                    {
                        //Stop capturing packets because the buffer is full
                        MessageBox.Show("The packet buffer has reached its maximum capacity. Clear the buffer or increase the maximum buffer size in order to continue.",
                                        "Packet Sniffer", MessageBoxButton.OK, MessageBoxImage.Warning);

                        Dispatcher.Invoke((() =>
                        {
                            Stop_Capturing();
                        }), DispatcherPriority.ContextIdle);
                    }
                }
            }
        }