Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / .NET SDK / March 2005

Tip: Looking for answers? Try searching our database.

Help with Multi-Threaded ICMP Ping

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
KW - 28 Mar 2005 23:01 GMT
Group,

Trying to create multiple threads, each of which creates a socket and sends ICMP echo packets to specified hosts. Using .NET (C#) and asynch BeginSendTo. All works great UNLESS a machine is on the net that a) correctly resolves (gethostbyname) and b) for some reason doesn't respond to ICMP.  In this case, rather than timeout on a failed receive (in receive callback) the call returns data with an IPEndPoint of another machine in the list. Interestingly, the other machine's thread/socket also receive the data.

I guess my question is, can I reasonably expect an ICMP response to be received only by the socket that sent the echo request?

Thanks,

Kevin

Signature

Kevin

"Peter Huang" [MSFT] - 29 Mar 2005 04:37 GMT
Hi

How do you receive the reply data?
Because the ICMP protocol did not specified the port, the reply message is
reply to the machine(just IP no port), if we just call BeginReceive or
Receive, it may not tell one from another.

You may try to use the ReceiveFrom instead, also here is some another ping
code for your test.

A Simple Ping Tool(C++)
http://www.codeproject.com/internet/simplepingtool.asp

Network Stuff (easy socket v3)(C#)
http://www.codeproject.com/csharp/networkstuff.asp

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 - 29 Mar 2005 16:26 GMT
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.


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.