I am back and yet again Windows Mobile / WinCE tutorial but this time soemthing interesting and on networking side. Lately i was developing Wireless PDA Update application which requires me to check if server which contains ROM image files is reachble before attempting to download files from the same. I thought that there is a WinCE API available to perform this task but to my surprise there is NO direct API however helper API is available. Then i have constructed an API which takes parameters and returns true if ping successful.
Here is the function .... If you require full application do let me know so that i can upload to any free server.
int Ping(PWCHAR wszHostname, int iWaitTime) {
WCHAR wszAdd[MAX_PATH] = L"\0";
in_addr addr;
HANDLE hFile;
IP_OPTION_INFORMATION ipInfo ;
IPAddr ipAddress ;
ICMP_ECHO_REPLY icmpEcho;
int iRet = -1;
char* pMBHost = (char*) malloc(MAX_PATH);
char* pMBAddr;
wcstombs(pMBHost, wszHostname, MAX_PATH ); // Convert Unicode to Multibyte
hostent* phe = gethostbyname(pMBHost);
if(phe) {
ipAddress = *(DWORD*)(phe->h_addr_list[0]);
}
if(ipAddress != INADDR_NONE) {
memcpy(&addr, phe->h_addr_list[0], sizeof(in_addr) );
pMBAddr = inet_ntoa(addr);
mbstowcs(wszAdd, pMBAddr, sizeof(CHAR)*strlen(pMBAddr));
// AfxMessageBox(wszAdd);
iRet = 0;
hFile = IcmpCreateFile();
// Set some reasonable default values
ipInfo.Ttl = 128 ;
ipInfo.Tos = 0;
ipInfo.Flags = 0;
ipInfo.OptionsSize = 0 ;
ipInfo.OptionsData = NULL ;
icmpEcho.Status = IP_SUCCESS;
IcmpSendEcho(hFile, ipAddress, NULL, 0,
(PIP_OPTION_INFORMATION)&ipInfo,
&icmpEcho, sizeof(ICMP_ECHO_REPLY),iWaitTime);
IcmpCloseHandle(hFile) ;
if ( icmpEcho.Status == IP_SUCCESS ) {
iRet = 1 ;
}
}
free(pMBHost);
return iRet;
}
Here is the function .... If you require full application do let me know so that i can upload to any free server.
int Ping(PWCHAR wszHostname, int iWaitTime) {
WCHAR wszAdd[MAX_PATH] = L"\0";
in_addr addr;
HANDLE hFile;
IP_OPTION_INFORMATION ipInfo ;
IPAddr ipAddress ;
ICMP_ECHO_REPLY icmpEcho;
int iRet = -1;
char* pMBHost = (char*) malloc(MAX_PATH);
char* pMBAddr;
wcstombs(pMBHost, wszHostname, MAX_PATH ); // Convert Unicode to Multibyte
hostent* phe = gethostbyname(pMBHost);
if(phe) {
ipAddress = *(DWORD*)(phe->h_addr_list[0]);
}
if(ipAddress != INADDR_NONE) {
memcpy(&addr, phe->h_addr_list[0], sizeof(in_addr) );
pMBAddr = inet_ntoa(addr);
mbstowcs(wszAdd, pMBAddr, sizeof(CHAR)*strlen(pMBAddr));
// AfxMessageBox(wszAdd);
iRet = 0;
hFile = IcmpCreateFile();
// Set some reasonable default values
ipInfo.Ttl = 128 ;
ipInfo.Tos = 0;
ipInfo.Flags = 0;
ipInfo.OptionsSize = 0 ;
ipInfo.OptionsData = NULL ;
icmpEcho.Status = IP_SUCCESS;
IcmpSendEcho(hFile, ipAddress, NULL, 0,
(PIP_OPTION_INFORMATION)&ipInfo,
&icmpEcho, sizeof(ICMP_ECHO_REPLY),iWaitTime);
IcmpCloseHandle(hFile) ;
if ( icmpEcho.Status == IP_SUCCESS ) {
iRet = 1 ;
}
}
free(pMBHost);
return iRet;
}
Comments