How to get the public IP of a connected user via socket?

Asked

Viewed 963 times

7

I need to get the IP address of a user who will remotely connect to my program, and list it, the program itself is a chat where only the server will store the IP address of the users. Client connects via socket, how to proceed to capture the IP?

  • How is the user connecting with your program? Via sockets, HTTP, some other way? The answer will depend on this.

  • Whoa, I forgot to clear that up! I already edited the question.

2 answers

8


When you get the socket connected to the client, use the method getInetAddress of the socket class, which it will give you the IP (among other information) about the client that is connected to your program. With the method getAddress class InetAddress you can get client IP

public static void main(String[] args) {
    int portNumber = 8000;

    try {
        ServerSocket serverSocket = new ServerSocket(portNumber);
        Socket clientSocket = serverSocket.accept();
        InetAddress address = clientSocket.getInetAddress();
        byte[] ip = address.getAddress();
        System.out.print("Client IP: ");
        for (int i = 0; i < ip.length; i++) {
            System.out.print(ip[i] & 0xFF);
            if (i < ip.length - 1) System.out.print(".");
        }
        System.out.println();
        clientSocket.close();
        serverSocket.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
  • this method I know, for me it only took the local ip.

  • 1

    No, it returns the remote IP.

  • Carlos, just out of curiosity, has some reason not to use the method getHostAddress?

  • 2

    @Marcorhayden Make sure you are calling the method getInetAddress client and not server. And also that both client and server are not on the same IP. In addition, it is important to note that if you intend to provide the connection via socket to third parties or publicly, it is good not to trust absolutely the IP number received, for example for security, as it may have been tampered with by someone malicious.

  • 1

    @utluiz no, getHostAddress would also work, depending on how you want to store the IP. In my example above it would be even better (since the code only prints the IP), but if you want to have more information about the IP (e.g., if the address is local), it is easier to get this information via the address raw.

  • Well, this was the method I used, I asked the question totally wrong... kkkkkkk what I want to know is different, I need to capture the public ip of the user before he establishes any kind of connection.. need to implement an ip capture in my program.

Show 1 more comment

-4

this tbm works

<?php

$clientIP = $_SERVER['HTTP_CLIENT_IP'] 
    ?? $_SERVER["HTTP_CF_CONNECTING_IP"] # when behind cloudflare
    ?? $_SERVER['HTTP_X_FORWARDED'] 
    ?? $_SERVER['HTTP_X_FORWARDED_FOR'] 
    ?? $_SERVER['HTTP_FORWARDED'] 
    ?? $_SERVER['HTTP_FORWARDED_FOR'] 
    ?? $_SERVER['REMOTE_ADDR'] 
    ?? '0.0.0.0';

# Earlier than PHP7
$clientIP = '0.0.0.0';

if (isset($_SERVER['HTTP_CLIENT_IP'])) {
    $clientIP = $_SERVER['HTTP_CLIENT_IP'];
} elseif (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
    # when behind cloudflare
    $clientIP = $_SERVER['HTTP_CF_CONNECTING_IP']; 
} elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $clientIP = $_SERVER['HTTP_X_FORWARDED_FOR'];
} elseif (isset($_SERVER['HTTP_X_FORWARDED'])) {
    $clientIP = $_SERVER['HTTP_X_FORWARDED'];
} elseif (isset($_SERVER['HTTP_FORWARDED_FOR'])) {
    $clientIP = $_SERVER['HTTP_FORWARDED_FOR'];
} elseif (isset($_SERVER['HTTP_FORWARDED'])) {
    $clientIP = $_SERVER['HTTP_FORWARDED'];
} elseif (isset($_SERVER['REMOTE_ADDR'])) {
    $clientIP = $_SERVER['REMOTE_ADDR'];
}

echo "IP do cliente: ". $clientIP;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.