vb.net 第十九节 Ping一个IP地址 IcmpCreateFile...

Posted VB.Net

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vb.net 第十九节 Ping一个IP地址 IcmpCreateFile...相关的知识,希望对你有一定的参考价值。

版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。
 

Imports System.Runtime.InteropServices

Public Class Form20

    'typedef struct icmp_echo_reply32 
    '  IPAddr                         Address;
    '  ULong                          Status;
    '  ULong                          RoundTripTime;
    '  UShort                         DataSize;
    '  UShort                         Reserved;
    '  VOID * POINTER_32              Data;
    '  struct ip_option_information32  Options;
    ' ICMP_ECHO_REPLY32, *PICMP_ECHO_REPLY32;

    'typedef struct ip_option_information 
    '  UCHAR  Ttl;
    '  UCHAR  Tos;
    '  UCHAR  Flags;
    '  UCHAR  OptionsSize;
    '  PUCHAR OptionsData;
    ' IP_OPTION_INFORMATION, *PIP_OPTION_INFORMATION;


    'Type ICMP_ECHO_REPLY
    '   address  As Long			'包含正回复的IP地址
    '   Status  As Long			'包含回复的状态(参看后面的常量部分)
    '   RoundTripTime  As Long	'往返时间RTT(毫秒)
    '   DataSize  As Integer		'回复数据大小(字节)
    '   Reserved  As Integer		'保留
    '   ptrData  As Long			'指向回复数据的指针
    '   Options  As IP_OPTION_INFORMATION	'回复选项
    '   Data  As String * 250	'
    'End Type
    'Type IP_OPTION_INFORMATION
    '   Ttl	As Byte				'生存时间
    '   Tos	As Byte				'服务类型
    '   Flags	As Byte				'IP头标志
    '   OptionsSize	As Byte		'选项数据的大小,字节
    '   OptionsData	As Long		'指向选项数据的指针
    'End Type

    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)>
    Public Structure ICMP_ECHO_REPLY
        Public address As Integer          '包含正回复的IP地址
        Public Status As Integer           '包含回复的状态(参看后面的常量部分)
        Public RoundTripTime As Integer    '往返时间RTT(毫秒)
        Public DataSize As Short      '回复数据大小(字节)
        Public Reserved As Short       '保留
        Public ptrData As IntPtr          '指向回复数据的指针
        Public Options As IP_OPTION_INFORMATION   '回复选项  
        <MarshalAs(UnmanagedType.ByValArray, SizeConst:=250)> Public Data() As Char
    End Structure


    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)>
    Public Structure IP_OPTION_INFORMATION
        Public Ttl As Byte              '生存时间
        Public Tos As Byte              '服务类型
        Public Flags As Byte                'IP头标志
        Public OptionsSize As Byte      '选项数据的大小,字节
        Public OptionsData As IntPtr       '指向选项数据的指针
    End Structure

    'DWORD IcmpSendEcho(
    '  _In_     HANDLE                 IcmpHandle,
    '  _In_     IPAddr                 DestinationAddress,
    '  _In_     LPVOID                 RequestData,
    '  _In_     WORD                   RequestSize,
    '  _In_opt_ PIP_OPTION_INFORMATION RequestOptions,
    '  _Out_    LPVOID                 ReplyBuffer,
    '  _In_     DWORD                  ReplySize,
    '  _In_     DWORD                  Timeout
    ');



    'Public Declare Function IcmpCreateFile Lib "iphlpapi.dll" () As Long
    'Public Declare Function IcmpSendEcho Lib "icmp.dll" (ByVal IcmpHandle As Long, ByVal DestinationAddress As Long, ByVal RequestData As String, ByVal RequestSize As Long, ByVal RequestOptions As Long, ReplyBuffer As ICMP_ECHO_REPLY, ByVal ReplySize As Long, ByVal Timeout As Long) As Long
    'Public Declare Function IcmpCloseHandle Lib "iphlpapi.dll" (ByVal IcmpHandle As Long) As Long
    'Public Declare Function inet_addr Lib "wsock32.dll" (ByVal cp As String) As Long


    Public Declare Function IcmpCreateFile Lib "iphlpapi.dll" () As IntPtr
    Public Declare Function IcmpSendEcho Lib "Iphlpapi.dll" (ByVal IcmpHandle As IntPtr,
                                                          ByVal DestinationAddress As Integer,
                                                          ByVal RequestData As String,
                                                          ByVal RequestSize As Integer,
                                                          ByRef RequestOptions As IP_OPTION_INFORMATION,
                                                          ByRef ReplyBuffer As ICMP_ECHO_REPLY,
                                                          ByVal ReplySize As Integer,
                                                          ByVal Timeout As Integer
                                                          ) As Integer

    Public Declare Function IcmpCloseHandle Lib "iphlpapi.dll" (ByVal IcmpHandle As IntPtr) As Integer
    Public Declare Function inet_addr Lib "wsock32.dll" (ByVal cp As String) As Integer

    Const ICMP_SUCCESS = 0                                      '成功
    Const ICMP_STATUS_BUFFER_TO_SMALL = 11001               '缓存太小
    Const ICMP_STATUS_DESTINATION_NET_UNREACH = 11002       '目的地网络不能到达
    Const ICMP_STATUS_DESTINATION_HOST_UNREACH = 11003  '目的地主机不能到达
    Const ICMP_STATUS_DESTINATION_PROTOCOL_UNREACH = 11004  '目的地协议不能到达
    Const ICMP_STATUS_DESTINATION_PORT_UNREACH = 11005      '目的地端口不能到达
    Const ICMP_STATUS_NO_RESOURCE = 11006                   '没有资源
    Const ICMP_STATUS_BAD_OPTION = 11007                    '错误选项
    Const ICMP_STATUS_HARDWARE_ERROR = 11008                '硬件错误
    Const ICMP_STATUS_LARGE_PACKET = 11009                      '信息包太大
    Const ICMP_STATUS_REQUEST_TIMED_OUT = 11010             '请求超时
    Const ICMP_STATUS_BAD_REQUEST = 11011                       '错误请求
    Const ICMP_STATUS_BAD_ROUTE = 11012                     '错误路由
    Const ICMP_STATUS_TTL_EXPIRED_TRANSIT = 11013               'TTL终止传输
    Const ICMP_STATUS_TTL_EXPIRED_REASSEMBLY = 11014        'TTL终止重新组装
    Const ICMP_STATUS_PARAMETER = 11015                     '参数有问题
    Const ICMP_STATUS_SOURCE_QUENCH = 11016                 '资源结束
    Const ICMP_STATUS_OPTION_TOO_BIG = 11017                '选项太大
    Const ICMP_STATUS_BAD_DESTINATION = 11018               '错误目的地
    Const ICMP_STATUS_NEGOTIATING_IPSEC = 11032             '谈判IPSEC
    Const ICMP_STATUS_GENERAL_FAILURE = 11050               '常规失败


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim IcmpHandle As IntPtr
        IcmpHandle = IcmpCreateFile()

        Dim ReplyBuffer As New ICMP_ECHO_REPLY
        '括号内为要Ping的目的地IP地址,这里是百度的一个ip
        Dim DAddr As Integer = inet_addr("14.215.177.39")
        Dim RData As String = "Hi,Hello!"
        Dim RequestSize As Short = RData.Length
        Dim IPOPTIONINFORMATION As IP_OPTION_INFORMATION
        IPOPTIONINFORMATION.Ttl = 255
        Dim ReplySize As Integer = Marshal.SizeOf(ReplyBuffer)
        Dim Timeout As Integer = 1000


        Dim result As Integer
        result = IcmpSendEcho(IcmpHandle, DAddr, RData, RequestSize, IPOPTIONINFORMATION, ReplyBuffer, ReplySize, Timeout)

        If result = 0 Then
            TextBox1.Text = "出错:" & Err.LastDllError
            Exit Sub
        End If


        TextBox1.Text = "状态:" & IcmpSta(ReplyBuffer.Status) & vbCrLf
        TextBox1.Text &= "IP地址:" & IntToIP(ReplyBuffer.address) & vbCrLf
        TextBox1.Text &= "回复数据大小:" & ReplyBuffer.DataSize & vbCrLf
        TextBox1.Text &= "往返时间(ms):" & ReplyBuffer.RoundTripTime & vbCrLf
        TextBox1.Text &= "回复数据:" & getReplayString(ReplyBuffer.Data, ReplyBuffer.DataSize) & vbCrLf
        TextBox1.Text &= "Ttl:" & ReplyBuffer.Options.Ttl

        result = IcmpCloseHandle(IcmpHandle)
    End Sub

    Public Function IcmpSta(icmpStatus As Long) As String
        Select Case icmpStatus
            Case ICMP_SUCCESS
                Return "成功"
            Case ICMP_STATUS_DESTINATION_HOST_UNREACH
                Return "目的地主机不能到达"
            Case Else
                Return "其它信息"
        End Select
    End Function

    Private Function IntToIP(ByVal intAddr As Integer) As String
        Dim bteIp() As Byte
        bteIp = BitConverter.GetBytes(intAddr)
        Dim ip As String = ""
        For i As Integer = 0 To bteIp.Length - 1
            ip &= bteIp(i).ToString & "."
        Next

        Return ip.Substring(0, ip.Length - 1)
    End Function

    Private Function getReplayString(ByVal Data() As Char, ByVal DataSize As Short) As String
        Return (New String(Data)).Substring(0, DataSize - 1)
    End Function


End Class

 

由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。

学习更多vb.net知识,请参看 vb.net 教程 目录

以上是关于vb.net 第十九节 Ping一个IP地址 IcmpCreateFile...的主要内容,如果未能解决你的问题,请参考以下文章

centos mysql 优化 第十九节课

centos mysql 实战 第十九节课

学习Linux第十九节课

EasyClick Html UI 第十九节 jQuery选择集转移

第十九节 epoll版的HTTP服务器

第十九节-opacity与visibility