|
-
Aug 15th, 2002, 10:29 AM
#1
Thread Starter
Hyperactive Member
network computer connected
Hello there..
If given a name of a computer on a local network. Is there a simple way I can check to see if the computer is booted up and connected to the network?
Thanks,
-mcd
[vbcode]
'*****************************
MsgBox "MCD :: [email protected]", vbInformation + vbOKOnly, "User"
'*****************************
[/vbcode]
-
Aug 15th, 2002, 10:41 AM
#2
I suppose you could try pinging it. Here is a ping example
Code:
Option Strict On
Option Explicit On
Imports System.Net
Imports System.Net.Sockets
Public Enum ICMPType
EchoReply = 0
Unreachable = 3
Echo = 8
End Enum
Module Module1
Private Const portICMP As Integer = 7
Private Const bufferHeaderSize As Integer = 8
Private Const packageHeaderSize As Integer _
= 28
Sub Main()
Dim hostName As String
Console.Write( _
"Enter Remote Host Name or IP: ")
hostName = Console.ReadLine()
Echo(hostName)
Console.WriteLine("Press 'Enter' to exit.")
Console.ReadLine()
End Sub
Public Sub Echo(ByVal RemoteName As String)
'address/port of remote host
Dim RemoteHost As IPEndPoint
'id of this packet
Dim Identifier As Short = 0
'sequence number of this packet
Dim Sequence As Short = 0
'number of bytes of data to send
Dim DataSize As Byte = 32
'the socket we use to connect and
'send data through
Dim ICMPSocket As Socket
'the request buffer
Dim RequestBuffer() As Byte
'the reply buffer
Dim ReplyBuffer(255) As Byte
'the number of bytes received
Dim RecvSize As Integer = 0
Try
ICMPSocket = New _
Socket(AddressFamily.InterNetwork, _
SocketType.Raw, ProtocolType.Icmp)
ICMPSocket.Blocking = False
RemoteHost = _
GetRemoteEndpoint(RemoteName)
DataSize = Convert.ToByte(DataSize + _
bufferHeaderSize)
' If odd data size, we need to add
' one empty byte
If (DataSize Mod 2 = 1) Then
DataSize += Convert.ToByte(1)
End If
ReDim RequestBuffer(DataSize - 1)
' Set Type Field
RequestBuffer(0) = _
Convert.ToByte(ICMPType.Echo)
' Set ID Field
BitConverter.GetBytes( _
Identifier).CopyTo(RequestBuffer, 4)
' Set Sequence Field
BitConverter.GetBytes( _
Sequence).CopyTo(RequestBuffer, 6)
' load some data into buffer
Dim i As Integer
For i = 8 To DataSize - 1
RequestBuffer(i) = _
Convert.ToByte(i Mod 8)
Next i
' Set Checksum
CreateChecksum(RequestBuffer, _
DataSize, RequestBuffer(2), _
RequestBuffer(3))
ICMPSocket.SendTo(RequestBuffer, 0, _
DataSize, SocketFlags.None, _
RemoteHost)
RecvSize = ICMPSocket.ReceiveFrom( _
ReplyBuffer, SocketFlags.None, _
CType(RemoteHost, EndPoint))
If RecvSize > 0 Then
Select Case ReplyBuffer(20)
Case Convert.ToByte( _
ICMPType.EchoReply)
Console.WriteLine( _
"Remote host " + _
RemoteHost.Address. _
ToString _
+ " responded. " + _
(RecvSize - _
packageHeaderSize _
).ToString() + _
" bytes received.")
Case Convert.ToByte( _
ICMPType.Unreachable)
Console.WriteLine( _
"Remote endpoint " + _
"unreachable.")
Case Else
Console.WriteLine( _
"Received unexpected " _
+ "data...")
End Select
End If
Catch e As Exception
Console.WriteLine("Error: " + _
e.Message)
Finally
If Not ICMPSocket Is Nothing Then
ICMPSocket.Close()
End If
End Try
End Sub
Public Function GetRemoteEndpoint(ByVal _
RemoteAddress As String) As IPEndPoint
Return New IPEndPoint( _
Dns.Resolve( _
RemoteAddress).AddressList(0) _
, portICMP)
End Function
' ICMP requires a checksum that is the one's
' complement of the one's complement sum of
' all the 16-bit values in the data in the
' buffer.
' Use this procedure to load the Checksum
' field of the buffer.
' The Checksum Field (hi and lo bytes) must be
' zero before calling this procedure.
Private Sub CreateChecksum(ByRef data() As _
Byte, ByVal Size As Integer, ByRef HiByte _
As Byte, ByRef LoByte As Byte)
Dim i As Integer
Dim chk As Integer = 0
For i = 0 To Size - 1 Step 2
chk += Convert.ToInt32(data(i) * _
&H100 + data(i + 1))
Next
chk = Convert.ToInt32((chk And &HFFFF&) + _
Fix(chk / &H10000&))
chk += Convert.ToInt32(Fix(chk / &H10000&))
chk = Not (chk)
HiByte = Convert.ToByte((chk And &HFF00) _
/ &H100)
LoByte = Convert.ToByte(chk And &HFF)
End Sub
End Module
-
Aug 15th, 2002, 10:52 AM
#3
Thread Starter
Hyperactive Member
Cool.. this is a good start.. maybe a little more than i want to do.. But i think i can pull out what I need here. Would there be any other way to check considering the computer shows up in My Network Places if the pc is turned on?
Thanks,
-mcd
[vbcode]
'*****************************
MsgBox "MCD :: [email protected]", vbInformation + vbOKOnly, "User"
'*****************************
[/vbcode]
-
Aug 15th, 2002, 10:55 AM
#4
not that I know of. I dont think there is anything built into .NET to do that. May have to use API to accomplish it
-
Aug 15th, 2002, 11:16 AM
#5
Hyperactive Member
Originally posted by Cander
not that I know of. I dont think there is anything built into .NET to do that. May have to use API to accomplish it
Most Windows API functions have .NET counterparts. You may have to go searching for them though. You really should try to use 100% .NET code for your apps, since using other API's would slow your program down quite a bit.
-
Aug 15th, 2002, 11:24 AM
#6
Im well aware of that but .NET doesnt cover eveything.
-
Aug 15th, 2002, 11:24 AM
#7
Thread Starter
Hyperactive Member
Originally posted by Hu Flung Dung
Most Windows API functions have .NET counterparts. You may have to go searching for them though. You really should try to use 100% .NET code for your apps, since using other API's would slow your program down quite a bit.
I would tend to disagree with your statement that API's would slow down your program quite a bit... quite frankly, most VB.NET function calls are actually just wrapping API calls.. like a middleman.. so how would it slow your program down if you avoid the middleman.. am i incorrect here?
thanks for the ideas though,
-mcd
[vbcode]
'*****************************
MsgBox "MCD :: [email protected]", vbInformation + vbOKOnly, "User"
'*****************************
[/vbcode]
-
Aug 15th, 2002, 11:28 AM
#8
Because it also has to go through the COMInterop and dllimport stuff to work with .NET. But in most cases speed increase isnt really noticable.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|