DEV Community

lovehmos
lovehmos

Posted on

HarmonyOS Ping Tool Dev Guide & Troubleshooting

HarmonyOS Ping Tool Dev Guide & Troubleshooting

About HarmonyOS 5

HarmonyOS 5 (also known as HarmonyOS Next) represents a revolutionary step in the evolution of Huawei's distributed operating system. As someone who has been following its development closely, I can attest to the remarkable improvements in this version. The system's microkernel architecture not only enhances security but also provides unprecedented flexibility in cross-device collaboration. What excites me most about HarmonyOS 5 is its focus on developer experience - the new ArkTS language, enhanced UI components, and improved debugging tools have made development much more efficient. The distributed capabilities allow us to create truly seamless experiences across different devices, from smartphones to tablets, smart TVs, and IoT devices.

About the Author

I am a dedicated HarmonyOS developer based in China, with over three years of experience in the ecosystem. My journey with HarmonyOS began with its early versions, and I've witnessed its remarkable evolution. As a developer who has contributed to several open-source HarmonyOS projects, I've gained valuable insights into its architecture and best practices. I'm particularly passionate about network tools and system integration, which led me to develop this comprehensive Ping tool. Through my blog and GitHub repositories, I've been sharing my experiences and helping other developers navigate the exciting world of HarmonyOS development.

Preface

Recently, while developing the HarmonyOS Developer Toolbox, we needed to implement a network connectivity testing feature. Due to HarmonyOS API limitations, we couldn't directly use the traditional ICMP protocol for Ping functionality. After multiple attempts, we found that using HTTP protocol was a viable alternative. Although it differs from traditional Ping, it basically meets the requirements for development and debugging.

I. Feature Overview

1.1 Basic Features

  • Support for IP address and domain name testing
  • Automatic execution of 4 tests
  • Display response time
  • Packet loss statistics

1.2 Interface Features

  • Address input field
  • Test status display
  • Result display
  • Favorite functionality

II. Technical Implementation

2.1 Ping Implementation Principle

During development, we found that HarmonyOS API currently doesn't support ICMP protocol and can't directly send ICMP Echo requests. After research, we decided to use HTTP protocol for network connectivity testing.

Reasons for choosing HTTP protocol:

  1. System Limitations

    • HarmonyOS API doesn't support ICMP protocol
    • Unable to send ICMP Echo requests
    • Lack of low-level network protocol support
  2. Alternative Solution

    • Use HTTP GET requests
    • Determine latency through response time
    • Use status codes to determine connectivity
  3. Implementation Advantages

    • Use standard HTTP API
    • No special permissions required
    • Good compatibility
    • Simple implementation
  4. Limitations

    • Cannot fully simulate ICMP Ping
    • Slightly higher response time
    • Dependent on HTTP service

Implementation Steps:

  1. Input Validation

    • Check if address is empty
    • Validate IP address format
    • Validate domain name format
  2. DNS Resolution

    • Get default network
    • Resolve hostname
    • Handle resolution failures
  3. Connectivity Testing

    • Create HTTP request
    • Set 3-second timeout
    • Send GET request
    • Calculate response time
  4. Result Processing

    • Check status code
    • Handle timeouts
    • Return results
  5. Resource Cleanup

    • Destroy request object
    • Release connection resources

2.2 Core Components

@Entry
@Component
struct PingTool {
  @State host: string = '';              // Target host address
  @State result: string = '';            // Test result
  @State isPinging: boolean = false;      // Test status
  @State isError: boolean = false;        // Error status
  @State pingCount: number = 0;          // Test count
  @State pingResults: string[] = [];      // Test results
  @State isFavorite: boolean = false;     // Favorite status

  private maxPingCount: number = 4;       // Maximum test count
}
Enter fullscreen mode Exit fullscreen mode

2.3 Core Code Implementation

// Ping tool component
@Entry
@Component
struct PingTool {
  @State host: string = '';              // Target host address
  @State result: string = '';            // Test result
  @State isPinging: boolean = false;      // Test status
  @State pingResults: string[] = [];      // Test results
  private maxPingCount: number = 4;       // Maximum test count

  // Execute test
  private async startTest(): Promise<void> {
    try {
      this.isPinging = true;
      this.pingResults = [];

      // Execute multiple tests
      for (let i = 0; i < this.maxPingCount; i++) {
        const result = await NetworkService.ping(this.host);
        this.pingResults.push(result);
        this.result = this.formatResults();

        // Test interval
        if (i < this.maxPingCount - 1) {
          await new Promise(resolve => setTimeout(resolve, 1000));
        }
      }

      // Add statistics
      this.addStatistics();
    } catch (error) {
      this.result = `Test failed: ${error?.message || 'Unknown error'}`;
    } finally {
      this.isPinging = false;
    }
  }
}

