I have this simple program:
public partial class Program
{
private static EthernetBuiltIn ethernet = new EthernetBuiltIn();
void ProgramStarted()
{
if( !ethernet.IsOpen)
ethernet.Open();
if (ethernet.NetworkInterface.NetworkInterfaceType != NetworkInterfaceType.Ethernet)
{
NetworkInterfaceExtension.AssignNetworkingStackTo(ethernet);
}
ethernet.NetworkInterface.EnableStaticIP("192.168.1.234","255.255.255.0","192.168.1.1");
ethernet.NetworkInterface.EnableStaticDns(new[]{"212.24.128.8","212.24.132.132"});
ethernet.NetworkInterface.PhysicalAddress = NTP.MACAddressToBytes("4C-0B-6D-22-54-5B");
Ping("192.168.1.11");
// Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
Debug.Print("Program Started");
}
private static byte lastSequenceNr = 0;
private static byte[] pingCommand;
public static bool Ping(string address)
{
bool result = false;
byte[] receiveBuffer = new byte[540];
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, 0);
IPEndPoint responseEndPoint = new IPEndPoint(IPAddress.Any, 0);
EndPoint castResponseEndPoint = (EndPoint)responseEndPoint;
pingCommand = new byte[8];
pingCommand[0] = 8; // Type
pingCommand[1] = 0; // Subtype
pingCommand[2] = 0; // Checksum
pingCommand[3] = 0;
pingCommand[4] = 1; // Identifier
pingCommand[5] = 0;
pingCommand[6] = 0; // Sequence number
pingCommand[7] = 0;
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);
socket.Bind(localEndPoint);
socket.SetSocketOption(
SocketOptionLevel.IP,
SocketOptionName.IpTimeToLive,
128
);
socket.ReceiveTimeout = 10;
DateTime timeSend = DateTime.Now;
Debug.Print(timeSend.ToString());
pingCommand[6] = lastSequenceNr++;
SetChecksum(pingCommand);
socket.SendTo(pingCommand, pingCommand.Length, SocketFlags.None, new IPEndPoint(IPAddress.Parse(address), 0));
Debug.Print("sended");
try
{
int Brecieved = socket.ReceiveFrom(
receiveBuffer,
0,
receiveBuffer.Length,
SocketFlags.None,
ref castResponseEndPoint
);
if ((receiveBuffer[20] == 0) && (pingCommand[4] == receiveBuffer[24]) && (pingCommand[5] == receiveBuffer[25]) &&
(pingCommand[6] == receiveBuffer[26]) && (pingCommand[7] == receiveBuffer[27]))
{
Debug.Print("response received");
Debug.Print(DateTime.Now.Subtract(timeSend).ToString());
result = true;
}
}
catch (SocketException e)
{
if (e.ErrorCode != (int)SocketError.TimedOut)
{
Debug.Print("unexpected error: " + e.ErrorCode);
}
}
socket.Close();
return result;
}
private static void SetChecksum(byte[] tel)
{
tel[2] = 0;
tel[3] = 0;
uint cs = 0;
for (int i = 0; i < pingCommand.Length; i = i + 2)
cs += (UInt16)(pingCommand[0 + i] << 0 | pingCommand[1 + i] << 8);
cs = ~((cs & 0xffffu) + (cs >> 16));
tel[2] = (byte)cs;
tel[3] = (byte)(cs >> 8);
}
public static UInt16 ToUInt16(byte[] value, int index = 0) { return (UInt16)(value[0 + index] << 0 | value[1 + index] << 8); }
}
Ping method is from codeshare.
It gives this output:
sended
response received
00:00:00.0338452
Program Started
Now, if I change the private ip address in Ping method to some public, say 85.13.110.1, I get this:
sended
#### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (1) ####
#### Message:
#### Microsoft.SPOT.Net.SocketNative::recvfrom [IP: 0000] ####
#### System.Net.Sockets.Socket::ReceiveFrom [IP: 0026] ####
#### SpiderNet.Program::Ping [IP: 00ce] ####
#### SpiderNet.Program::ProgramStarted [IP: 0070] ####
#### SpiderNet.Program::Main [IP: 0015] ####
#### SocketException ErrorCode = 10060
#### SocketException ErrorCode = 10060
A first chance exception of type ‘System.Net.Sockets.SocketException’ occurred in Microsoft.SPOT.Net.dll
#### SocketException ErrorCode = 10060
#### SocketException ErrorCode = 10060
Program Started
I have tried also NTP client from codeshare, which works for me on Hydra with ENC28, it ends with timeout waiting for response from ntp server.
What can be wrong?