Ping сервера из своего приложения - Visual Basic .NET
Формулировка задачи:
Приветствую. Помогите пожалуйста кто может. Делаю программулю на ней уже есть список консольных команд , узнать ip сайта , добавить сайт в hosts файл (делаю эту программу для того чтобы упростить жизнь так как занимаюсь ремонтом ПК и часто бывает что приходиться сталкиваться с консолью win).
Вот вид программы:
Вопрос: Как добавить на форму консоль? Или как при нажатии кнопки в label1 появлялась информация как при помощи команды в cmd -
ping (адрес или ip) .
Пишу в Visual Studio 2013 - VB.
Заранее спасибо.
Решение задачи: «Ping сервера из своего приложения»
textual
Листинг программы
Imports System.Net
Imports System.Net.NetworkInformation
Imports System.Threading
Imports System.Threading.Tasks
Class Pinger
Private _numRequests As Integer
Private _timeout As Integer
Private _pingTask As Task
Private _scheduler As TaskScheduler
Private _hostNameOrAddress As String
Public Event ReplyReceived As EventHandler(Of PingReplyEventArgs)
Public Sub New(numRequests As Integer)
Me.New(numRequests, 4000)
End Sub
Public Sub New(numRequests As Integer, timeout As Integer)
If numRequests < 1 Then Throw New ArgumentOutOfRangeException("numRequests", "Attempts value must be greater than zero")
_numRequests = numRequests
_timeout = timeout
End Sub
Public Sub BeginSend(hostNameOrAddress As String)
If _pingTask IsNot Nothing AndAlso Not _pingTask.IsCompleted Then
Throw New InvalidOperationException("Ping is already in progress")
End If
_hostNameOrAddress = hostNameOrAddress
_scheduler = TaskScheduler.FromCurrentSynchronizationContext()
_pingTask = Task.Factory.StartNew(AddressOf DoPing, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default)
End Sub
Private Sub DoPing()
Dim pingData() As Byte
ReDim pingData(31)
For I = 0 To pingData.Length - 1
pingData(I) = CByte(97 + I Mod 30)
Next
Using ping As New Ping()
For I = 0 To _numRequests - 1
If I > 0 Then Thread.Sleep(900)
Dim reply As PingReply = Nothing
Try
reply = ping.Send(_hostNameOrAddress, _timeout, pingData)
Catch ex As PingException
End Try
Task.Factory.StartNew( _
Sub(state) RaiseEvent ReplyReceived(Me, New PingReplyEventArgs(DirectCast(state, PingReply))), _
reply, _
CancellationToken.None, TaskCreationOptions.None, _
_scheduler _
).Wait()
Next
End Using
End Sub
End Class
Public Class PingReplyEventArgs
Inherits EventArgs
Private _status As IPStatus
Private _address As IPAddress
Private _roundtripMilliseconds As Long
Private _ttl As Integer
Public ReadOnly Property IsSuccess As Boolean
Get
Return _status = IPStatus.Success
End Get
End Property
Public ReadOnly Property Status As IPStatus
Get
Return _status
End Get
End Property
Public ReadOnly Property Address As IPAddress
Get
Return _address
End Get
End Property
Public ReadOnly Property RoundtripMilliseconds As Long
Get
Return _roundtripMilliseconds
End Get
End Property
Public ReadOnly Property Ttl As Integer
Get
Return _ttl
End Get
End Property
Public Sub New(reply As PingReply)
If reply IsNot Nothing Then
_status = reply.Status
_address = reply.Address
_roundtripMilliseconds = reply.RoundtripTime
_ttl = reply.Options.Ttl
Else
_status = IPStatus.Unknown
_address = IPAddress.None
_roundtripMilliseconds = -1
_ttl = -1
End If
End Sub
End Class