OK, that's excellent.
Thanks!
Main Topics
Browse All Topics
I have pasted the msdn PING sample code into an application that needs to know if a machine is still running on the network.
I have made no changes to the sample code. This line --
bread = setsockopt(sockRaw,SOL_SOC
sizeof(timeout));
with timeout having a value of 1000, it seems like the recvfrom should time out after a second.
This is the call to recvfrom, as in the sample code
bread = recvfrom(sockRaw,recvbuf,M
&fromlen);
if (bread == SOCKET_ERROR){
if (WSAGetLastError() == WSAETIMEDOUT) {
printf("timed out\n");
If I take the machine being pinged off the network, recvfrom never returns. The timeout doesn't seem to work.
What do I need to modify or include in the sample code from msdn to make it handle the machine PINGed being a valid workstation , but having been shut down or taken off the network. Assume I am not a Winsock programmer and don't have time to become one tonight.
thanks in advance
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Business Accounts
Answer for Membership
by: clarkaPosted on 2001-02-18 at 21:07:44ID: 5854797
To check to see if a pc is on your network you should us a udp ICMP_ECHO packet.
--- ---
--------- --------- ver), period); gServ02,.. .... ver); , __LINE__, _T("gethostbyname - Failed")); Server)); , __LINE__, _T("gethostbyaddr - Failed"));
---------- ---------- ------- ---------- ---------- ------- f(TRACE_IN T_FUNC, TRACE_LEVEL1,
t);
, __LINE__, _T("socket create - Failed"));
, __LINE__, _T("setsockopt - Failed"));
---- ----
MP_HEADER_ SIZE];
P_HEADER_S IZE + sizeof(DWORD)], ICMP_DATA, strlen(ICMP_DATA));
!= 0) , __LINE__, _T("sendto - Failed")); , __LINE__, _T("sendto - Failed (SentBytes != PacketSize)"));
f(TRACE_IN T_FUNC, TRACE_LEVEL1, e::Connect HeartBeatP ing(Intern etChksum Failed)"));
Here is a sample:
// EEPing.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <atlbase.h>
#include "WINSOCK.H"
#include <comdef.h> //add _bstr_t
#define WINSOCK_VERSION 0x0101 // program requires winsock version 1.1
#define NO_FLAGS 0 // ping var
#define ICMP_ECHO 8 // ping var
#define ICMP_ECHO_REPLY 0 // ping var
#define ICMP_HEADER_SIZE 8 // ping var
#define ICMP_DATA "My Data" // ping var
#define PING_TIME_OUT 4000 // ping var
struct icmp
{
BYTE icmp_type;
BYTE icmp_code;
WORD icmp_cksum;
WORD icmp_id;
WORD icmp_seq;
BYTE icmp_data[1];
};
struct icmp *pIcmpHeader;
WORD InternetChksum(LPWORD lpwIcmpData, WORD wDataLength);
int main(int argc, char* argv[])
{
WSADATA wsaData;
int nRes = 0;
int iPacketSize = 0;
int iSentBytes = 0;
int iReceivedBytes = 0;
int iHostAddrLength = 0;
int iError = 0;
int iOptValue = 0;
int period = 46;
unsigned int socketproto = 0;
PDWORD pdwTimeStamp = NULL;
DWORD dwReturnTime = 0;
DWORD dwRoundTrip = 0;
BYTE IcmpSendPacket[128];
BYTE IcmpRecvPacket[128];
SOCKET socketID = 0;
SOCKADDR_IN sockAddrLocal;
SOCKADDR_IN sockAddrHost;
IN_ADDR inaddr;
WCHAR* wdest = NULL;
_bstr_t m_PrimaryServer;
struct hostent* m_PrimaryHost = NULL; // host struct for ping
USES_CONVERSION;
// set this var to name or ip address.
// change this next var for the pcname or ip address you are trying to check on.
m_PrimaryServer = _T("PCName");
// setup protocol
socketproto = IPPROTO_ICMP;
//------------------------
// start up sockets
//------------------------
nRes = WSAStartup (WINSOCK_VERSION, &wsaData);
if(0 != nRes)
{
return (FALSE);
}
//------------------------
// get hostinfo for m_PrimaryServer
//------------------------
if(!m_PrimaryHost)
{
// check to see if we have an ip address..
wdest = wcsrchr(OLE2W(m_PrimarySer
if(NULL == wdest)
{
//lookup computer name ie OS2NetDiagServer,OS2NetDia
m_PrimaryHost = gethostbyname(m_PrimarySer
//CheckSocket(_T(__FILE__)
} else {
// lookup aaa.bbb.ccc.ddd (ip Address)
inaddr.S_un.S_addr = inet_addr(OLE2CA(m_Primary
m_PrimaryHost = gethostbyaddr((char *) &inaddr, 4, PF_INET);
//CheckSocket(_T(__FILE__)
}
}
//------------------------
// if hostinfo lookup for m_PrimaryServer failed exit
//------------------------
if(!m_PrimaryHost)
{
//_Module.m_pDCap->DCPrint
// _T("---- (gethostby[%s] lookup Failed)"), OLE2W(m_PrimaryServer));
return FALSE;
}
// setup local address
sockAddrLocal.sin_family = AF_INET;
sockAddrLocal.sin_addr = *((LPIN_ADDR) *m_PrimaryHost->h_addr_lis
//------------------
// Create Socket
//------------------
if(INVALID_SOCKET == (socketID = socket(PF_INET, SOCK_RAW, socketproto)))
{
//CheckSocket(_T(__FILE__)
return FALSE;
}
//---------------------
// Set Receive Time out
//---------------------
if (SOCKET_ERROR == setsockopt (socketID, SOL_SOCKET, SO_RCVTIMEO, (char*)&(iOptValue = PING_TIME_OUT), sizeof(int)) )
{
//CheckSocket(_T(__FILE__)
closesocket(socketID);
return FALSE;
}
//------------------------
// Setup ICMP Header and Data
//------------------------
pIcmpHeader = (struct icmp *) IcmpSendPacket;
pIcmpHeader->icmp_type = ICMP_ECHO;
pIcmpHeader->icmp_code = 0;
pIcmpHeader->icmp_id = 0;
pIcmpHeader->icmp_seq = 0;
pIcmpHeader->icmp_cksum = 0;
//put tick count in the optional data area
pdwTimeStamp = (PDWORD)&IcmpSendPacket[IC
*pdwTimeStamp = GetTickCount();
// copy echo data into packet
memcpy(&IcmpSendPacket[ICM
iPacketSize = ICMP_HEADER_SIZE + sizeof(DWORD) + strlen(ICMP_DATA);
// calc checksum and store it in the icmp_cksum
pIcmpHeader->icmp_cksum = InternetChksum((LPWORD) pIcmpHeader, iPacketSize);
if(pIcmpHeader->icmp_cksum
{
//----------------------
// Send Ping to Primary
//----------------------
iSentBytes = sendto (socketID, (LPSTR)IcmpSendPacket, iPacketSize, NO_FLAGS, (LPSOCKADDR) &sockAddrLocal, sizeof(sockAddrLocal));
if(SOCKET_ERROR == iSentBytes)
{
// sento error
//CheckSocket(_T(__FILE__)
closesocket(socketID);
return FALSE;
}
if(iSentBytes != iPacketSize)
{
// wrong number of bytes sent error
//CheckSocket(_T(__FILE__)
closesocket(socketID);
return FALSE;
}
iHostAddrLength = sizeof(sockAddrHost);
//------------------------
// Recv Ping from Primary
//------------------------
iReceivedBytes = recvfrom(socketID, (LPSTR) IcmpRecvPacket, sizeof(IcmpRecvPacket), NO_FLAGS, (LPSOCKADDR) &sockAddrHost, &iHostAddrLength);
if( SOCKET_ERROR == iReceivedBytes)
{
// Error recvfrom primary
// don't display datacap here because it will print out over and over
// until the primary comes back on line.
closesocket(socketID);
return FALSE;
}
} else {
// check sum error
//_Module.m_pDCap->DCPrint
// _T("---- CNCREventLoggingDcomServic
}
closesocket(socketID);
dwReturnTime = GetTickCount();
dwRoundTrip = dwReturnTime - *pdwTimeStamp;
// clean up winsock
WSACleanup();
return TRUE;
}
WORD InternetChksum(LPWORD lpwIcmpData, WORD wDataLength)
{
long lSum;
WORD wOddByte;
WORD wAnswer;
lSum = 0L;
while( wDataLength > 1 )
{
lSum += *lpwIcmpData++;
wDataLength -= 2;
}
// Handle the odd byte if necessary and make sure the top half is zero
if(wDataLength == 1)
{
wOddByte = 0;
*((LPBYTE) &wOddByte) = *(LPBYTE)lpwIcmpData; // one byte only
lSum += wOddByte;
}
// Add Back the carry outs from the 16 bits to the low 16 bits
lSum = (lSum >> 16) + (lSum & 0xffff); // add high-16 to low-16
lSum += (lSum >> 16); // add carry
wAnswer = (WORD)~lSum; // 1's complement , then
// truncate to 1 bits
return(wAnswer);
}