internal int DiscoverTcpPort (int timeoutSeconds)
{
int SqlServerTcpPort;
Client.Blocking = false;
// send command to UDP 1434 (SQL Monitor) to get
// the TCP port to connect to the MS SQL server
ASCIIEncoding enc = new ASCIIEncoding ();
Byte[] rawrq = new Byte [instance.Length + 1];
rawrq[0] = 4;
enc.GetBytes (instance, 0, instance.Length, rawrq, 1);
Send (rawrq, rawrq.Length);
if (!Active)
return -1; // Error
bool result;
long timeout = timeoutSeconds * 1000000;
result = Client.Poll ((int)timeout, SelectMode.SelectRead);
if (result == false)
return -1; // Error
if (Client.Available <= 0)
return -1; // Error
#if NET_2_0
IPEndPoint endpoint = new IPEndPoint (Dns.GetHostEntry ("localhost").AddressList [0], 0);
#else
IPEndPoint endpoint = new IPEndPoint (Dns.GetHostByName ("localhost").AddressList [0], 0);
#endif
Byte [] rawrs;
rawrs = Receive (ref endpoint);
string rs = Encoding.ASCII.GetString (rawrs);
string[] rawtokens = rs.Split (';');
Hashtable data = new Hashtable ();
for (int i = 0; i < rawtokens.Length / 2 && i < 256; i++) {
data [rawtokens [i * 2]] = rawtokens [ i * 2 + 1];
}
if (!data.ContainsKey ("tcp")) {
string msg = "Mono does not support names pipes or shared memory "
+ "for connecting to SQL Server. Please enable the TCP/IP "
+ "protocol.";
throw new NotImplementedException (msg);
}
SqlServerTcpPort = int.Parse ((string) data ["tcp"]);
Close ();
return SqlServerTcpPort;
}
}