socket.reacive

naama

Member
Joined
Dec 17, 2019
Messages
9
Programming Experience
Beginner
i've open a raw socket for icmp packet with Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);
then i bind it with IPAddress.Any
i've send request packet to my computer ip("192.168.1.29")
i know the packet was reacive due to wireshark put i can't get the packet with s.reacive using VS
the only way i can get the packet is if i'am binding the socket to "192.168.1.29" and then adding the option of
s.IOControl(IOControlCode.ReceiveAll, byTrue, null);
it's problematic since then my program is more or less acting as sniffer and i can't send back icmp packet using sendTo.
help whould be appreciate
 
What antivirus are you using and have you disabled all its firewalls? Did you also disable the Windows Defender firewalls?
 
i can't get the packet with s.reacive using VS
I assume meant using your program, rather than using Visual Studio. When you compile and run your code, it is your program that is running and trying to listen for packets, not Visual Studio. If you really did mean Visual Studio, are you writing a Visual Studio extension?
 
What antivirus are you using and have you disabled all its firewalls? Did you also disable the Windows Defender firewalls?

i did disabled firewalls and anti virus. i am also runing VS as administor.
before i did all of that i couldn't sniff the packets using s.IOControl(IOControlCode.ReceiveAll, byTrue, null);

I assume meant using your program, rather than using Visual Studio. When you compile and run your code, it is your program that is running and trying to listen for packets, not Visual Studio. If you really did mean Visual Studio, are you writing a Visual Studio extension?

yes you are right i meant my program runing using VS debugger
 
It's been a while, since I had to do low level socket programming but my recollection is that the network card(?) has to be set to promiscuous mode. Anyway, have you looked at this: A Network Sniffer in C#
 
It's been a while, since I had to do low level socket programming but my recollection is that the network card(?) has to be set to promiscuous mode. Anyway, have you looked at this: A Network Sniffer in C#
the problem is that i dont want to build a sniffer i want my program to reacive the packet that being send to my computer address("192.168.1.29") but from some reason the socket.reacive doesnt return unless I add the iocontrol
 
Oh! I didn't realize you didn't want to act as a sniffer. You should use RecieveFrom() or RecieveMessageFrom().
RecieveFrom doesn't work either. both function doesn't return. it seem like for some reason the program run on other ip even though it is the local ip of the computer.
i tried printing all the computer addressList and the ip (192.168.1.29) that the packet send to is one of them
 
So can you definitely say that the sample code that came with the RecieveFrome() doesn't work for you as well?

I think it's time for you to share your code. Right now all we can do is guess at what you are doing.
 
looking at the server i've tried to narrow it to the basic to see if it reacive the packet.
ללא שם.png

i also tried to bind the socket to IPAddress.Parse("192.168.1.29")
as you can see in wireshark the packet was recive.d
ללא שם.png
 
C#:
static void Main(string[] args)
        {
            Socket s=new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);
            s.ReceiveTimeout = 50000;
            s.Ttl = MyPing.TTL;
            //s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.DontFragment, 1); 
            s.Bind(new IPEndPoint(IPAddress.Any, 1550));
            byte[] buffer = new byte[MyPing.PACKET_MAX_SIZE];
            Console.WriteLine("ready to recive");
            IPEndPoint e = new IPEndPoint(IPAddress.Parse("192.168.1.25"), 1550);
            EndPoint e1 = (EndPoint)e;
            int size = s.ReceiveFrom(buffer, ref e1);
            Console.WriteLine("GOT MESSAGE");
        }
 
My ip address is 192.168.1.29
the ip address of the sender is 192.168.1.25 i've tried using the explict ip at the bind end point but it still doesn't work
 
Line 7 to 10 does not make sense.

A listener (which is essentially what you need) is meant to listen for traffic from ALL ip addresses, so why set a new endpoint with a custom IP on line 10?

I suggest looking at the Socket link above, for an idea of how one should be written. Take the docs provided and use the sample source. Ensure it works and then edit it to suit your needs. Also, If you're trying to use low level APIs to get around problems your network is causing, its your network you should be configuring, and not leaning on low level APIs as a fix.
 
Back
Top Bottom