Unable to read incoming responses using raw sockets

I am trying to read a response from a website using code while listening to a raw socket, although so far I have only been able to read outgoing requests sent by my computer, and not incoming responses, which I am really addicted to. How can I read incoming answers?

EDIT: Using Wireshark, I came to find that the data I'm looking for is sent over TCP, I suppose.

Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Unspecified);
IPAddress localIP = Dns.GetHostByName(Dns.GetHostName()).AddressList[0];
listener.Bind(new IPEndPoint(localIP, 0));
byte[] invalue = new byte[4] { 1, 0, 0, 0 };
byte[] outvalue = new byte[4] { 1, 0, 0, 0 };
listener.IOControl(IOControlCode.ReceiveAll, invalue, outvalue);
while (true)
{
    byte[] buffer = new byte[1000000];
    int read = listener.Receive(buffer);
    if (read >= 20)
    {
        Console.WriteLine("Packet from {0} to {1}, protocol {2}, size {3}",
            new IPAddress((long)BitConverter.ToUInt32(buffer, 12)),
            new IPAddress((long)BitConverter.ToUInt32(buffer, 16)),
            buffer[9],
            buffer[2] << 8 | buffer[3]
        );
    }
}
+2
source share
3 answers

port 0 says it will listen on all ports, I think you need to install ProtocolType.Unspecified for ProtocolType.IP instead.

new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Raw); for ipv6 from what i read only on msdn. ProtocolType.IP is supported for ipv4 with raw sockets.

, ? Reciveall , . ip- u , :

Socket sck = new Socket( AddressFamily.InterNetwork  , SocketType.Raw  , ProtocolType.IP);
   sck.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, true);  

, :)

+4

++, . , . . .

VS2008 ++ Win7 x64.

. VS, , -, "" , .

, . Win7 , , , .

+1

, ! , , , , - "Tom Erik" ProtocolType.IP.

    static void Main(string[] args)
    {
        // Receive some arbitrary incoming IP traffic to an IPV4 address on your network adapter using the raw socket - requires admin privileges
        Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
        IPAddress ipAddress = Dns.GetHostEntry(Dns.GetHostName()).AddressList
            .Where((addr) => addr.AddressFamily == AddressFamily.InterNetwork)
            .Where((addr) => addr.GetAddressBytes()[0] != 127)
            .First();
        s.Bind(new IPEndPoint(ipAddress, 0));
        byte[] b = new byte[2000];
        EndPoint sender = new IPEndPoint(0, 0);
        int nr = s.ReceiveFrom(b, SocketFlags.None, ref sender);
    }
+1

All Articles