By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,654 Members | 1,519 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,654 IT Pros & Developers. It's quick & easy.

What is the difference between getPort() and getLocalPort() of the Socket class?

P: 7
Some may say that
Expand|Select|Wrap|Line Numbers
  1. getLocalPort()
returns the "Local Port" and
Expand|Select|Wrap|Line Numbers
  1. getPort()
returns the "remote Port" but what i find interesting yet ironically confusing is that how can be there two ports associated with the same Socket?
Mar 26 '13 #1
Share this Question
Share on Google+
4 Replies

Expert Mod 10K+
P: 12,421
Because you connect to a port on the remote computer from a port on your computer. You connect to their port 80 from your port 8009 for example. Pretty much every web server uses port 80. If you have multiple websites loaded, they can't very well all use your port 80 as well. In fact, none of them use your port 80. It's reserved for if you want to run a web server yourself.
Mar 26 '13 #2

P: 7
But isn't a Socket like an end point between two communicating entities.for instance,


So the question arises that how can we have two end-points in a connection.( I seriously can't grab this one!!!)
Mar 27 '13 #3

P: 13,264
Ports exist on computers not inside some socket object. A computer (whether client or server) always needs to be able to open a port in order for it to communicate with another computer.
The client computer and the server computer do not both have to open the same port number to be able to communicate. The client can use port X to communicate out but be connecting to port Y of the computer it is communicating with (if that computer is using port Y to communicate out).
Mar 27 '13 #4

Expert Mod 10K+
P: 12,421
Your chart is wrong. It's more like this
Expand|Select|Wrap|Line Numbers
  1. ---------------------------socket-----------------------------
  2. | local socket address    protocol     remote socket address |
  3. |          TCP         |
  4. --------------------------------------------------------------
  6. local socket----------------remote socket
Yes, a socket is an end-point for data transmission. But each computer creates their own socket.
Mar 27 '13 #5

Post your reply

Sign in to post your reply or Sign up for a free account.