예제 #1
0
        static string GetMSSqlPort(string instanceName, string dataSource, int timeout)
        {
            string port = String.Empty;

            try {
                DatagramSocket socket = new DatagramSocket();

                // send request
                sbyte[]        buf     = new sbyte[] { 2 };
                InetAddress    address = InetAddress.getByName(dataSource);
                DatagramPacket packet  = new DatagramPacket(buf, buf.Length, address, 1434);
                socket.send(packet);
                sbyte[] recbuf = new sbyte[1024];
                packet = new DatagramPacket(recbuf, recbuf.Length, packet.getAddress(), packet.getPort());

                // try to receive from socket while increasing timeouts in geometric progression
                int iterationTimeout = 1;
                int totalTimeout     = 0;
                for (;;)
                {
                    socket.setSoTimeout(iterationTimeout);
                    try {
                        socket.receive(packet);
                        break;
                    }
                    catch (SocketTimeoutException e) {
                        totalTimeout     += iterationTimeout;
                        iterationTimeout *= 2;
                        if (totalTimeout >= timeout * 1000)
                        {
                            throw new java.sql.SQLException(
                                      String.Format("Unable to retrieve the port number for {0} using UDP on port 1434. Please see your network administrator to solve this problem or add the port number of your SQL server instance to your connection string (i.e. port=1433).", dataSource)
                                      );
                        }
                    }
                }
                sbyte[] rcvdSbytes = packet.getData();
                char[]  rcvdChars  = new char[rcvdSbytes.Length];
                for (int i = 0; i < rcvdSbytes.Length; i++)
                {
                    rcvdChars[i] = (char)rcvdSbytes[i];
                }
                String received = new String(rcvdChars);

                java.util.StringTokenizer st = new java.util.StringTokenizer(received, ";");
                String prev            = "";
                bool   instanceReached = instanceName == null || instanceName.Length == 0;
                while (st.hasMoreTokens())
                {
                    if (!instanceReached)
                    {
                        if (prev.Trim().Equals("InstanceName"))
                        {
                            if (String.Compare(instanceName, st.nextToken().Trim(), true, CultureInfo.InvariantCulture) == 0)
                            {
                                instanceReached = true;
                            }
                        }
                    }
                    else
                    {
                        if (prev.Trim().Equals("tcp"))
                        {
                            port = st.nextToken().Trim();
                            //ensure we got a valid int
                            java.lang.Integer.parseInt(port);
                            break;
                        }
                    }
                    prev = st.nextToken();
                }
                socket.close();

                if (!instanceReached)
                {
                    throw new java.sql.SQLException(
                              String.Format("Specified SQL Server '{0}\\{1}' not found.", dataSource, instanceName)
                              );
                }
                return(port);
            }
            catch (java.sql.SQLException) {
                throw;
            }
            catch (Exception e) {
                throw new java.sql.SQLException(e.Message);
            }
        }