DbPortResolver.cs 3.2 KB

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