[Mono-list] Asynchronous socket methods in mono.

Gregg Edghill Gregg Edghill <gregg.edghill@gmail.com>
Thu, 21 Apr 2005 16:58:41 -0400


Hey all,

  I am doing some network programming using asynchronous socket.  I am
trying to connect async to a list of endpoint however,  I can only
connect to three from the list..  The program just sits there even
those I have a wait handle that manages the timeout.  A sample is
provided below.

I was just wondering if async network programming is fully supported.
Gregg.

TimeSpan wait =3D TimeSpan.FromMilliseconds(7000);

foreach(IPEndPoint ep in gi.EndPointList)
{
=09try
=09{
=09=09// Create a socket that will try to connect to the remote end point.
=09=09socket =3D new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);

=09=09// Try to connect to the remote end point.
=09=09IAsyncResult ar =3D socket.BeginConnect(ep, null, null);
=09=09if(ar.IsCompleted =3D=3D false)
=09=09{
=09=09=09// If the asynchronous receive operation has not completed, wait
for the operation to complete.
=09=09=09Console.WriteLine("Download session {0}, waiting to connect to
remote end point {1} ...", id, ep);

=09=09=09ar.AsyncWaitHandle.WaitOne(wait, false);

=09=09=09if(ar.IsCompleted =3D=3D false)
=09=09=09{
=09=09=09=09// If the receive operation is not complete and the timeout has
expired, throw a TimeoutException
=09=09=09=09throw new Exception(
=09=09=09=09=09=09=09=09String.Format("Download session {0}, connect timeou=
t.", id));
=09=09=09}
=09=09}
=09=09// End the receive operation.
=09=09socket.EndConnect(ar);

=09=09// We are not connected, try to authenticate the remote connection.
=09=09ThreadPool.QueueUserWorkItem(new
WaitCallback(ReadAndVerifyHandshake), socket);
=09}
=09catch(SocketException e)
=09{
=09=09Console.WriteLine(e.Message);
=09=09socket.Shutdown(SocketShutdown.Both);
=09=09socket.Close();
=09}
}