Java套接字 - 本地端口 [英] Java Socket - Local Port

查看:98
本文介绍了Java套接字 - 本地端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习使用Java的套接字编程。
我使用以下声明连接到网站:

I'm learning the socket programming with Java. I connect to a website using this statement:

s = new Socket("www.nba.com", 80);

当我调试应用程序并查看s的内容时,我看到:

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)我想知道这个本地端口7846来自何处以及它究竟是什么。


2)如果网站的IP地址是2.21.246.97,为什么我只能在浏览器的地址栏中输入2.21.246.97才能连接到网站?

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.

参见什么是套接字?


在客户端:客户端知道主机名运行服务器的
机器和服务器
监听的端口号。要发出连接请求,客户端会尝试与服务器的计算机和端口上的服务器进行
集合。
客户端还需要向服务器标识自己,以便绑定到在此连接期间将使用的
本地端口号。 这是系统通常分配的

关于第二个问题:

我认为这个IP地址是您在查找 www.nba.com 时由DNS服务器获得的(我的是不同)。问题可能是此地址的HTTP服务器服务于多个虚拟主机和/或它关心浏览器发送的主机标头。在您的情况下,它是IP地址而不是 www.nba.com

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 the Host header your browser sends. In your case it is the IP address instead of www.nba.com.

这篇关于Java套接字 - 本地端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