473,413 Members | 1,767 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Get network status on mobile device

Hello,
I try to ping from my mobile device with a wireless connection to my
pc. I found the class clsping on the internet and it works perfect
with application from pc to pc but when i use it from mobile device
(win ce.net 4.2) to pc then it doesn't work anymore. I get on error
on the following statement:

Dim sckSocket As New System.Net.Sockets.Socket( _

Net.Sockets.AddressFamily.InterNetwork, _

Net.Sockets.SocketType.Raw, _

Net.Sockets.ProtocolType.Icmp)

ERROR: The support for the specified socket type does not exist
if I change icmp to tcp and raw to stream then I get an error on:

sckSocket.SetSocketOption(SocketOptionLevel.Socket ,
SocketOptionName.ReceiveTimeout, 1000)

ERROR: An unknown, invalid, or unsupported option or level was
specified in a getsockopt or setsockopt call
Can someone tell me how i can solve the problem or how I can retrieve
the network status on my mobile device

thnx

=====SOURCE CODE============================================== =================

CLASS CLSPING

Option Explicit On

Imports System
Imports System.Net
Imports System.Net.Sockets

'* Class clsPing
'*
'* Author : Paulo S. Silva Jr.
'* Date : September 2002
'* Objective : Ping a host and return basic informations
'*
'* Class Properties
'*
'* +------------+-------------+-------------------------------------------------+
'* | Name | Type | Description
|
'* +------------+-------------+-------------------------------------------------+
'* | DataSize | Integer | Size of the data to be send to the
host |
'* | Identifier | Integer | Identifier of ping packet
|
'* | Sequence | Integer | Sequence of the packet
|
'* | LocalHost | IPHostEntry | Info for Local Computer
|
'* | Host | Object | Host Entry for the remote computer
|
'* +------------+-------------+-------------------------------------------------+
'*
'* Class Methods
'*
'* +-------------------+----------------------------------------------------+
'* | Name | Description
|
'* +-------------------+----------------------------------------------------+
'* | PingHost | Pings the specified host
|
'* +-------------------+----------------------------------------------------+
'*
'* Parts of the code based on information from Visual Studio Magazine
'* more info : http://www.fawcette.com/vsm/2002_03/...qa/default.asp
'*
Public Class clsPing

Public Structure stcError
Dim Number As Integer
Dim Description As String
End Structure

Public Const PING_SUCCESS As Long = 0
Public Const PING_ERROR As Long = (-1)
Public Const PING_ERROR_BASE As Long = &H8000
Public Const PING_ERROR_HOST_NOT_FOUND As Long = PING_ERROR_BASE +
1
Public Const PING_ERROR_SOCKET_DIDNT_SEND As Long =
PING_ERROR_BASE + 2
Public Const PING_ERROR_HOST_NOT_RESPONDING As Long =
PING_ERROR_BASE + 3
Public Const PING_ERROR_TIME_OUT As Long = PING_ERROR_BASE + 4

Private Const ICMP_ECHO As Integer = 8
Private Const SOCKET_ERROR As Integer = -1

Private udtError As stcError

Private Const intPortICMP As Integer = 7
Private Const intBufferHeaderSize As Integer = 8
Private Const intPackageHeaderSize As Integer = 28

Private intDataSize As Byte
Private ipheLocalHost As System.Net.IPHostEntry
Private ipheHost As System.Net.IPHostEntry

Public Property DataSize() As Byte
Get
Return intDataSize
End Get
Set(ByVal Value As Byte)
intDataSize = Value
End Set
End Property

Public ReadOnly Property Identifier() As Byte
Get
Return 0
End Get
End Property

Public ReadOnly Property Sequence() As Byte
Get
Return 0
End Get
End Property

Public ReadOnly Property LocalHost() As System.Net.IPHostEntry
Get
Return ipheLocalHost
End Get
End Property

Public Property Host() As Object
Get
Return ipheHost
End Get
Set(ByVal Value As Object)
If (Value.GetType.ToString.ToLower = "system.string") Then
'*
'* Find the Host's IP address
'*
Try
ipheHost = System.Net.Dns.GetHostByName(Value)
Catch
ipheHost = Nothing

udtError.Number = PING_ERROR_HOST_NOT_FOUND
'MsgBox(ipheHost.HostName)
udtError.Description = "Host not found."
End Try
ElseIf (Value.GetType.ToString.ToLower =
"system.net.iphostentry") Then
ipheHost = (Value)
Else
ipheHost = Nothing
End If
End Set
End Property

