Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

difference between Ports ?

 
Udit Manektala
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Udit Manektala
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
vu lee
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic