[关闭]
@HUST-SuWB 2018-07-11T09:05:13.000000Z 字数 2659 阅读 363

还计算机网络课程的债

Finlabtech


前言

平常开发过程中可能考虑网络这一块考虑得比较少,毕竟从业务角度看这些属于基础设施和服务,所以在开发过程中都是默认一切正常并且无需任何改动的。但是最近稍微深入一思考就发现很多不太明白的点,感觉在这方面自己的知识体系完全经不起推敲。哎,大学学的计算机网络感觉都还给老师了,罪过罪过。

TCP/HTTP/端口

先贴一段话,看完对我有豁然开朗的感觉。

  1. TCP is the transport layer. By definition, it's responsible for carrying application layer protocols (HTTP in your case) over it. TCP does not run over a port. It is the arbitrator of ports. In other words, when you connect to an HTTP server, you connect on TCP port 80. When you connect to HTTPS, you're connecting over TCP port 443. HTTP and HTTPS can run over any TCP port. 80 and 443 are just the common ones. You can make any application listen on those ports if you want. So yes, you can connect to your server over port 80 using some other protocol instead of HTTP, but only if the server is configured to listen on that port using that other protocol, and only if HTTP or HTTPS are configured to not use those ports (assuming you're running a web server on it

HTTP协议只是跑在应用层之上的协议而已,其依赖更为底层的传输层TCP协议。而我们平常所说的端口都是指的TCP端口,这其中HTTP协议默认使用了80,HTTPS默认使用了443,FTP默认使用了21等等,这都是约定俗成的通用做法而已,并不代表强制只能使用某个端口。
那么问题来了,平常我们用浏览器的时候根本不涉及端口,为什么能正常访问网络服务呢?原因就在于浏览器默认我们使用http协议的时候访问的是80端口,所以如果我们的网络服务提供商使用的默认80端口则可以显式指定端口即可访问。如果一个网络服务提供商偏偏不想使用默认端口,则客户端访问的时候就必须显式指定端口了。而这个时候我们的客户端其实是使用的随机端口在与服务端通信,也就是说客户端通过某个随机端口向服务器的80端口发送请求。
如果我们仔细观察,可以发现当我们在访问某一个网站的时候,敲入www.xxx.com,浏览器有时候会自动的给我们加上http://,有时候又会自动的加上https://,这是为什么呢。下面是解答。

  1. The website can set HSTS header to tell browsers that they should redirect to HTTPS automatically for future requests, or they can register themselves into HSTS preload list, and users can install browser plugins to always load HTTPS based on a white list or even to always try HTTPS first. All of these are opt-in, either the website or the user has to do something to make the browser do this. In its default configuration, without explicit action by the user or the web site, no major browsers would automatically use HTTPS.

IP/域名/DNS

ip就是网络设备的唯一标识,而域名则是方便我们记忆的别名,通过域名定位ip的过程就是dns解析。
一个典型的http请求如下:客户端输入域名,通过DNS将域名解析成为服务器ip,找到代理服务器,因为http协议服务所占用的端口默认为80端口,所以会访问服务器的80端口,然后再通过代理服务器将请求转发到不同的服务器以及端口中。如图域名与端口的对应

完整的DNS解析是一个递归的过程。首先访问根域名服务器,然后是顶级域名服务器,然后是二级域名服务器等等,直到定位到完整域名的ip为止。DNS递归解析

参考文献

https://zhidao.baidu.com/question/689748505970653204.html
https://www.jianshu.com/p/806d0514ec7d
https://bbs.csdn.net/topics/300222170
https://zhidao.baidu.com/question/197096850.html
https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers
https://stackoverflow.com/questions/2957757/how-can-an-application-use-port-80-http-without-conflicting-with-browsers
https://security.stackexchange.com/questions/166204/does-http-redirect-to-https-automatically
https://webmasters.stackexchange.com/questions/71393/how-do-i-link-my-domain-name-with-my-web-server-ip-address-so-that-i-can-use-ssl
https://superuser.com/questions/907220/if-http-runs-on-a-port-does-that-mean-tcp-can-also-run-on-that-port

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注