'*
'* Class Constructor
'*
Public Sub New()
'*
'* Initializes the parameters
'*
intDataSize = 32
udtError = New stcError

'*
'* Get local IP and transform in EndPoint
'*
ipheLocalHost =
System.Net.Dns.GetHostByName(System.Net.Dns.GetHos tName())

'*
'* Defines Host
'*
ipheHost = Nothing
End Sub
Public Enum ICMPType As Byte
EchoReply = 0
DestinationUnreachable = 3
SourceQuench = 4
Redirect = 5
AlternateHostAddress = 6
EchoRequest = 8
RouterAdvertisement = 9
RouterSolicitation = 10
TimeExceeded = 11
ParameterProblem = 12
TimeStampRequest = 13
TimeStampReply = 14
InformationRequest = 15
InformationReply = 16
AddressMaskRequest = 17
AddressMaskReply = 18
IpixTraceRouter = 30
ConversionError = 31
MobileHostRedirect = 32
IPv6WhereAreYou = 33
IPv6HereIAm = 34
MobileRegistrationRequest = 35
MobileRegistrationReply = 36
DomainNameRequest = 37
DomainNameReply = 38
SkipAlgorithmDiscoveryProtocol = 39
IpSecSecurityFailures = 40
End Enum

'*
'* Function : PingHost
'* Author : Paulo dos Santos Silva Jr
'* Date : 05/09/2002
'* Objective : Pings a specified host
'*
'* Parameters : Host as String
'*
'* Returns : Response time in milliseconds
'* (-1) if error
'*
Public Function Ping() As Long

Dim intCount As Integer
Dim aReplyBuffer(255) As Byte

Dim intNBytes As Integer = 0

Dim intEnd As Integer
Dim intStart As Integer

Dim epFrom As System.Net.EndPoint
Dim epServer As System.Net.EndPoint
Dim ipepServer As System.Net.IPEndPoint


'*
'* Try send the packet
'*
Try
ipepServer = New
System.Net.IPEndPoint(ipheHost.AddressList(0), 0)
epServer = CType(ipepServer, System.Net.EndPoint)

epFrom = New
System.Net.IPEndPoint(ipheLocalHost.AddressList(0) , 0)

'*
'* Builds the packet to send
'*
DataSize = Convert.ToByte(DataSize + intBufferHeaderSize)

'*
'* The packet must by an even number, so if the DataSize
is and odd number adds one
'*
If (DataSize Mod 2 = 1) Then
DataSize += Convert.ToByte(1)
End If
Dim aRequestBuffer(DataSize - 1) As Byte

'*
'* --- ICMP Echo Header Format ---
'* (first 8 bytes of the data buffer)
'*
'* Buffer (0) ICMP Type Field
'* Buffer (1) ICMP Code Field
'* (must be 0 for Echo and Echo Reply)
'* Buffer (2) checksum hi
'* (must be 0 before checksum calc)
'* Buffer (3) checksum lo
'* (must be 0 before checksum calc)
'* Buffer (4) ID hi
'* Buffer (5) ID lo
'* Buffer (6) sequence hi
'* Buffer (7) sequence lo
'* Buffer (8)..(n) Ping Data
'*

'*
'* Set Type Field
'*
aRequestBuffer(0) = Convert.ToByte(ICMPType.EchoRequest)

'*
'* Set ID field
'*
BitConverter.GetBytes(Identifier).CopyTo(aRequestB uffer,
4)

'*
'* Set Sequence
'*
BitConverter.GetBytes(Sequence).CopyTo(aRequestBuf fer, 6)

'*
'* Load some data into buffer
'*
Dim i As Integer
For i = 8 To DataSize - 1
aRequestBuffer(i) = Convert.ToByte(i Mod 8)
Next i

'*
'* Calculate Checksum
'*
Call CreateChecksum(aRequestBuffer, DataSize,
aRequestBuffer(2), aRequestBuffer(3))
'*
'* Create the socket
'*
Dim sckSocket As New System.Net.Sockets.Socket( _

Net.Sockets.AddressFamily.InterNetwork, _

Net.Sockets.SocketType.Raw, _

Net.Sockets.ProtocolType.Icmp)

