C++ PING
#pragma once
#include "stdafx.h"
#include <WS2tcpip.h>
#include <WinSock2.h>
#pragma comment(lib, "WS2_32")
typedef struct icmp_hdr
{
unsigned char icmp_type;
unsigned char icmp_code;
unsigned short icmp_checksum;
unsigned short icmp_id;
unsigned short icmp_sequence;
unsigned long icmp_timestamp;
} ICMP_HDR;
USHORT checksum(USHORT* buff, int size)
{
unsigned long cksum = 0;
while (size > 1)
{
cksum += *buff++;
size -= sizeof(USHORT);
}
if (size)
{
cksum += *(UCHAR*)buff;
}
cksum = (cksum >> 16) + (cksum & 0xffff);
cksum += (cksum >> 16);
return (USHORT)(~cksum);
}
int main(int argc, char *argv[])
{
char buff[sizeof(ICMP_HDR) + 32];
ICMP_HDR* pIcmp = (ICMP_HDR*)buff;
pIcmp->icmp_type = 8;
pIcmp->icmp_code = 0;
pIcmp->icmp_id = (unsigned short)GetCurrentProcessId();
pIcmp->icmp_checksum = 0;
pIcmp->icmp_sequence = 0;
memset(&buff[sizeof(ICMP_HDR)], 'A', 32);
struct sockaddr_in destAddr;
struct sockaddr_in fromAddr;
char recvBuff[1024];
int addr_len = sizeof(struct sockaddr_in);
destAddr.sin_family = AF_INET;
destAddr.sin_port = htons(3456);
destAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
SOCKET sRaw = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
while (pIcmp->icmp_sequence++ < 4)
{
int Ret;
pIcmp->icmp_checksum = 0;
pIcmp->icmp_timestamp = GetTickCount();
pIcmp->icmp_checksum = checksum((USHORT*)buff, sizeof(ICMP_HDR) + 32);
Ret = sendto(sRaw, buff, sizeof(ICMP_HDR) + 32, 0, (struct sockaddr *)&destAddr, sizeof(destAddr));;
printf("%d\n", &Ret);
closesocket(sRaw);
memset(recvBuff, '\0', 1024);
Ret = recvfrom(sRaw, recvBuff, sizeof(recvBuff), 0, (struct sockaddr *)&fromAddr, &addr_len);
printf("%d\n", &Ret);
printf("%s\n", inet_ntoa(fromAddr.sin_addr));
}
}

这是什么情况???
#include "stdafx.h"
#include <WS2tcpip.h>
#include <WinSock2.h>
#pragma comment(lib, "WS2_32")
typedef struct icmp_hdr
{
unsigned char icmp_type;
unsigned char icmp_code;
unsigned short icmp_checksum;
unsigned short icmp_id;
unsigned short icmp_sequence;
unsigned long icmp_timestamp;
} ICMP_HDR;
USHORT checksum(USHORT* buff, int size)
{
unsigned long cksum = 0;
while (size > 1)
{
cksum += *buff++;
size -= sizeof(USHORT);
}
if (size)
{
cksum += *(UCHAR*)buff;
}
cksum = (cksum >> 16) + (cksum & 0xffff);
cksum += (cksum >> 16);
return (USHORT)(~cksum);
}
int main(int argc, char *argv[])
{
char buff[sizeof(ICMP_HDR) + 32];
ICMP_HDR* pIcmp = (ICMP_HDR*)buff;
pIcmp->icmp_type = 8;
pIcmp->icmp_code = 0;
pIcmp->icmp_id = (unsigned short)GetCurrentProcessId();
pIcmp->icmp_checksum = 0;
pIcmp->icmp_sequence = 0;
memset(&buff[sizeof(ICMP_HDR)], 'A', 32);
struct sockaddr_in destAddr;
struct sockaddr_in fromAddr;
char recvBuff[1024];
int addr_len = sizeof(struct sockaddr_in);
destAddr.sin_family = AF_INET;
destAddr.sin_port = htons(3456);
destAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
SOCKET sRaw = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
while (pIcmp->icmp_sequence++ < 4)
{
int Ret;
pIcmp->icmp_checksum = 0;
pIcmp->icmp_timestamp = GetTickCount();
pIcmp->icmp_checksum = checksum((USHORT*)buff, sizeof(ICMP_HDR) + 32);
Ret = sendto(sRaw, buff, sizeof(ICMP_HDR) + 32, 0, (struct sockaddr *)&destAddr, sizeof(destAddr));;
printf("%d\n", &Ret);
closesocket(sRaw);
memset(recvBuff, '\0', 1024);
Ret = recvfrom(sRaw, recvBuff, sizeof(recvBuff), 0, (struct sockaddr *)&fromAddr, &addr_len);
printf("%d\n", &Ret);
printf("%s\n", inet_ntoa(fromAddr.sin_addr));
}
}

这是什么情况???
...全文
相关推荐
MY_ping(1).rar_myping.cpp
标题 "MY_ping(1).rar_myping.cpp" 指示的是一个关于实现 Ping 命令的项目,这个项目使用 Visual C++ 开发环境。
基于原始套接字的ping C++实现.zip
本文介绍了一个使用cppnetwork库中的Ping类实现网络ping操作的C++代码示例。代码文件demo.cpp由sotter编写,演示了如何ping本机地址、域名以及不存在的IP地址,并将结果输
C++ Ping程序的cpp文件
基于控制台边学的应用程序,Ping程序可以对原始套接字简单俩接, 作为一个基础了解与认识怎么运用应用协议!
C++实现ping功能
在IT行业中,C++是一种强大的编程语言,常用于构建高性能的应用程序。本主题将深入探讨如何使用C++来实现ping功能,这是一种网络诊断工具,用于检查网络连接的可达性。
源_c/C++_网络编程_stickael_ping程序_
在提供的源.cpp文件中,我们可以找到实现这些功能的具体代码。分析和理解这些代码将有助于我们深入学习C++网络编程,特别是关于ICMP协议和套接字API的使用。


