Unable to read incoming responses using raw sockets

て烟熏妆下的殇ゞ 提交于 2019-12-05 19:11:43

port 0 says he will listen on all ports, i think you need to set ProtocolType.Unspecified to ProtocolType.IP instead.

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

Also im thinking this is a connectionless socket right? Reciveall wouldnt really have an affect unless thats the case. if youre after the ip header u can get it by setting up the code like this:

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

hope this helps :)

Joe Mattioni

I had a similar problem in my C++ program using basically the same raw sockets setup. In my case I can see incoming packets in the debug build, but not in a release build. Outgoing packets are seen in both builds. I'm not sending any packets in this program.

I'm building with VS2008 native C++ under Win7 x64.

My issue was with the firewall. When the project was created in VS it apparently put an "Allow" entry in the firewall for network access by the project build, but only for the debug build of the program.

I had to add another entry for the release build and this then allowed incoming packets. The advance firewall settings under Win7 can also cause specific protocols to be blocked, so if you're getting only partial incoming messages check those settings for your program's entry.

Today I was wondering how to do the exact same thing! Here is my code which now seems to work, credit for getting this working is due to fellow answerer 'Tom Erik' for suggesting 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);
    }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!