sckSocket.Blocking = True
'Dim linger As New LingerOption(True, 2)
sckSocket.SetSocketOption(SocketOptionLevel.Socket ,
SocketOptionName.ReceiveTimeout, 1000)
'sckSocket.SetSocketOption(SocketOptionLevel.Socke t,
SocketOptionName.Linger, linger)

'*
'* Records the Start Time
'*
intStart = System.Environment.TickCount
sckSocket.SendTo(aRequestBuffer, 0, DataSize,
SocketFlags.None, ipepServer)
intNBytes = sckSocket.ReceiveFrom(aReplyBuffer,
SocketFlags.None, epServer)
intEnd = System.Environment.TickCount

If (intNBytes > 0) Then
'*
'* Informs on GetLastError the state of the server
'*
udtError.Number = (aReplyBuffer(19) * &H100) +
aReplyBuffer(20)
Select Case aReplyBuffer(20)
Case 0 : udtError.Description = "Success"
Case 1 : udtError.Description = "Buffer too Small"
Case 2 : udtError.Description = "Destination
Unreahable"
Case 3 : udtError.Description = "Dest Host Not
Reachable"
Case 4 : udtError.Description = "Dest Protocol Not
Reachable"
Case 5 : udtError.Description = "Dest Port Not
Reachable"
Case 6 : udtError.Description = "No Resources
Available"
Case 7 : udtError.Description = "Bad Option"
Case 8 : udtError.Description = "Hardware Error"
Case 9 : udtError.Description = "Packet too Big"
Case 10 : udtError.Description = "Reqested Timed
Out"
Case 11 : udtError.Description = "Bad Request"
Case 12 : udtError.Description = "Bad Route"
Case 13 : udtError.Description = "TTL Exprd In
Transit"
Case 14 : udtError.Description = "TTL Exprd
Reassemb"
Case 15 : udtError.Description = "Parameter
Problem"
Case 16 : udtError.Description = "Source Quench"
Case 17 : udtError.Description = "Option too Big"
Case 18 : udtError.Description = "Bad Destination"
Case 19 : udtError.Description = "Address Deleted"
Case 20 : udtError.Description = "Spec MTU Change"
Case 21 : udtError.Description = "MTU Change"
Case 22 : udtError.Description = "Unload"
Case Else : udtError.Description = "General
Failure"
End Select
End If
Return (intEnd - intStart)
sckSocket.Close()
Catch oExcept As Exception
MsgBox(Err.Number & Err.Description)
Return -1
End Try
'Exit Function
'fout:
'Return -1
End Function

Public Function GetLastError() As stcError
Return udtError
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 Class
Nov 20 '05 #1
0 1799

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: ripken95 | last post by:
I connect to the internet through ADSL. I want to write the web page which can detect the connection status with javascript. This detection is like the signal detection in the mobile phone. If the...
1
by: John | last post by:
Hi We have an access db which records daily orders. We would like the orders to be downloaded to a windows mobile 2003 device via usb cradle. Drivers then take along the device and get...
3
by: Sara | last post by:
HI, I want to code a program to detect GSM mobile (any kind) which connected through serial port to computer and then be able to send SMS through this mobile phone to other mobile phones, could...
5
by: Al | last post by:
Hi, Is there any way to detected if a device is connected? How would I query to see the device is connected to network. My application is mobile and mostly on wireless connection but it could be...
1
by: Jeff | last post by:
Hey asp.net 2.0 I wonder if it's possible to have both standard asp.net and mobile asp.net in the same web application. When the web server gets a request it can detect if this requested...
2
by: Paulson | last post by:
bye Paulson Hi all I want to create a small application on my mobile(SamsungC200) As a first step I made a solution and added Project SamrtDeviceApplication I named it...
0
by: tthkbw | last post by:
I am writing an application and have the need to open files that exist on an ActiveSync connected Mobile Device (in this case, a PocketPC, could be a SmartPhone). When the mobile device is...
7
by: cj2 | last post by:
I saw a MS demo a few years back where they developed a web page and the software automatically rendered it for different devices. Is this what I'm looking for? http://www.asp.net/mobile/ ...
1
by: =?Utf-8?B?VGFtbXkgTmVqYWRpYW4=?= | last post by:
Could someone please let me know the C# codes to check the battery status of Windows mobile 5.0. The application should check the PC device battery level. I can use BatteryStatus however it runs...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.