// ping.h#pragma once#include "IpMonitor.h"#include "IpMonitorDlg.h" class CIpMonitorDlgclass CPing{public:CPing(void) ~CPing(void)void Start(CIpMonitorDlg* pDialog) void Stop() bool IsRunning(){return m_thread!= NULL} CString m_sIp CString m_sPort CString m_sTimeSpan CString m_sCommandbool m_bStopCIpMonitorDlg* m_pIpMonitorDlgprotected:HANDLE m_thread DWORD m_threadId } //---------------- ping.cpp -------------------------//孙局#include "StdAfx.h"#include "Ping.h"#include "afxsock.h" /* TRACE(_T("\r\n"))*/ CString GetErrorMessage(){CString msg DWORD err = GetLastError() LPVOID lpMsgBufFormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,NULL,err,0, // Default language(LPTSTR) &lpMsgBuf,0,NULL )msg.Format(_T("[%d] %s"),err, (LPCTSTR)lpMsgBuf)// Free the buffer.LocalFree( lpMsgBuf ) return msg} void Trace(CPing* parent, LPCTSTR lpcszMSg){TRACE(lpcszMSg) if(parent != NULL &&parent->m_pIpMonitorDlg != NULL){//ASSERT(::IsWindow(parent->m_pIpMonitorDlg->m_hWnd)) //if(::IsWindow(parent->m_pIpMonitorDlg->m_hWnd))parent->m_pIpMonitorDlg->TraceMessage(lpcszMSg) } }DWORD WINAPI RunBody( LPVOID lpParam ){CString msg CPing* parent = (CPing*) lpParam ASSERT(parent != NULL)LPCTSTR lpszHostAddress = parent->m_sIp UINT nHostPort = _tstoi(parent->m_sPort) ASSERT(nHostPort >敬凯雀 0)int timespan = _tstoi(parent->m_sTimeSpan) if (timespan <亮早 3) timespan = 3timespan *= 1000 LPTSTR szCmdline = _tcsdup(parent->m_sCommand) STARTUPINFO si PROCESS_INFORMATION piZeroMemory( &si, sizeof(si) ) si.cb = sizeof(si) ZeroMemory( &pi, sizeof(pi) )Trace(parent,_T("thread start\r\n"))if(!AfxSocketInit ()){msg.Format(_T("AfxSocketInit fail , %s \r\n"), GetErrorMessage()) Trace(parent,msg) return 0x01 } CSocket connectbool bTestOk while(!parent->m_bStop){if (!connect.Create(/*0, SOCK_STREAM, _T("127.0.0.1")*/)){msg.Format(_T("connect.Create fail, %s \r\n"), GetErrorMessage()) Trace(parent,msg) }else{bTestOk = connect.Connect(lpszHostAddress, nHostPort) if(bTestOk){msg.Format(_T("connect to %s:%s ok...\r\n"), parent->m_sIp, parent->m_sPort) }else{msg.Format(_T("connect to %s:%s fail, %s ...\r\n"), parent->m_sIp, parent->m_sPort, GetErrorMessage()) }Trace(parent,msg) connect.Close()if(!bTestOk){if(!CreateProcess(NULL,szCmdline, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi)){msg.Format(_T("Execute command fail, %s\r\n"), GetErrorMessage()) Trace(parent,msg) }}}::Sleep(timespan) } free(szCmdline) szCmdline = NULLTrace(parent,_T("thread end\r\n"))return 0} CPing::CPing(void){m_thread = NULL m_threadId = 0 m_bStop = false} CPing::~CPing(void){Stop()} void CPing::Start(CIpMonitorDlg* pDialog){TRACE(_T("CPing::Start\r\n")) ASSERT(m_thread == NULL) ASSERT(pDialog != NULL)if(m_thread != NULL){TRACE(_T("CPing::Start m_thread != NULL\r\n")) return }m_pIpMonitorDlg = pDialogm_bStop = falsem_thread = CreateThread(NULL, 0, RunBody, this, NULL, &m_threadId)} void CPing::Stop(){Trace(this, _T("CPing::Stop\r\n")) m_bStop = true if(m_thread != NULL){int timespan = _tstoi(m_sTimeSpan) if (timespan <3) timespan = 3 timespan += 3 timespan *= 1000Trace(this, _T("waiting for thread stop...\r\n")) DWORD r = ::WaitForSingleObject(m_thread, timespan) if(WAIT_TIMEOUT == r){TRACE(_T("CPing::Stop TerminateThread ...\r\n")) TerminateThread(m_thread, 0x02) }::CloseHandle(m_thread) m_thread = NULL }}#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int ping(char * ip)
{
int ok = 1FILE * f = 0
char cmd[80]=""sprintf(cmd, "汪孝毕ping %s >ping.txt", ip)
system(cmd)
f = fopen("ping.txt", "r")
if(!f) return 0
while(!feof(f)) {
fgets(cmd, 80, f)
if(strstr(cmd, "timed out")) ok = 0
}/*end while*/
system("del ping.txt")
return ok
}
int main(void)
{
int ok = ping("192.168.1.1")
if(ok) printf("目标连通!")else printf("网络不通!")
return 0
}
看明白了没有?system命令把ping结果困芹存到一个ping.txt的临时文件里面,然后检验文件内容是否有“超时”的字样,如果有则慎培返回“不通”。
1、“文件”->“新建...”->“
项目...”,在“已安装的模板”下选择“Visual C++”,并选择“Win32”下的“Win32控制台应用程序”,陆则输入
解决方案名后“确定”,之后“下一步”,勾选“空项目”后“完成”。
2、在“解决方案资源管理早皮棚握虚器”中展开项目后,右击“源文件”后选择“添加...”->“新建项...”,在“已安装的模板”下的“Visual C++”中选择“C++源文件(.cpp)”添加即可。
评论列表(0条)