大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 技术开发 -> 程序开发 -> 使用TCP堆栈来Ping计算机

使用TCP堆栈来Ping计算机

时间: 2021-07-31 作者:daque

//原著:les jordan//翻译:重庆大学光电工程学院   贾旭滨//欢送大师品评指点,感谢!    以次的这个类是处置一个很普遍的题目的:在一台win95的计划机上如何样运用mstcp仓库去ping其余一台计划机。固然,这个类在nt3.51和nt4上也不妨用。明显,microsoft公司不会那么笨,在win体例中又其余结构这么一个独立的体制来处置这个题目,让从来就搀杂的win体例越发搀杂。那么,咱们只能用icmp dll本人来处置这个题目了。然而,很让人悲观,microsoft公司直到winsock 2.0也没有处置这个题目。    困难即是:给一个计划机的名字,大概一台计划机的ip地方,如何样去ping它,并且获得它的相应功夫。以是咱们用了icmp dll,而对于少许socket构造来说,在csocket中仍旧十足都有设置了。以是,底下的类是一个csocket的子类,它将会有更好的功效来处置更多的题目。然而你得先获得icmpapi.h,icmapi.cpp,icmp.lib和ipexport.h,ipexport.cpp那些文献,那些文献该当加在你的工程中。那些文献你不妨在microsoft developers network的光盘上获得,然而在微软公司的网页上也能拿到,并且革新。类中有4个大众因变量,如次:unsigned long resolveip(cstring strip)unsigned long resolvename(cstring strhostname)cstring getip(unsigned long ulip)dword pinghost(unsigned long ulip, int ipingtimeout)    resolveip(cstring strip)因变量运用一个ip地方的字符串来动作参数,归来值是ip地方值。    resolvename(cstring strhostname)因变量运用一计划机名的字符串来动作参数,过程dns大概wins的领会,归来值是被ping计划机的ip 地方,提防它运用了gethostbyname模块化。    getip(unsigned long ulip)因变量是以ip地方动作参数(提防是ip地方),归来值是一个表白ip地方的字符串。    pinghost(unsigned long ulip, int ipingtimeout)因变量,第1个参数是ip地方(提防是ip地方,而不是ip地方的字符串),第2个参数是表白功夫值的,表白间隙功夫的。因变量功效是,去ping一台计划机,归来值是ping的相应功夫。以次是代码://*//-------------------------------------------------------------------------//-------------------------------------------------------------------------//icmpecho.h//-------------------------------------------------------------------------//------------------------------------------------------------------------//*class cicmpecho : public csocket{// attributespublic:// operationspublic:    cicmpecho();    virtual ~cicmpecho();    unsigned long resolveip(cstring strip);    unsigned long resolvename(cstring strhostname);    dword pinghost(unsigned long ulip, int ipingtimeout);    cstring getip(unsigned long ulip);// overridespublic:    // classwizard generated virtual function overrides    //{{afx_virtual(cicmpecho)    //}}afx_virtual    // generated message map functions    //{{afx_msg(cicmpecho)        // note - the classwizard will add and remove member functions here.    //}}afx_msg// implementationprotected:};///////////////////////////////////////////////////////////////////////////////*//------------------------------------------------------------------------------------------------------------------//icmpecho.cpp//------------------------------------------------------------------------------------------------------------------//*// icmpecho.cpp : implementation file//#include "icmpecho.h"extern "c" {#include "ipexport.h"#include "icmpapi.h"}#define ping_timeout 100#ifdef _debug#define new debug_new#undef this_filestatic char this_file[] = __file__;#endif/////////////////////////////////////////////////////////////////////////////// cicmpechocicmpecho::cicmpecho(){}cicmpecho::~cicmpecho(){}// do not edit the following lines, which are needed by classwizard.#if 0begin_message_map(cicmpecho, csocket)    //{{afx_msg_map(cicmpecho)    //}}afx_msg_mapend_message_map()#endif    // 0/////////////////////////////////////////////////////////////////////////////// cicmpecho member functionsunsigned long cicmpecho::resolveip(cstring strip){    //task 1:    given ip address i.e. "111.111.111.111",    //    return network byte ordered address (ulip)    unsigned long ulip;    ulip =(ipaddr)inet_addr(strip);    return ulip;}unsigned long cicmpecho::resolvename(cstring strhostname){    //task 1:    resolve hostname (through dns or wins, whichever appropriate)    //task 2:    return network byte ordered address (ulip)    unsigned long ulip;    hostent* phostent;    phostent = gethostbyname(strhostname);        if (phostent == null)        return 0;    ulip = *(dword*)(*phostent->h_addr_list);    return ulip;}dword cicmpecho::pinghost(unsigned long ulip, int ipingtimeout){    //task 1:    open icmp handle    //task 2:    create structure to receive ping reply    //task 3:    sendping (sendecho)    //task 4:    close icmp handle    //task 5:    return roundtriptime    unsigned long ip = ulip;    handle icmphandle = icmpcreatefile();    char reply[sizeof(icmp_echo_reply)+8];    icmp_echo_reply* iep=(icmp_echo_reply*)&reply;    iep->roundtriptime = 0xffffffff;    icmpsendecho(icmphandle,ip,0,0,null,reply,sizeof(icmp_echo_reply)+8,ipingtimeout);    icmpclosehandle(icmphandle);    return iep->roundtriptime;}cstring cicmpecho::getip(unsigned long ulip){    //task 1:    given a host order ulip address    //    return a ip address in format of xxx.xxx.xxx.xxx    lpstr szaddr;    struct in_addr inetaddr;        inetaddr.s_addr = (ipaddr)ulip;    szaddr = inet_ntoa(inetaddr);    cstring csip = szaddr;    return csip;}

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.