I did something like this in the past,
I needed it since I wanted to connect to several servers
at once, but didnt want to wait to long...

This class only checks if a server is listening on a certain port.

class Pinger
{
  bool m_bAvailable = false;
  IPEndPoint m_EndPoint;

  public Pinger( IPEndPoint endPoint, int timeout)
  {
    m_EndPoint = endPoint;
    AsyncCallback ConnectedMethodCallback = new AsyncCallback(
ConnectedMethod);

    Socket socket = new Socket( AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
    IAsyncResult ar = socket.BeginConnect( m_EndPoint,
ConnectedMethodCallback, socket);
    ar.AsyncWaitHandle.WaitOne( timeout, false);
  }

  public bool Available
  {
    get { return m_bAvailable; }
  }
  public IPEndPoint EndPoint
  {
    get { return m_EndPoint; }
  }

  void ConnectedMethod( IAsyncResult ar)
  {
    Socket socket = (Socket)ar.AsyncState;
    try
    {
      socket.EndConnect( ar);
      m_bAvailable = true;
    }
    catch( Exception)
    {
    }
    socket.Close();
  }
}

HTH
// Ryan


-------------------------------------
The information included in this message is personal and/or confidential and
intended exclusively for the addressees as stated. This message and/or the
accompanying documents may contain confidential information and should be
handled accordingly. If you are not the intended reader of this message, we
urgently request that you notify Centric immediately and that you delete
this e-mail and any copies of it from your system and destroy any printouts
immediately.
It is forbidden to distribute, reproduce, use or disclose the information in
this e-mail to third parties without obtaining prior permission from
Centric. We expressly point out that there are risks associated with the use
of e-mail. Centric and the companies within the group shall not accept any
liability whatsoever for damage resulting from the use of e-mail. Legally
binding obligations can only arise for Centric by means of a written
instrument, signed by an authorized representative of Centric.
-------------------------------------

===================================
This list is hosted by DevelopMentor�  http://www.develop.com
Some .NET courses you may be interested in:

NEW! Guerrilla ASP.NET, 17 May 2004, in Los Angeles
http://www.develop.com/courses/gaspdotnetls

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to