
Signature
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
Peter,
Thanks for the reply! The ping code is below. The key to the problem is that
this code is running in multiple threads, one for each machine. If a given
machine is up but has ICMP disabled, for example, the receive portion simply
grabs another machine/thread/socket's ICMP response. However, the other
socket also receives the response. My question is, is this expected
behavior? I can work around it by examining the ref endpoint to make sure
that the response is from the machine associated with the thread, but it's
important that I understand that this is not a defect in the framework, but
rather a side-effect of the nature of the ICMP protocol. Thanks,
Kevin
public void ping(int intTimeout)
{
mPingStart = Environment.TickCount;
mSocket.BeginSendTo(mPacket.serialize(), 0, mPacket.Size,
SocketFlags.None, mRemoteMachine, new AsyncCallback(SendCallback), null);
//wait until signaled or the timeout period expires.
mPaused.WaitOne(intTimeout, false);
//did the call timeout?
if (mPingTime == -1) //Yes
mExceptionMessage = "Host Unreachable";
}
private void SendCallback(IAsyncResult ar)
{
int bytesSent = 0;
try
{
bytesSent = mSocket.EndSendTo(ar);
//were any bytes sent?
if (bytesSent == SOCKET_ERROR) //No
mExceptionMessage = "Host Unreachable";
else //Yes, bytes were sent
mSocket.BeginReceiveFrom(mRecv_Buffer, 0, RECV_SIZE,
SocketFlags.None, ref mLocalMachine, new AsyncCallback(ReceiveCallback),
null);
}
catch (Exception e)
{
mExceptionMessage = e.Message;
}
}
private void ReceiveCallback(IAsyncResult ar)
{
int bytesReceived = 0;
try
{
bytesReceived = mSocket.EndReceiveFrom(ar, ref mLocalMachine);
//were any bytes received?
if (bytesReceived > 0) //Yes
mPingTime = Environment.TickCount - mPingStart;
}
catch (Exception e)
{
mExceptionMessage = e.Message;
}
}

Signature
Kevin
> Hi
>
[quoted text clipped - 20 lines]
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
"Peter Huang" [MSFT] - 30 Mar 2005 07:22 GMT
Hi
I think this is the nature of the ICMP protocol, as I said before, the ICMP
message did not specified the port, all ICMP message changed is based on
the Machine(IP address) without the port. So the recv program did not have
idea to tell one from other, because to recv machine, it just know that one
machine return a ICMP message, but he has no idea about about which program
will have interested in it.
So your approach is OK for this scenario, we need to check the returned
IPAddress in the receivefrom method to check if that is the machine we are
interested in.
Best regards,
Peter Huang
Microsoft Online Partner Support

Signature
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
KW - 30 Mar 2005 15:41 GMT
Thanks, Peter, I appreciate your help.
Kevin

Signature
Kevin
> Hi
>
[quoted text clipped - 20 lines]
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
"Peter Huang" [MSFT] - 31 Mar 2005 02:41 GMT
Hi
You are welcome!
Best regards,
Peter Huang
Microsoft Online Partner Support

Signature
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.