473,835 Members | 1,794 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Get network status on mobile device

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.Sock ets.Socket( _

Net.Sockets.Add ressFamily.Inte rNetwork, _

Net.Sockets.Soc ketType.Raw, _

Net.Sockets.Pro tocolType.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.SetSo cketOption(Sock etOptionLevel.S ocket,
SocketOptionNam e.ReceiveTimeou t, 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


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


Option Explicit On

Imports System
Imports System.Net
Imports System.Net.Sock ets

'* 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

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 intBufferHeader Size As Integer = 8
Private Const intPackageHeade rSize As Integer = 28

Private intDataSize As Byte
Private ipheLocalHost As System.Net.IPHo stEntry
Private ipheHost As System.Net.IPHo stEntry

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

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

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

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

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

'MsgBox(ipheHos t.HostName)
udtError.Descri ption = "Host not found."
End Try
ElseIf (Value.GetType. ToString.ToLowe r =
"system.net.iph ostentry") Then
ipheHost = (Value)
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(S ystem.Net.Dns.G etHostName())

'* Defines Host
ipheHost = Nothing
End Sub
Public Enum ICMPType As Byte
EchoReply = 0
DestinationUnre achable = 3
SourceQuench = 4
Redirect = 5
AlternateHostAd dress = 6
EchoRequest = 8
RouterAdvertise ment = 9
RouterSolicitat ion = 10
TimeExceeded = 11
ParameterProble m = 12
TimeStampReques t = 13
TimeStampReply = 14
InformationRequ est = 15
InformationRepl y = 16
AddressMaskRequ est = 17
AddressMaskRepl y = 18
IpixTraceRouter = 30
ConversionError = 31
MobileHostRedir ect = 32
IPv6WhereAreYou = 33
IPv6HereIAm = 34
MobileRegistrat ionRequest = 35
MobileRegistrat ionReply = 36
DomainNameReque st = 37
DomainNameReply = 38
SkipAlgorithmDi scoveryProtocol = 39
IpSecSecurityFa ilures = 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(25 5) As Byte

Dim intNBytes As Integer = 0

Dim intEnd As Integer
Dim intStart As Integer

Dim epFrom As System.Net.EndP oint
Dim epServer As System.Net.EndP oint
Dim ipepServer As System.Net.IPEn dPoint

'* Try send the packet
ipepServer = New
System.Net.IPEn dPoint(ipheHost .AddressList(0) , 0)
epServer = CType(ipepServe r, System.Net.EndP oint)

epFrom = New
System.Net.IPEn dPoint(ipheLoca lHost.AddressLi st(0), 0)

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

'* 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.EchoRe quest)

'* Set ID field
BitConverter.Ge tBytes(Identifi er).CopyTo(aReq uestBuffer,

'* Set Sequence
BitConverter.Ge tBytes(Sequence ).CopyTo(aReque stBuffer, 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.Sock ets.Socket( _

Net.Sockets.Add ressFamily.Inte rNetwork, _

Net.Sockets.Soc ketType.Raw, _

Net.Sockets.Pro tocolType.Icmp)

sckSocket.Block ing = True
'Dim linger As New LingerOption(Tr ue, 2)
sckSocket.SetSo cketOption(Sock etOptionLevel.S ocket,
SocketOptionNam e.ReceiveTimeou t, 1000)
'sckSocket.SetS ocketOption(Soc ketOptionLevel. Socket,
SocketOptionNam e.Linger, linger)

'* Records the Start Time
intStart = System.Environm ent.TickCount
sckSocket.SendT o(aRequestBuffe r, 0, DataSize,
SocketFlags.Non e, ipepServer)
intNBytes = sckSocket.Recei veFrom(aReplyBu ffer,
SocketFlags.Non e, epServer)
intEnd = System.Environm ent.TickCount

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

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 1829

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

Similar topics

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 signal is weak, the status may have one squares or zero on the phone. How could I do if I want to detect the status of the network? Thx and Best Regards.
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 signatures from customers when items delivered. Once drivers are back, the information to be synched back to the access db as drivers may have amended the order detail. How does one go about synching to/from mobile device? Are there any code examples...
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 anyone help me and guide me, I wrote a program which could open COM port but still couldn't detect mobile phone and send SMS through it. I searched internet and I didn't find right answer to my question. please if anyone could guide me. the...
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 wired as well. Any Help would be appreciated. Regards Al
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 comes from a mobile or not???. Maybe there is setting in web.config specifying the default page if requested by a mobile phone???
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 SmartDeviceTest1
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 connected via ActiveSync an I open Windows Explorer, under My Computer, I see "Mobile Device" and can browse files on the PocketPC. However, in my C# code, when I open a FileDialog, "Mobile Device" does not appear under My Computer.
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/ Much of the info looks old. I have a MSDN account where can I download it or is it already in VS 2008 and I just don't know how to find it? I'm mainly interested in having simple pages render in Internet Explorer
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 only in Windows Form; it wont run with smart device form. Thanks -- Nejadian
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.