aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes port in URL object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "port in URL object" Watch "port in URL object" New topic
Author

port in URL object

naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

In java.net URL class docs it is specified that
"If the port is not specified in URL, the default port for the protocol is used instead.
the default port for http is 80."


but when i crete a URL object for www.degraeve.com/reference/urlencoding.php. and try to find which port is been used
using getPort(). I found that getPort() returns -1 which is a case when port is not set.



My confusion:
1. when port is not set or specified , Shouldn't getPort() return default port(80) since no port is specified in www.degraeve.com/reference/urlencoding.php? why it returns -1.

2. getPort() says that it returns -1 when port is not set. where to set the port ? Is it set by remote machine or in the URL ?

regards
naveen
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You have started with the question "Which port is being used?", but you haven't looked for the answer to that question. And besides, you already know the answer because you quoted what the API documentation specifically says about that. But instead you have substituted an entirely different question about setting the port. This question does not lead you to the answer to the question "Which port is being used?" -- at least not directly.

If getPort() returns -1, then as you already know, that means the port has not been set. And if you examine the API, you'll see that the only way for you to set the port is by specifying it in the constructor in the URL. And therefore if getPort() returns -1, that means you didn't specify the port in the URL. And the quote you posted from the class docs then tells you what you already knew was the answer.

Now if your question was meant to ask how you could find out that (for example) 80 was the default HTTP port via Java code, then I recommend the getDefaultPort() method.
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

after reading your reply i understand this.(* Please correct me if wrong anywhere).

Since i haven't set the port in URL constructor , that's why getPort() had returned the -1. If port is set to the 85 , getPort() will return the 85. Ok

but in docs it says that if port is not specified in URL then (HTTP )port 80 will be used by default. What about that?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41489
    
  53
Why are you saying "but in docs...", as if there were some kind of mismatch? Obviously, calling setPort explicitly would override the -1 value that causes the default port to be used.


Ping & DNS - my free Android networking tools app
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

Ulf Dittmer wrote:.................... the -1 value that causes the default port to be used.


Is that mean -1 is stand for the default port 80?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41489
    
  53
No, it means that no port has specified. You already know which port will be used in that case.
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

ok. So when does HTTP port 80 will be used as default ?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41489
    
  53
Port 80 will be used as the default if 1) ... and 2) ...

Can you fill in the two conditions? At least one has been mentioned a couple of times in the previous posts.
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

1) when no port is specified in URL constructor.
2) ?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41489
    
  53
#1 - not quite: when no port has been specified. Specifying it in the URl constructor is one way, but you could also use the setPort method

#2 - when the protocol being used is HTTP. Other protocols have other default ports.
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

ok. when HTTP protocoal is used port 80 is used as default.
then why getPort() have not returned 80 in my case?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41489
    
  53
Why would it return 80 if that port has not been specified? Don't read anything into the javadocs that isn't written in them - they say nothing about returning the default port. That's what the getDefaultPort method is for.
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

Thanks for keeping the patience .
The logic behind previous post is that "when no port is specified , then default port should be used".but it turns out that it is not the case

when a request is made for particular resource on internet using URL object. Some port must have to be used through which data can be transmitted. Isn't it ?
if yes then which port is used in following case
URL url = new URL("http://www.degraeve.com/reference/urlencoding.php");
System.out.println("port = " + url.getPort()); // -1


if not please clear what i'm missing here.



Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Yes, that's correct. Some port has to be used. And the quote you included in your very first post:

"If the port is not specified in URL, the default port for the protocol is used instead.
the default port for http is 80."


tells you which port will be used. You aren't missing anything. You are just making the incorrect assumption that the getPort() method tells you which port will be used. It doesn't do that, and its documentation doesn't say it will do that.
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

It means that port 80(default) is used. but as the docs for getPort() doesn't says anything about that. So getPort() does not return that port(80).
And since i have not set any port in the URL ,that's why getPort() returns -1.

Is that a whole story ?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18709
    
  40

naveen yadav wrote:It means that port 80(default) is used. but as the docs for getPort() doesn't says anything about that. So getPort() does not return that port(80).
And since i have not set any port in the URL ,that's why getPort() returns -1.

Is that a whole story ?



I don't think that the URL class has a fixed default port -- meaning that the default port is specified by the URLStreamHandler (the protocol handler). And it just so happens that the default port for the built in handler for http is set for port 80. If you configure the URL for other protocols, such as ftp or https; or if you use your own stream handler, you can have a default port that is not port 80.

Regardless, if you know that the port hasn't been set, you can always ask for the default port (from the handler) instead....



Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

Paul Clapham wrote: You are just making the incorrect assumption that the getPort() method tells you which port will be used. It doesn't do that


and in docs

public int getPort() Gets the port number of this URL.

Returns:the port number, or -1 if the port is not set


i'm not saying that getPort() tells which port will be used , But it does return the port number which was used as a component of a URL object .
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384


Paul Clapham wrote:................. You are just making the incorrect assumption that the getPort() method tells you which port will be used. It doesn't do that, and its documentation doesn't say it will do that.


i guess Paul was right. I was just reading too much in to the definition of getPort() and concluding it otherwise. my badwas i was making assumption that getPort() return the port which ever port is used even the default one which is not the case. getPort() returns the port which is explicitly and if not then returns -1. It does not return the default one in any case even when default port is used. There is a getDefaultPort() to return the default port.

Thanks everybody.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: port in URL object