Skip to main content

Overview

The PublicIPAddress class provides functionality to retrieve the public IP address of the player’s machine. This is essential for identifying and tracking players in the ACHCE Client system.

Class Definition

namespace vbEngC
{
    public class PublicIPAddress
    {
        public string GetPublicIPAddress()
        {
            // Implementation
        }
    }
}

Methods

GetPublicIPAddress

Retrieves the public IP address of the current machine using an external service.
GetPublicIPAddress
method
Returns the public IP address as a string, or an error message if the retrieval fails.

Method Signature

public string GetPublicIPAddress()

Returns

string
string
The public IP address of the player (e.g., “203.0.113.42”), or an error message if the operation fails.

Implementation Details

The method uses the ipinfo.io external service to retrieve the public IP address:
public string GetPublicIPAddress()
{
    try
    {
        // Utiliza un servicio externo para obtener la dirección IP pública
        using (var client = new WebClient())
        {
            string publicIP = client.DownloadString("http://ipinfo.io/ip").Trim();
            return publicIP;
        }
    }
    catch (Exception ex)
    {
        return "No se pudo obtener la dirección IP pública: " + ex.Message;
    }
}

Usage Example

Basic Usage

// Create an instance of PublicIPAddress
PublicIPAddress IpAddressPlayer = new PublicIPAddress();

// Retrieve the player's public IP
string IpPlayer = IpAddressPlayer.GetPublicIPAddress();

// Use the IP address
Console.WriteLine($"Player IP: {IpPlayer}");

Integration with Player Class

The PublicIPAddress class is typically used in conjunction with the Player class:
// Obtain the client's IP address
PublicIPAddress IpAddressPlayer = new PublicIPAddress();
string IpPlayer = IpAddressPlayer.GetPublicIPAddress();

// Create a player with the IP address
Player ply = new Player()
{
    IP = IpPlayer,
};

Error Handling

The method includes exception handling to gracefully manage network errors or service unavailability. If the IP retrieval fails, the method returns an error message instead of throwing an exception.

Possible Error Scenarios

  • Network connectivity issues: No internet connection available
  • Service unavailable: The ipinfo.io service is down or unreachable
  • Timeout: The request takes too long to complete
  • Blocked access: Firewall or security software blocks the request

Example Error Message

No se pudo obtener la dirección IP pública: The remote server returned an error: (503) Server Unavailable.

Technical Details

Dependencies
System.Net.WebClient
Uses the WebClient class from System.Net namespace to perform HTTP requests.
External Service
ipinfo.io
Relies on the http://ipinfo.io/ip endpoint to retrieve the public IP address.
The method trims whitespace from the returned IP address to ensure clean data storage in the database.

Best Practices

  1. Handle return values: Always check if the returned string is a valid IP address or an error message
  2. Network dependency: Ensure the application has internet connectivity before calling this method
  3. Caching: Consider caching the IP address to avoid repeated external API calls
  4. Alternative services: Implement fallback to alternative IP detection services for better reliability
  • Player - Uses the IP address for player identification
  • Forms - Implements IP retrieval during form initialization

Build docs developers (and LLMs) love