Hello friends,
I am having a small problem, i have already designed a chat application, the chat works fine over a local network but now I want to use it over the internet. Logically the program should but what happened is that to get the IP I have used

clientInformation.clientIPAddress = clientsoc.getInetAddress().getHostAddress();

and it gives only the local and I want the public IP of that machine. Can someone help me please.

I just want to get the external IP in a textbox!!

Thanks a lot in advance for your help.
Regards
Mike

Hello, thanks for the link, I have tried to run the code but I've got the following error:


Error:
C:\GetIP.java:8: cannot find symbol
symbol : class HttpURLConnection
location: class GetIP
java.net.HttpURLConnection Conn = (HttpURLConnection)URL.openConnection();
^
C:\GetIP.java:17: cannot find symbol
symbol : method readline()
location: class java.io.BufferedReader
System.out.print("Your IP address is " + Br.readline());
^
2 errors

Process completed.


Thanks a lot for the answer.

Hello,
I've modified the code and no more errors, but still I didn't get the IP, the program keeps running and nothing appears.

import java.net.*;
import java.io.*;
import javax.swing.JOptionPane;
public class GetIP {

    public GetIP() {
    

    }
        
        
        
         public static void main(String args[]) {
    	try{
    	
    	
    	java.net.URL URL = new java.net.URL("http://www.whatismyip.org/");
 
		java.net.HttpURLConnection Conn = (HttpURLConnection)URL.openConnection();
		 
		java.io.InputStream InStream = Conn.getInputStream();
		 
		java.io.InputStreamReader Isr = new java.io.InputStreamReader(InStream);
		 
		java.io.BufferedReader Br = new java.io.BufferedReader(Isr);
		 
		System.out.print("Your IP address is " + Br.readLine());
		JOptionPane.showMessageDialog(null, "IP is: " + Br.readLine() );
		
    	}catch(Exception e){e.printStackTrace();}
		
    	}
    
}

I've not tried this myself, but the API doc implies you should be able to

java.net.HttpURLConnection Conn = (HttpURLConnection)URL.openConnection();
Conn.setReadTimeout(2000); // 2,000 miliiseconds 
java.io.InputStream InStream = Conn.getInputStream();
// etc

This code displays the External IP Address or the Public IP Address of the Sytem connected to the Internet. However, it gives an Error if you're system isn't connected to the Internet. You can also see this** Java Program To Display Local Host IP Address**.

import java.net.*;
import java.io.*;
import java.util.*;

class SystemIPAddress 
{
    public static void main(String[] arguments) 
        {
            String systemipaddress = "";
                try 
            {
                        URL url_name = new URL("http://bot.whatismyipaddress.com");
                        BufferedReader sc = new BufferedReader(new InputStreamReader(url_name.openStream()));
                        systemipaddress = sc.readLine().trim();
                        if (!(systemipaddress.length() > 0)) 
                    {
                            try 
                        {
                                    InetAddress localhost = InetAddress.getLocalHost();
                                    System.out.println((localhost.getHostAddress()).trim());
                                    systemipaddress = (localhost.getHostAddress()).trim();
                            } 
                        catch(Exception e1) 
                        {
                                    systemipaddress = "Cannot Execute Properly";
                            }
                        }
                }    
            catch (Exception e2) 
            {
                        systemipaddress = "Cannot Execute Properly";
                }
                System.out.println("\nYour IP Address: " + systemipaddress +"\n");
        }
}

Source of the Program: http://www.codingalpha.com/find-public-ip-address-in-java-programming

This question has already been answered. Start a new discussion instead.