[Mono-dev] question on the Socket class
Dmitry Key
dmitryskey at hotmail.com
Tue Sep 5 04:54:16 EDT 2006
Hi, all!
In the C# code below I try to check during given timeout whether host is
available or not. This code is working fine under .Net with the following
output:
10035
Time: 5,040425 seconds
For the Mono 1.1.13/Debian
10035
Test Failed: 1,660835 seconds
Could somebody explain what I did wrong in my code?
Ignoring of the socket errors #10035 and #10035 is under suspicion, but I
have read in the documentation that these errors can be skipped.
using System;
using System.Net;
using System.Net.Sockets;
using System.Collections;
namespace ConsoleApplication1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
///
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
DateTime start = DateTime.Now;
try
{//host 1.1.1.1:8080, timeout 5 seconds
CheckConnection("1.1.1.1", 8080, 5);
}
catch(Exception)
{
Console.Out.WriteLine("Time: " +
DateTime.Now.Subtract(start).TotalSeconds.ToString() + " seconds");
return;
}
Console.Out.WriteLine("Test failed: " +
DateTime.Now.Subtract(start).TotalSeconds.ToString() + " seconds");
return;
}
public static void CheckConnection(string host, int port, int timeout)
{
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
bool isConnected = false;
IPHostEntry entries = Dns.GetHostByName(host);
foreach (IPAddress ipAddr in entries.AddressList)
{
sock.Blocking = false;
try
{
sock.Connect(new IPEndPoint(ipAddr, port));
}
catch (SocketException ex)
{
Console.Out.WriteLine(ex.ErrorCode.ToString());
// ignore errors:
// Resource temporarily unavailable.
// Operation now in progress.
if (ex.ErrorCode != 10035 && ex.ErrorCode != 10036)
throw;
}
ArrayList checkWrite = new ArrayList();
checkWrite.Add(sock);
ArrayList checkError = new ArrayList();
checkError.Add(sock);
Socket.Select(null, checkWrite, checkError, timeout * 1000000);
sock.Blocking = true;
sock.Close();
isConnected = checkWrite.Count > 0;
if (isConnected)
break;
}
if (!isConnected)
throw new Exception();
}
}
}
_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today it's FREE!
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/
More information about the Mono-devel-list
mailing list