Is port no. necessary in networking through URL class?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Zaid181294
    New Member
    • Mar 2013
    • 7

    Is port no. necessary in networking through URL class?

    In the following code:

    Code:
    public class URLDemo {
           public static void main(String args[])throws Exception {
                URL u=new URL("http://www.google.com");
                System.out.println("Port No : "+u.getPort());
              }
           }
    I haven't explicitly specified the Port No. in the URL above, but is it so "that port nos. ain't necessary"
    Also the above program gives me the output "-1" so
    1. Is there a default port automatically assigned by Java?(i heard it is "80")
    2. If YES then why did the output render(display) "-1".
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    The default port is set according to the protocol. You can print out u.getDefaultPor t() to see what the default is.
    You should read your earlier thread on sockets to understand how ports are assigned.

    Comment

    • Zaid181294
      New Member
      • Mar 2013
      • 7

      #3
      So what if I specify the port 23 and the protocol as HTTP
      as in case of,
      Code:
      URL u=new URL("http://www.osborne.com:23/index.html");
      Sorry to say but i was enable to get your point in the previous thread!!!

      Comment

      • Zaid181294
        New Member
        • Mar 2013
        • 7

        #4
        And you didn't explain me "why the program outputs -1"

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          You need to read up on basic computer networking first before looking at the Java API that uses it otherwise everything won't make sense.

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            Post #1
            1. The default port for http is 80.
            2. You did not explicitly define a port. When you don't explicitly define a port, it returns -1.

            Post #5
            You can specify the port like that in your URL if you want. It won't connect to anything because the server probably uses port 80. But you can explicitly define the port that way.

            Comment

            • Zaid181294
              New Member
              • Mar 2013
              • 7

              #7
              So if I don't specify the port no. "The Output will be -1. but the port that will be used to access the resource will be 80 i.e HTTP". Am i correct?

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                As I said above, the defaultPort will still be assigned (by the HTTP handler) to 80 whether you specified a port or not. You still don't seem to understand the difference between those two.

                Comment

                Working...