• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Getting port no of server at which web application is running

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi team ,

I have a task in java for getting the IP address and port no of server at which web application is running .I am able to find the solution of getting the IP address .But I am facing difficulties for finding the port no

       InetAddress ip = InetAddress.getLocalHost();
       hostname = ip.getHostName();
       // serverName= ip.getHostName();
       serverName = ip.getHostAddress();

       // Socket socket =new Socket();
       // serverName=(((InetSocketAddress)
       // socket.getRemoteSocketAddress()).getAddress()).toString().replace("/","");
       System.out.println("Your current IP address : " + ip);
       System.out.println("Your current Hostname : " + hostname);
       System.out.println("Your current Servername : " + serverName);
                 
Can anyone suggest sample piece of code for getting the port no of server at which web application is running.The web application is implemented un java.
 
Saloon Keeper
Posts: 7098
165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So this piece of code would be running on the same machine as the web app for which you're trying to figure this out? Would it be part of that web app? If it's not part of that web app, the task is impossible in the general sense, since there could be multiple HTTP processes running on the machine. If, OTOH, it IS part of that web app, surely it would be easier to just set a global value somewhere at startup which your piece of code could check.

Maybe I'm missing something, as it's not clear to me what the purpose of this is; maybe you can elaborate on that.
 
Marshal
Posts: 74004
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

We have moved you to our sockets forum.
 
Saloon Keeper
Posts: 24295
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's an exercise in futility. A webapp server can listen on many ports, many interfaces and via many protocols.

For example, Tomcat will listen on all interfaces (including localhost) and in its out-of-the-box form, it listens on port 8080 (http), 8443 (https), 8009 (proxy) and 8005 (control), plus I think one other but can't remember. By adding/altering Connector elements in Tomcat's [tt]server.xml[/i] you can add more ports and/or change the existing port numbers. And, indeed, it's not uncommon to do so.

You can get the port number on which a given HttpServletRequest was received by using its API methods, but that only applies to that one request. If there were additional ports also listening, you wouldn't be able to tell without querying Tomcat's internal bean structure. And, of course, that would be different logic than any other server such as WebSphere would require.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic