1,105,450 Community Members

Getting the external(public) IP of a machine

Member Avatar
neutralfox
Junior Poster
124 posts since Mar 2009
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
JamesCherrill
... trying to help
10,387 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 
Member Avatar
neutralfox
Junior Poster
124 posts since Mar 2009
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
neutralfox
Junior Poster
124 posts since Mar 2009
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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();}
		
    	}
    
}
Member Avatar
neutralfox
Junior Poster
124 posts since Mar 2009
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Okie, thanks dude , its working.

The problem was with the link, I ve changed it to

http://whatismyip.com/automation/n09230945.asp

Now can you help me to set the timeout please.

Question Answered as of 4 Years Ago by JamesCherrill
Member Avatar
JamesCherrill
... trying to help
10,387 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

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
Member Avatar
kumarprabhug
Newbie Poster
1 post since Jun 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-1
 

we want the if any one login then that system address.
how to get please help me.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article