I’m learning the socket programming with Java.
I connect to a website using this statement:
s = new Socket("www.nba.com", 80);
When i debug the application and look at the content of s, i see:
Socket[addr=www.nba.com/2.21.246.97,port=80,localport=7846]
1) I want to know where this localport 7846 comes from and what it exactly is.
2) if the IP address of the website is 2.21.246.97, why can’t i connect to the website by just typing 2.21.246.97 in the address field of my browser ?
Thanks
It is a local socket port number. It is usually assigned by the system.
See What is a Socket?.
As to the second question:
I assume this IP address is what you get by your DNS server when you lookup
www.nba.com(mine is different). The problem might be that the HTTP server at this address serves multiple virtual hosts and/or it cares about theHostheader your browser sends. In your case it is the IP address instead ofwww.nba.com.