⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myping.cpp

📁 ping 不是很好要完善
💻 CPP
字号:
// MyPing.cpp: implementation of the CMyPing class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Ping.h"
#include "MyPing.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CMyPing::CMyPing()
{

}

CMyPing::~CMyPing()
{

}

//DEL void CMyPing::Ping(UINT nRetries, LPCSTR pstrHost, HWND hwnd)
//DEL {
//DEL 
//DEL }

//DEL int CMyPing::WaitForEchoReply(SCOKET s)
//DEL {
//DEL 
//DEL }

//DEL int CMyPing::SendEchoReply(SCOKET, LPSOCKADDR_IN)
//DEL {
//DEL 
//DEL }

DWORD CMyPing::RecxEchoReply(SOCKET, LPSOCKADDR_IN, u_char *)
{

}

int CMyPing::WaitForEchoReply(SOCKET s)
{

}

int CMyPing::SendEchoRequest(SOCKET, LPSOCKADDR_IN)
{

}

u_short CMyPing::in_cksum(u_short *addr, int len)
{

}

void CMyPing::WSAError(LPCSTR pstrFrom)
{

}



void CMyPing::Ping(UINT nRetries, LPCSTR pstrHost, HWND hWnd)
{
	SOCKET rawSocket;
	LPHOSTENT lpHost;
	UINT nLoop;
	int nRet;
	struct sockaddr_in saDest;
	struct sockaddr_in saSrc;
	DWORD dwTimeSent;
	DWORD dwElapsed;
	u_char cTTL;
	m_hWnd=hWnd;
	CString str;
	ASSERT(IsWindow(hWnd));
	rawSocket=socket(AF_INET,SOCK_RAW<IPPROTO_ICMP);
	if(rawSocket==SOCKET_ERROR)
	{
		WSAError("socket()");
		return;
	}
	lpHost=gethostbyname(pstrHost);
	if(lpHost==NULL)
	{
		str.Format("Host not found:%s",pstrHost);
		::PostMessage(m_hWnd,WM_MSG_STATUS,0,(LPARAM)AllocBuffer(str));
		::PostMessage(m_hWnd,WM_PING_END,0,0);
		return;
	}
	saDest.sin_addr.s_addr=*((u_long FAR*)(lpHost->h_addr));
	saDest.sin_family=AF_INET;
	saDest.sin_port=0;
	str.Format("Pinging %s[%s] with %d bytes of data:",
		pstrhost,
		inet_ntoa(saDest.sin_addr),
		REQ_DATASIZE);
	::PostMessage(m_hWnd,WM_MSG_STATUS,1,(LPARAM)AllocBuffer(str));
	for(nLoop=0;nLoop<nRetries;nLoop++0
	{
		SendEchoRequest(rawSocket,&saDest);
		nRet=WaitForEchoReply(rawSocket);
		if(nRet==SOCKET_ERROR)
		{
			WSAError("select()");
			break;
		}
		if(!nRet)
		{
			str.Format("Request Timed Out");
			::PostMessage(m_hWnd,WN_MSG_STATUS,3,(LPARAM)AllocBuffer(str));
		}
		else
		{
			dwTimeSent=RecvEchoReply(rawSocket,&saSrc,&cTTL);
			dwElapsed=GetTickCount()-dwTimeSent;
			str.Foemat("Reply[%d] from:%s:bytes=%d
				time=%ldms TTL=%d",
				nLoop+1,
				inet_ntoa(asSrc.sin_addr),
				REQ_DATASIZE,
				dwElapsed,
				cTTL);
			::PostMessage(m_hWnd,WM_MSG_STATUS,2,(LPARAM)AllocBuffer(str));
			Sleep(1000);
		}
	}
	::PostMessage(m_hWnd,WM_PING_END,0,1);
	nRet=closesocket(rawSocket);
	if(nRet==SOCKET_ERROR)
		WSAError("closesocket()");
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -