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
 

Skydiver

Well-known member
Joined
Apr 6, 2019
Messages
922
Location
Virginia Beach, VA
Programming Experience
10+
What antivirus are you using and have you disabled all its firewalls? Did you also disable the Windows Defender firewalls?
 

Skydiver

Well-known member
Joined
Apr 6, 2019
Messages
922
Location
Virginia Beach, VA
Programming Experience
10+
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?
 

naama

Member
Joined
Dec 17, 2019
Messages
9
Programming Experience
Beginner
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
 

Skydiver

Well-known member
Joined
Apr 6, 2019
Messages
922
Location
Virginia Beach, VA
Programming Experience
10+
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#
 

naama

Member
Joined
Dec 17, 2019
Messages
9
Programming Experience
Beginner
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
 

Skydiver

Well-known member
Joined
Apr 6, 2019
Messages
922
Location
Virginia Beach, VA
Programming Experience
10+

naama

Member
Joined
Dec 17, 2019
Messages
9
Programming Experience
Beginner
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
 

Skydiver

Well-known member
Joined
Apr 6, 2019
Messages
922
Location
Virginia Beach, VA
Programming Experience
10+
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.
 

naama

Member
Joined
Dec 17, 2019
Messages
9
Programming Experience
Beginner
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
 

Sheepings

Senior Programmer
Joined
Sep 5, 2018
Messages
844
Location
UK
Programming Experience
10+
Post your code at text, not images, and please post them with code tags [CODE=csharp]Your code here[/CODE]
 

naama

Member
Joined
Dec 17, 2019
Messages
9
Programming Experience
Beginner
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");
        }
 

Skydiver

Well-known member
Joined
Apr 6, 2019
Messages
922
Location
Virginia Beach, VA
Programming Experience
10+

naama

Member
Joined
Dec 17, 2019
Messages
9
Programming Experience
Beginner
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
 

Sheepings

Senior Programmer
Joined
Sep 5, 2018
Messages
844
Location
UK
Programming Experience
10+
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.
 

naama

Member
Joined
Dec 17, 2019
Messages
9
Programming Experience
Beginner
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.
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.
Ive tried using reacive instead of reacive from with the same reasult
 

Skydiver

Well-known member
Joined
Apr 6, 2019
Messages
922
Location
Virginia Beach, VA
Programming Experience
10+
It's curious that someone back in 2009 was experiencing the same problem trying to receive ICMP packets, and the problem seems to only have started with Vista based on some of the responses.
 

naama

Member
Joined
Dec 17, 2019
Messages
9
Programming Experience
Beginner
It's curious that someone back in 2009 was experiencing the same problem trying to receive ICMP packets, and the problem seems to only have started with Vista based on some of the responses.
I read that but unforthantly the solution they suggested isnt relavant here.
Adding the recive all they basicaly created a sniffer. I try to tunnel tcp packet over icmp. Meaning i have to send ping back from the server to the client.
It is not possible with reacive all if the server and client connect to the same router(the server sniff its on packet).
I also dont work with vista both my computer has windows 10
 

Skydiver

Well-known member
Joined
Apr 6, 2019
Messages
922
Location
Virginia Beach, VA
Programming Experience
10+
I was just trying to point out that the issue with ICMP receives started with Vista and has gone onwards. Prior to that, things worked in XP.
 
Top Bottom