我有SNMP类创建SNMP数据包,然后创建一个UDP套接字来发送数据包..但是当我使用这个类发送Get Packet时,我没有得到任何响应......我已经到了这一点因为我使用wireshark来检查某些流量是否来自udp端口161,但我在调试程序时没有获得任何流量....这是创建套接字的类的一部分:
Dim sock As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 4500)
' Dim ihe As IPHostEntry = Dns.GetHostEntry(host)
Dim iep As New IPEndPoint(IPAddress.Parse(host), 161)
Dim ep As EndPoint = DirectCast(iep, EndPoint)
sock.SendTo(packet, snmplen, SocketFlags.None, iep)
udp1.Send(packet, packet.Length, iep)
'Receive response from packet
Try
Dim recv As Integer = sock.ReceiveFrom(packet, ep)
Catch generatedExceptionName As SocketException
packet(0) = &HFF
End Try
Return packet
这是我用来发送SNMP get packet
的代码Imports System.Text
Class Form1
Dim commlength As Integer, miblength As Integer, datatype As Integer, datalength As Integer, datastart As Integer
Dim uptime As Integer = 0
Dim output As String
Dim conn As New SNMP()
Dim response As Byte() = New Byte(1023) {}
' Public Shared Sub Main(ByVal argv As String())
'End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
IPaddress.Text = "115.186.118.188"
Community.Text = "public"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListBox1.Items.Add("Device SNMP information:")
' Send sysName SNMP request
response = conn.[get]("get", IPaddress.Text, Community.Text, "1.3.6.1.2.1.1.5.0")
If response(0) = &HFF Then
ListBox1.Items.Add("No response from " & IPaddress.Text)
Return
End If
' If response, get the community name and MIB lengths
commlength = Convert.ToInt16(response(6))
miblength = Convert.ToInt16(response(23 + commlength))
' Extract the MIB data from the SNMP response
datatype = Convert.ToInt16(response(24 + commlength + miblength))
datalength = Convert.ToInt16(response(25 + commlength + miblength))
datastart = 26 + commlength + miblength
output = Encoding.ASCII.GetString(response, datastart, datalength)
ListBox1.Items.Add(" sysName - Datatype:" & datatype & "," & output)
End Class