ping的C++代码及释义
如题了,释义希望是中文的,俺不想自己翻译是给别人赶作业用的,希望高人帮忙了
追问:这东西我也搜到了,就是那些微微纠结的英文,因为专业英文不好翻译人家要的就是汉语的……
如题了,释义希望是中文的,俺不想自己翻译是给别人赶作业用的,希望高人帮忙了
追问:这东西我也搜到了,就是那些微微纠结的英文,因为专业英文不好翻译人家要的就是汉语的……
答案:
#include "winsock2i.h" #pragma comment(lib,"ws2_32.lib") #pragma pack(4) #define WIN32_LEAN_AND_MEAN #define ICMP_ECHO 8 #define ICMP_ECHOREPLY 0
typedef struct iphdr unsigned int h_len:4; // length of the header unsigned int version:4; // Version of IP unsigned char tos; // Type of service unsigned short total_len; // total length of the packet unsigned short ident; // unique identifier unsigned short frag_and_flags; // flags unsigned char ttl; unsigned char proto; // protocol (TCP, UDP etc) unsigned short checksum; // IP checksum unsigned int sourceIP; unsigned int destIP; }IpHeader; // ICMP header typedef struct _ihdr BYTE i_type; BYTE i_code; /* type sub code */ USHORT i_cksum; USHORT i_id; USHORT i_seq; /* This is not the std header, but we reserve space for time */ ULONG timestamp; }IcmpHeader;
#define DEF_PACKET_SIZE 32 #define MAX_PACKET 1024
#define xfree(p) HeapFree (GetProcessHeap(),0,(p)) //The response is an IP packet. We must decode the IP header to locate the ICMP data void decode_response(char* buf, int bytes, struct sockaddr_in* from) IcmpHeader *icmphdr; unsigned short iphdrlen; iphdr = (IpHeader *)buf; iphdrlen = iphdr->h_len * 4 ; // number of 32-bit words *4 = bytes if (bytes < iphdrlen + ICMP_MIN) icmphdr = (IcmpHeader*)(buf + iphdrlen); if (icmphdr->i_type != ICMP_ECHOREPLY) if (icmphdr->i_id != (USHORT)GetCurrentProcessId()) printf("%d bytes from %s:",bytes, inet_ntoa(from->sin_addr)); printf(" icmp_seq = %d. ",icmphdr->i_seq); printf(" time: %d ms ",GetTickCount()-icmphdr->timestamp); printf("\n"); USHORT checksum(USHORT *buffer, int size) while(size > 1)
cksum = (cksum >> 16) + (cksum & 0xffff); cksum += (cksum >>16); return (USHORT)(~cksum);
void fill_icmp_data(char* icmp_data, int datasize) char* datapart; icmp_hdr = (IcmpHeader*)icmp_data; icmp_hdr->i_type = ICMP_ECHO; icmp_hdr->i_code = 0; icmp_hdr->i_id = (USHORT)GetCurrentProcessId(); icmp_hdr->i_cksum = 0; icmp_hdr->i_seq = 0; datapart = icmp_data + sizeof(IcmpHeader); // Place some junk in the buffer. void Usage(char *progname) fprintf(stderr,"%s [data_size]\n",progname); fprintf(stderr,"datasize can be up to 1Kb\n"); ExitProcess(STATUS_FAILED); int main(int argc, char **argv) SOCKET sockRaw; struct sockaddr_in dest; struct sockaddr_in from; struct hostent* hp; int bread,datasize; int fromlen = sizeof(from); int timeout = 1000; char* dest_ip; char* icmp_data; char* recvbuf; unsigned int addr=0; USHORT seq_no = 0; if (WSAStartup(MAKEWORD(2,1),&wsaData) != 0) ExitProcess(STATUS_FAILED); if (argc < 2) sockRaw = WSASocket(AF_INET, SOCK_RAW, IPPROTO_ICMP, NULL, 0, 0); if (sockRaw == INVALID_SOCKET) ExitProcess(STATUS_FAILED); bread = setsockopt(sockRaw,SOL_SOCKET,SO_RCVTIMEO,(char*)&timeout, sizeof(timeout)); if(bread == SOCKET_ERROR) ExitProcess(STATUS_FAILED); timeout = 1000; bread = setsockopt(sockRaw,SOL_SOCKET,SO_SNDTIMEO,(char*)&timeout, sizeof(timeout)); if(bread == SOCKET_ERROR) memset(&dest,0,sizeof(dest)); hp = gethostbyname(argv[1]);
if ((!hp) && (addr == INADDR_NONE) ) if (hp != NULL)
dest_ip = inet_ntoa(dest.sin_addr); datasize += sizeof(IcmpHeader); icmp_data = (char*)malloc(MAX_PACKET); recvbuf = (char*)malloc(MAX_PACKET);
ExitProcess(STATUS_FAILED); memset(icmp_data,0,MAX_PACKET); fill_icmp_data(icmp_data,datasize); while(1) ((IcmpHeader*)icmp_data)->timestamp = GetTickCount(); ((IcmpHeader*)icmp_data)->i_seq = seq_no++; ((IcmpHeader*)icmp_data)->i_cksum = checksum((USHORT*)icmp_data, datasize); int bwrote = sendto(sockRaw,icmp_data,datasize,0,(struct sockaddr*)&dest, sizeof(dest)); if (bwrote == SOCKET_ERROR) fprintf(stderr,"sendto failed: %d\n",WSAGetLastError()); ExitProcess(STATUS_FAILED); if (bwrote < datasize ) bread = recvfrom(sockRaw,recvbuf, MAX_PACKET, 0, (struct sockaddr*)&from, &fromlen); if (bread == SOCKET_ERROR) fprintf(stderr,"recvfrom failed: %d\n",WSAGetLastError()); ExitProcess(STATUS_FAILED); } decode_response(recvbuf, bread, &from); Sleep(1000); 上一个:求图书馆管理系统C++源代码 更多图片编程知识:
访问www.zzzyk.com 试试 CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络, |