HarmonyOS 鸿蒙Next ping工具
HarmonyOS 鸿蒙Next ping工具
更多关于HarmonyOS 鸿蒙Next ping工具的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
这个可以,就是目前只看到一个ping的方法
有其他需求可以提 ISSUE
您好,当前没有三方库可以处理ping和监听wifi变化;
connection可以监听网络类型:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-net-connection-V5#onnetcapabilitieschange
ping功能看下通过c socket能否替代:
std::string TestPing::ping(const char *ip) {
OH_LOG_DEBUG(LOG_APP, "Start ping");
struct sockaddr_in addr;
bzero(&addr, sizeof(addr));
std::string strip = ip;
addr.sin_family = AF_INET;
addr.sin_port = 0;
if (inet_aton(ip, (struct in_addr *)&addr.sin_addr.s_addr) == 0) {
OH_LOG_DEBUG(LOG_APP, “bad ip address: %s\n”, ip);
struct hostent* hptr = gethostbyname(ip);
if (hptr == nullptr) {
return "Ping " + strip + “: Name does not resolve\n”;
}
memcpy(&addr.sin_addr, hptr->h_addr, sizeof(hptr->h_addr));
}
// 创建一个可以发送和接收ICMP消息的套接字
int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP);
if (sock == -1) {
OH_LOG_DEBUG(LOG_APP, “create icmp socket error”);
return “create icmp socket error”;
}
// 设置接收socket消息的超时时间
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = RECV_TIMEOUT_USEC;
int ret = setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
if (ret == -1) {
OH_LOG_DEBUG(LOG_APP, “setsockopt option error”);
return “setsockopt option error”;
}
double next_ts = get_timestamp();
int ident = getpid();
int seq = 1;
double current_ts = get_timestamp();
if (current_ts >= next_ts) {
// 发送回显请求
ret = send_echo_request(sock, &addr, ident, seq);
if (ret == -1) {
OH_LOG_DEBUG(LOG_APP, “Send failed”);
<span class="hljs-keyword">return</span> <span class="hljs-string">"Send failed"</span>;
}
next_ts = current_ts + 1;
seq += 1;
}
// 接收ICMP回显应答报文
std::string recv_ping_echo_reply = recv_echo_reply(sock, ident, strip);
if (recv_ping_echo_reply == “”) {
OH_LOG_DEBUG(LOG_APP, “Receive failed”);
return “Receive failed”;
}
return recv_ping_echo_reply;
}
int TestPing::send_echo_request(int sock, struct sockaddr_in *addr, int ident, int seq) {
// 定义ICMP报文
struct icmp_echo icmp;
bzero(&icmp, sizeof(icmp));
// 定义头部数据
icmp.type = 8;
icmp.code = 0;
icmp.ident = htons(ident);
icmp.seq = htons(seq);
strncpy(icmp.magic, MAGIC, MAGIC_LEN);
icmp.sending_ts = get_timestamp();
// 计算校验和
icmp.checksum = htons(calculate_checksum((unsigned char *)&icmp, sizeof(icmp)));
// 发送ICMP报文
int bytes = sendto(sock, &icmp, sizeof(icmp), 0, (struct sockaddr *)addr, sizeof(*addr));
if (bytes == -1) {
return -1;
}
return 0;
}
std::string TestPing::recv_echo_reply(int sock, int ident, const std::string& strip) {
OH_LOG_DEBUG(LOG_APP, “Start recv_echo_reply”);
// 定义缓冲区
unsigned char buffer[IP_BUFFER_SIZE];
struct sockaddr_in peer_addr;
// 接收一个新报文
socklen_t addr_len = sizeof(peer_addr);
int bytes = recvfrom(sock, buffer, sizeof(buffer), 0, (struct sockaddr *)&peer_addr, &addr_len);
if (bytes == -1) {
// 接收报文超时
if (errno == EAGAIN || errno == EWOULDBLOCK) {
OH_LOG_DEBUG(LOG_APP, “errno == EAGAIN || errno == EWOULDBLOCK, timeout, return\n”);
return “”;
}
OH_LOG_DEBUG(LOG_APP, “recvfrom bytes is -1, return\n”);
return “”;
}
// IP头部长度
int ip_header_len = (buffer[0] & 0xf) << 2;
// 从IP报文中取出ICMP报文
struct icmp_echo *icmp = (struct icmp_echo *)(buffer + ip_header_len);
// 检查ICMP报文类型
if (icmp->type != 0 || icmp->code != 0) {
OH_LOG_DEBUG(LOG_APP, “check type failed\n”);
return “”;
}
// IP头部
struct ip *iphdr = (struct ip *)buffer;
std::ostringstream stream;
stream << “Ping " << strip << “(” << inet_ntoa(peer_addr.sin_addr) << “):\n”
<< inet_ntoa(peer_addr.sin_addr) << " seq=” << ntohs(icmp->seq)
<< " ttl=" << static_cast<unsigned int>(iphdr->ip_ttl) << " time=" << std::fixed << std::setprecision(2)
<< (get_timestamp() - icmp->sending_ts) * 1000 << “ms\n”;
OH_LOG_DEBUG(LOG_APP, “stream.str(): %s\n”, stream.str().c_str());
return stream.str();
}
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>
针对HarmonyOS鸿蒙Next的ping工具问题,以下是一些专业解答:
HarmonyOS鸿蒙Next作为华为推出的新一代操作系统,其开发环境和工具链均经过精心设计和优化。ping工具通常用于测试网络连接,而在HarmonyOS鸿蒙Next的开发环境中,开发者可能更关注于系统内部的通信和调试。
如果需要在HarmonyOS鸿蒙Next环境中使用ping工具,首先确保开发环境已经正确搭建,包括DevEco Studio的安装和配置。随后,可以通过命令行界面(Terminal)或集成开发环境(IDE)中的工具来执行ping命令。
请注意,HarmonyOS鸿蒙Next可能具有特定的网络配置和权限要求,因此在使用ping工具时,需要确保具有相应的网络访问权限和正确的网络配置。
如果在使用ping工具时遇到问题,建议检查网络连接、防火墙设置以及HarmonyOS鸿蒙Next的特定网络要求。如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html。