// Network service
export class NetworkService {
  // Execute Ping test
  static async ping(host: string): Promise<string> {
    const httpRequest = http.createHttp();
    try {
      const startTime = new Date().getTime();

      // Send request
      const response = await httpRequest.request(
        `http://${host}`,
        {
          method: http.RequestMethod.GET,
          connectTimeout: 3000,
          readTimeout: 3000
        }
      );

      const duration = new Date().getTime() - startTime;
      return `Response time ${duration}ms`;
    } catch (error) {
      return 'Connection failed';
    } finally {
      httpRequest.destroy();
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

2.4 Network Service Implementation

// NetworkService.ts
export class NetworkService {
  // Validate IP address
  static isValidIP(ip: string): boolean {
    try {
      const ipRegex = /^(\d{1,3}\.){3}\d{1,3}$/;
      if (!ipRegex.test(ip)) return false;

      const parts = ip.split('.');
      return parts.every(part => {
        const num = parseInt(part);
        return num >= 0 && num <= 255;
      });
    } catch {
      return false;
    }
  }

  // Validate domain name
  static isValidDomain(domain: string): boolean {
    try {
      const domainRegex = /^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/;
      return domainRegex.test(domain);
    } catch {
      return false;
    }
  }

  // Execute Ping test
  static async ping(host: string): Promise<string> {
    try {
      if (!host || host.trim() === '') {
        return 'Error: Host address cannot be empty';
      }

      if (!NetworkService.isValidIP(host) && !NetworkService.isValidDomain(host)) {
        return 'Error: Invalid IP address or domain name format';
      }

      const httpRequest = http.createHttp();

      try {
        const startTime = new Date().getTime();

        // DNS resolution
        try {
          const netConnection = await connection.getDefaultNet();
          await netConnection.getAddressesByName(host);
        } catch (error) {
          httpRequest.destroy();
          return 'Error: DNS resolution failed';
        }

        // Send request
        const response = await httpRequest.request(
          `http://${host}`,
          {
            method: http.RequestMethod.GET,
            connectTimeout: 3000,
            readTimeout: 3000,
            expectDataType: http.HttpDataType.STRING,
            header: {
              'User-Agent': 'HarmonyOS-PingTool'
            }
          }
        );

        const endTime = new Date().getTime();
        const duration = endTime - startTime;

        if (response.responseCode >= 200 && response.responseCode < 400) {
          return `Success: Response time ${duration}ms`;
        } else {
          return `Failed: Server returned status code ${response.responseCode}`;
        }
      } finally {
        httpRequest.destroy();
      }
    } catch (error) {
      if (error.code === -1) {
        return 'Error: Connection timeout';
      }
      return `Error: ${error.message || 'Unable to connect to target host'}`;
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

III. Development Experience

3.1 Problems Encountered

  1. Network Latency

    • Problem: Test timeout
    • Solution: Set reasonable timeout period
  2. Input Validation

    • Problem: Invalid address
    • Solution: Add format validation
  3. State Synchronization

    • Problem: State out of sync
    • Solution: Use state management
  4. Memory Management

    • Problem: Memory usage
    • Solution: Release resources promptly

3.2 Optimization Suggestions

  1. Input Validation

    • Check IP format
    • Validate domain name
    • Handle special characters
  2. Performance Optimization

    • Control test frequency
    • Optimize UI rendering
    • Release resources promptly
  3. User Experience

    • Display test progress
    • Support cancel operation
    • Save test history
  4. Security

    • Limit test targets
    • Control test frequency
    • Protect user privacy

IV. Summary

The Ping tool implements basic network connectivity testing functionality. Although implemented using HTTP protocol, it basically meets the requirements for development and debugging. Through this tool, you can:

  • Test network connectivity
  • Diagnose network issues
  • Monitor network quality
  • Optimize network configuration

V. Reference Resources

Welcome to Experience

The Ping tool introduced in this article has been integrated into the HarmonyOS Developer Toolbox. Welcome to download and experience more features!

HarmonyOS Developer Toolbox


Author: In the World of Development
Email: 1743914721@qq.com
Copyright Notice: This article is an original work by the CSDN blogger, please include the original source link and this statement when reprinting.

Top comments (0)