my dog learned polymorphism*
The moose likes Servlets and the fly likes difference between Ports ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "difference between Ports ?" Watch "difference between Ports ?" New topic
Author

difference between Ports ?

Udit Manektala
Ranch Hand

Joined: Sep 17, 2006
Posts: 30
System.out.println("getRemotePort: " +request.getRemotePort());
System.out.println("getLocalPort: " +request.getLocalPort());
System.out.println("getServerPort: " +request.getServerPort());

Output:
getRemotePort: 2343
getLocalPort: 8080
getServerPort: 8080

1.Since getRP and getLP are the same it means that the request was sent on the port 8080 of the Server and since it was the only request, the Server let it remain on port 8080 \ Forwarded it again to port 8080 ?

2. getRemotePort is 2343 this means that its the Port Firefox sent it from ?
Even thoug I enetered 8080 in thwe browser address to Tomcat. Where does Firefox get this 2343 port number from ?
Dave Wingate
Ranch Hand

Joined: Mar 26, 2002
Posts: 262
Remote port 2343 is the port that your browser used for communication. Your browser doesn't need to use the same port as the server it's communicating with. Moreover, your browser typically won't use the same port as the server with which it is communicating, since your computer may be running both web browser and web server.


Fun programming etcetera!
Udit Manektala
Ranch Hand

Joined: Sep 17, 2006
Posts: 30
This discrepancey between the browser's port and the server's port is exactly what confuses me. I mean, what is the browser doing on port 2432, listening or sending to ? If its listening, then exactly what is it listening to ?
Dave Wingate
Ranch Hand

Joined: Mar 26, 2002
Posts: 262
The easiest way to understand what's going on is to proffer that an ip address is a way to find a computer and that a port number is a way to find a particular application on that computer. If you've ever lived in an apartment, you probably had an address like 123 Fake Road, #3. Your web browser has a similar apartment-style address: 127.0.0.1:2343. In the same way that the post master tells you what your apartment address is, your browser is assigned an ip address and a port number by the operating system. When your browser first attempts to communicate with a web server, it obtains both an ip address and a port number from the operating system and then says 'hey remote server, please send me file 123.txt ... oh, and by the way, the my address is 127.0.0.1:2343'

2343 is a port that your browser uses for communicating with other servers. If you and I both live in apartments and wish to send each other snail mail, there's no requirement that we both live in apartments with the same number. You can live in apt. # 2343 and I can live in apt. # 8080, and we can still send each other mail. The metaphor applies to your web browser and the remote web server ... i.e. they need not use the same port number to communicate.
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
When browser sends a socket request , it makes request to port 8080 but it local port is something else . So its ok when you get 2343 as you remote port as that port would be remort from the servlets prospective .But the strange thing is server port and local port should not be same.The server / listening socket as soon as gets a request , it accepts it and returns a seperate port for further communication with client and again goes back to listening for client request.

This seems somewhat to be strnage to me.


Rahul Bhattacharjee
LinkedIn - Blog
vu lee
Ranch Hand

Joined: Apr 19, 2005
Posts: 189
But the strange thing is server port and local port should not be same.The server / listening socket as soon as gets a request , it accepts it and returns a seperate port for further communication with client and again goes back to listening for client request.

This seems somewhat to be strnage to me.


Suppose this is the case, then while receiving the response, the client getRemotePort() would NOT return port 8080. But I don't think it is the case since I could programatically use the same HttpConnection to request different file from the web server.
[ March 26, 2007: Message edited by: vu lee ]
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
I still not convinced.
As HTTPUrlConnection is just another HTTP client.So when it makes a request to a URL , say http://rahul:8090/ranch/cool.jsp
In this case also the HTTPUrlConnection would try to connect to host rahul at port# 8090 , as soon as the server accepts the request it assigns another port for further communication with client(Done at the server end).Henceforth client would use the new port for further communication.

When I get time I would try this out and post.Netstat would be a useful tool to see the current port in use.
Jaime M. Tovar
Ranch Hand

Joined: Mar 28, 2005
Posts: 133
You get a strange port because it is the port assigned by the OS in your client. It will usually take the next available greater than 1024. The fixed port is the server port, that is because as a client you need o know where to ask for a service. The server doesn�t needs to know in advance from where will the request come. That�s because when the request arrives it will have the port to which the server must reply.

When the client sends the request the server is listening. Then the server begins with its stuff but the connection is still up because the client is waiting for a response to arrive. When the response arrives the client can close the connection or change it to a waiting status if it is probable to use it again.


She will remember your heart when men are fairy tales in books written by rabbits.<br /> As long as there is duct tape... there is also hope.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42293
    
  64
Reading the javadocs it sounds to me like the two methods getServletPort and getLocalPort should return the same value, with getLocalPort being the newer version that got introduced in Servlet API 2.4 along with getLocalAddr and getLocalName. If there actually is a difference, it may be in the case of port forwarding.


Ping & DNS - my free Android networking tools app
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: difference between Ports ?