G
Guest
Hello
I have some code (see below) that sends an ICMP echo request and that works
fine on Windows XP and 2003 but fails on Vista. The code sends the echo
request but times out trying to receive the echo reply. If I use MS network
monitor I can see the echo request being sent and the echo reply being
returned but my app doesn't receive it.
I've turned off the Vista firewall and have confirmed that using ping.exe to
the same address works. Also, if I use the .NET Ping class this also works.
So, there must be something wrong with my code although, as I said, this
works fine on the other OS's.
My code is below. A SocketException is thrown in the ReceiveFrom.
How do I fix this code so it works on Vista too?
Thanks
Gavin
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace IcmpTest {
class Program {
// ICMP Echo request
static readonly byte[] bytesToSend = {
0x08, 0x00, 0x28, 0x01, 0xde, 0xfc, 0x00, 0x01,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
};
static void Main(string[] args) {
try {
Socket socket = new Socket(AddressFamily.InterNetwork,
SocketType.Raw, ProtocolType.Icmp);
socket.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout, 5000);
socket.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.IpTimeToLive, 32);
socket.SendTo(bytesToSend, (EndPoint)new
IPEndPoint(IPAddress.Parse("64.26.22.64"), 0));
byte[] responseBytes = new byte[1024];
EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
int bytesReceivedCount = socket.ReceiveFrom(responseBytes,
ref remoteEndPoint);
Console.WriteLine(bytesReceivedCount);
}
catch (Exception ex) {
// ReceiveFrom throws a SocketException -
// A connection attempt failed because the connected party
did not properly respond
// after a period of time, or established connection failed
because connected host has failed to respond
Console.WriteLine(ex.ToString());
}
}
}
}
I have some code (see below) that sends an ICMP echo request and that works
fine on Windows XP and 2003 but fails on Vista. The code sends the echo
request but times out trying to receive the echo reply. If I use MS network
monitor I can see the echo request being sent and the echo reply being
returned but my app doesn't receive it.
I've turned off the Vista firewall and have confirmed that using ping.exe to
the same address works. Also, if I use the .NET Ping class this also works.
So, there must be something wrong with my code although, as I said, this
works fine on the other OS's.
My code is below. A SocketException is thrown in the ReceiveFrom.
How do I fix this code so it works on Vista too?
Thanks
Gavin
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace IcmpTest {
class Program {
// ICMP Echo request
static readonly byte[] bytesToSend = {
0x08, 0x00, 0x28, 0x01, 0xde, 0xfc, 0x00, 0x01,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
};
static void Main(string[] args) {
try {
Socket socket = new Socket(AddressFamily.InterNetwork,
SocketType.Raw, ProtocolType.Icmp);
socket.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout, 5000);
socket.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.IpTimeToLive, 32);
socket.SendTo(bytesToSend, (EndPoint)new
IPEndPoint(IPAddress.Parse("64.26.22.64"), 0));
byte[] responseBytes = new byte[1024];
EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
int bytesReceivedCount = socket.ReceiveFrom(responseBytes,
ref remoteEndPoint);
Console.WriteLine(bytesReceivedCount);
}
catch (Exception ex) {
// ReceiveFrom throws a SocketException -
// A connection attempt failed because the connected party
did not properly respond
// after a period of time, or established connection failed
because connected host has failed to respond
Console.WriteLine(ex.ToString());
}
}
}
}