Click here to Skip to main content
15,894,180 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to write Raw Socket for getting Payload data from TCP Packet..
I have tried WinPcap unable to fetch Payload data... that only show number bytes in Payload is provided..

What I have tried:

C#
public void Run()
      {

          using (PacketCommunicator communicator = 
                selectedAdapter.Open(65536,                                 // portion of the packet to capture
                                                                            // 65536 guarantees that the whole packet will be captured on all the link layers
                                    PacketDeviceOpenAttributes.Promiscuous, // promiscuous mode
                                    1000))                                  // read timeout
           {
                 
               if (communicator.DataLink.Kind != DataLinkKind.Ethernet)
                {
                   listBox1.Items.Add("This program works only on Ethernet networks.");
                    return;
                }

                // Compile the filter
                using (BerkeleyPacketFilter filter = communicator.CreateFilter("ip and tcp"))
                {
                    // Set the filter
                    communicator.SetFilter(filter);
                }

               listBox1.Items.Add("Listening on " + selectedAdapter.Description + "...");
                // start the capture
                communicator.ReceivePackets(0, PacketHandler);
           }

         }
       
        
        public  void PacketHandler(Packet packet)
        {

            listBox1.Items.Add(packet.Timestamp.ToString("yyyy-MM-dd hh:mm:ss.fff") + " length:" + packet.Length);
            
            IpV4Datagram ip = packet.Ethernet.IpV4;
            TcpDatagram tcp = ip.Tcp;
            
            listBox1.Items.Add(" ** Source **" + packet.Ethernet.IpV4.Source + " ** Destination **" + packet.Ethernet.IpV4.Destination + " ** Payload **" + packet.IpV4.Payload);

            listBox1.Items.Add(ip.Source + " : " + tcp.SourcePort + " ->>>  " + ip.Destination + "  :  " + tcp.DestinationPort);
            listBox1.Items.Add("Payload: "+tcp.Payload);
        
        }
Posted
Updated 9-Sep-16 5:09am
v2
Comments
Nathan Minier 9-Sep-16 12:51pm    
You might be best served by looking at the source of those who already do it best:
https://code.wireshark.org/review/gitweb?p=wireshark.git;a=tree

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900