Windows Mobile ping command implementation

Source: Internet
Author: User

Recently, when implementing the MSN direct project, we need to use the VPN + ADSL solution to connect the Sigma server to the launch device on the TV tower. In order to test whether the network works normally, the two sides need to cooperate and ping the command to determine whether the network is connected. As TV Tower only allows debugging on Tuesday afternoon, debugging time is limited, and manpower is limited, in order to save time, so I came up with the idea of running a Windows Mobile ping command, in this way, you can directly test whether the network is connected through a mobile phone.

Windows Mobile does not have a ready-made Ping available, so you need to do it yourself. The most direct idea is to use socket to implement the ICMP protocol (for the ping command part), for the desktop version. the socket in the. NET Framework can send and receive ICMP packets as long as it is defined as follows.

Socket socket = new socket (addressfamily. InterNetwork, sockettype. Raw, protocoltype. ICMP );

However, unfortunately, the. NET Compact framework does not support the protocoltype. ICMP parameter, so it cannot be implemented using socket. Fortunately, Windows Mobile also provides icmp api interfaces, which can be used to implement the ping command function.CodeAs follows:

 

Public   Class SW

{

P/invoke # Region P/invoke

[Dllimport ( " \ Windows \ iphlpapi. dll " , Setlasterror =   True )]

Static   Extern Intptr icmpcreatefile ();

[Dllimport ( " \ Windows \ iphlpapi. dll " , Setlasterror =   True )]

Static   Extern   Bool Icmpclosehandle (intptr handle );

[Dllimport ( " \ Windows \ iphlpapi. dll " , Setlasterror =   True )]

Static   Extern Int32 icmpsendecho (intptr icmphandle, int32 destinationaddress, string requestdata, int16 requestsize, Ref Ip_option_information requestoptions, Ref Icmp_echo_reply replybuffer, int32 replysize, int32 timeout );

[Structlayout (layoutkind. Sequential)]

Public   Struct Ip_option_information

{

Public   Byte TTL;

Public   Byte ToS;

Public   Byte Flags;

Public   Byte Optionssize;

Public Intptr optionsdata;

}



[Structlayout (layoutkind. Sequential, charset = Charset. Auto)]

Private   Struct Icmp_echo_reply

{

Public   Int Address;

Public   Int Status;

Public   Int Roundtriptime;

Public Int16 datasize;

Public Int16 reserved;

Public Intptr dataptr;

[Financialas (unmanagedtype. byvaltstr, sizeconst =   250 )]

Public String data;

Public Ip_option_information options;

}

# Endregion

Public   Static   Int Ping ( String IP, Int Timeout, Out   String Info)

{

Ipendpoint host =   New Ipendpoint (DNS. gethostentry (IP). Addresslist [ 0 ], 0 );

If (Host =   Null   | Environment. osversion. Platform ! = Platformid. wince)

{

Info= "IP error!";

Return -1;

}

Int Result =   - 1 ;

Intptr icmphandle;

Int32 IIP;

String sdata;

Ip_option_information oicmpoptions =   New Ip_option_information ();

Icmp_echo_reply icmpreply =   New Icmp_echo_reply ();

Int32 ireplies;

Icmphandle = Icmpcreatefile ();

IIP = Bitconverter. toint32 (host. Address. getaddressbytes (), 0 );

Sdata =   " Abcdefghijklmnopqrstuvwxyz012345 " ;

Oicmpoptions. TTL =   255 ;

Ireplies = Icmpsendecho (icmphandle, IIP, sdata, (int16) sdata. length, Ref Oicmpoptions, Ref Icmpreply, Marshal. sizeof (icmpreply), timeout );

If (Icmpreply. Status =   0 )

{

Result = Icmpreply. roundtriptime;

Info =   " Ping bytes = "   + Sdata. length. tostring () +   " Time = "   + Result. tostring () +   " Ms ttl = "   + Oicmpoptions. TTL. tostring ();

}

Else

{< br>
info = " Ping " + ip + " timed out. " ;

}

Icmpclosehandle (icmphandle );

Return Result;

}

}

 

FinalProgramThe running interface is as follows:

It should be noted that the GPRS connection must be cmnet (For information on how to establish a GPRS connection, if you are interested, refer to what I wrote earlierArticle: Connect your smartphone to your home computer (wm6 GPRS). Otherwise, you cannot ping the Internet IP address. In addition, we found that the IP address of Windows Mobile Phone is 10. x. x. x. We know that the IP addresses starting with 10 are Class A addresses. For more information, we can see that 10.0.0.0 to 10.20.255.255 are private addresses (the so-called private addresses are not used on the Internet, addresses used in the local area network ). Each class A address can theoretically connect 16777214 hosts, which is about 6 million, which is the maximum number of mobile phones using GPRS at the same time. Because the IP address assigned by the Windows Mobile phone is not a public IP address, we are sorry that we cannot ping the Windows Mobile phone on the PC, so we cannot test whether the ping command on the PC is normal. [Ye fan Studio]

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/yefanqiu/archive/2009/09/21/4578180.aspx

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.