DbPortResolver.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // System.Data.ProviderBase.PDbPortResolver
  3. //
  4. // Authors:
  5. // Konstantin Triger <[email protected]>
  6. // Boris Kirzner <[email protected]>
  7. //
  8. // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using java.net;
  32. namespace System.Data.Common
  33. {
  34. public class DbPortResolver
  35. {
  36. public static int getMSSqlPort(String sqlName, String instanceName,int timeout)
  37. {
  38. int port = -1;
  39. try
  40. {
  41. DatagramSocket socket = new DatagramSocket();
  42. // send request
  43. sbyte[] buf = new sbyte[] {2};
  44. InetAddress address = InetAddress.getByName(sqlName);
  45. DatagramPacket packet = new DatagramPacket(buf, buf.Length, address, 1434);
  46. socket.send(packet);
  47. sbyte[] recbuf = new sbyte[1024];
  48. packet = new DatagramPacket(recbuf, recbuf.Length, packet.getAddress(), packet.getPort());
  49. // try to receive from socket while increasing timeouts in geometric progression
  50. int iterationTimeout = 1;
  51. int totalTimeout = 0;
  52. while (totalTimeout < timeout*1000)
  53. {
  54. socket.setSoTimeout(iterationTimeout);
  55. try
  56. {
  57. socket.receive(packet);
  58. break;
  59. }
  60. catch (SocketTimeoutException e)
  61. {
  62. totalTimeout += iterationTimeout;
  63. iterationTimeout *= 2;
  64. }
  65. }
  66. sbyte[] rcvdSbytes = packet.getData();
  67. char[] rcvdChars = new char[rcvdSbytes.Length];
  68. for(int i=0; i < rcvdSbytes.Length; i++)
  69. {
  70. rcvdChars[i] = (char)rcvdSbytes[i];
  71. }
  72. String received = new String(rcvdChars);
  73. java.util.StringTokenizer st = new java.util.StringTokenizer(received, ";");
  74. String prev = "";
  75. bool instanceReached = false;
  76. while (st.hasMoreTokens())
  77. {
  78. if (!instanceReached)
  79. {
  80. if (prev.Trim().Equals("InstanceName"))
  81. {
  82. if (String.Compare(instanceName,st.nextToken().Trim(),true) == 0)
  83. {
  84. instanceReached = true;
  85. }
  86. }
  87. }
  88. else
  89. {
  90. if (prev.Trim().Equals("tcp"))
  91. {
  92. port = java.lang.Integer.parseInt(st.nextToken().Trim());
  93. break;
  94. }
  95. }
  96. prev = st.nextToken();
  97. }
  98. socket.close();
  99. return port;
  100. }
  101. catch (java.lang.Exception e)
  102. {
  103. return port;
  104. }
  105. }
  106. }
  107. }