vb.net enumerate all SQL 2000 servers in the network

Source: Internet
Author: User
Tags integer connect tostring client
Server | network

For SQL Server2000, open the SQL Server client ready to connect, and when the list of servers is pulled out, all SQL Server servers are listed for the entire LAN.


vb.net enumerate all SQL 2000 servers in the network

For SQL Server2000, open the SQL Server client ready to connect, and when the list of servers is pulled out, all SQL Server servers are listed for the entire LAN. So I found out that this UDP packet was broadcast from 1434 Port (192.168.0.255) on my own Machine (192.168.0.1), and then the SQL Server server in the entire LAN started responding to this UDP packet, My client can get all of the server information.

This is the process by which the client connects: When the client connects to the server, the application requests to connect to the remote computer, and Dbnetlib.dll opens the connection to UDP port 1434 on the computer network name specified in the connection. All computers running SQL Server 2000 are listening on this port. When a client Dbnetlib.dll connects to the port, the server returns a packet of all instances running on the server. For each instance, the packet reports the server net-library and network addresses that the instance is listening to. After the Dbnetlib.dll on the application computer receives the packet, select the Net-library that is enabled on both the application computer and the SQL Server instance, and then connect to the addresses listed in the Net-library for this packet.

Transport specific UDP packets over 1434 ports, and then the server starts responding, all in plaintext, and we can easily detect the 1434 port of an IP address and get information about the SQL Server running on that IP address. This information includes: host name, instance name, version, pipe name, and port used. This port is Microsoft's own use, and not like the default 1433 port can be changed, 1434 is not changed, oh, then we for security, to change this 1433 port can play what role?

We can capture these datagrams and find that 1434-port data is very simple and the client simply sends 21 bytes out. But many times, it is found that 03 is sometimes sent. So I use the following program one test, send other data. But in the end only 02, 03, 04 have responded. It seems that these three bytes are used for SQL Server probes. And you can send 02 00 00, you can also send 02 00 00 00 00, etc. can get SQL Server response, but send 02 03 is not OK.

Detailed code:

Imports System.Net

Imports System.Net.Sockets

Imports System.Net.Sockets.Socket

Imports System.Collections.Specialized

Public Class Getservers




' Use UDP protocol

Private M_client as Sockets.udpclient




' broadcast address, representing the entire network

Private strhost as String = "255.255.255.255"




' Broadcast port

Private Intport as Integer = 1434




' To send the data, send this data all SQL Server will respond to the

Private B () as Byte = New Byte () {&H2}




' Cause there's a dead loop in the send, and here's a timer to handle the timeout.

Private WithEvents T as New Timers.timer





' Send data out

Function Send () as ArrayList

M_client = New UdpClient




' Used to store the obtained SQL list

Dim atemp as New ArrayList




' Send the target, broadcast the address

Dim Host as New IPEndPoint (Ipaddress.parse (me.strhost), Me.intport)

Try

' Send data here

M_client.send (B, 1, host)




' Read now, because the number of SQL Servers in the network is large, the packet returns one by one

' So here's the loop.

While True




' Open timer

T.interval = 1000

t.enabled = True




' Array of bytes received

Dim BR as Byte () = m_client.receive (host)

Dim A as System.Text.Encoding = System.Text.Encoding.Default




' Used to store the current string

Dim S as String = ""




' Starting with number 4th, the first three characters are nonprinting characters

S = A.getstring (BR, 3, Br. Length-3)

Console.WriteLine (S)




' Here resolves the acquired text

Atemp. ADD (New SQL Server (S))




' Off

t.enabled = False

End While

Catch ex as Exception

Console.WriteLine (ex. Message)

Finally

M_client.close ()

End Try

Return atemp

' Console.WriteLine ("Check it out.") ")

End Function




' Force end network wait after timeout.

Private Sub t_elapsed (ByVal sender as Object, ByVal e as System.Timers.ElapsedEventArgs) Handles t.elapsed

' Console.WriteLine ("Off"). ")

M_client.close ()

T.dispose ()

End Sub

End Class





Public Class SQL Server

' SQL Server parsing class

'

' Because the returned string resembles Name1;value1;name2;value2;name3;value3; ....

' and not fixed length

' So here you return with NameValueCollection.

Private Nv as NameValueCollection




Sub New (ByVal Str as String)

Nv = New NameValueCollection

Dim A as String () = Str.split (";")

For i as Integer = 0 to A.length-1 Step 2

Dim T as Integer = i

Nv (A (t)) = a (T + 1)

Next

End Sub




' Because to add to the ComboBox, this overloads the ToString to display the text

Overrides Function ToString () as String

Return Nv ("ServerName")

End Function




End Class

Calling code


Calling code

Sub Getsqlservers ()

Me.cursor = Cursors.waitcursor

Dim A as ArrayList

Dim g as New getservers

A = G.send ()

For each C as SQL Server in a

ME.CB_SERVER.ITEMS.ADD (c)

Next

Me.cursor = Cursors.Default

End Sub


Sub Getsqlservers ()

Me.cursor = Cursors.waitcursor

Dim A as ArrayList

Dim g as New getservers

A = G.send ()

For each C as SQL Server in a

ME.CB_SERVER.ITEMS.ADD (c)

Next

Me.cursor = Cursors.Default

End Sub




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.