Git Product home page Git Product logo

Comments (9)

CyC2018 avatar CyC2018 commented on May 22, 2024

@g10guang 不一定描述地太细,因为整个过程内容很多,比如 HTTP 请求这块,如果要深入下去,缓存,Cookie 这些都需要考虑进去。这个问题主要是说出大概过程吧,如果你希望面试官往某方面深入下去,那可以着重描述那一方面。至于你说的端口问题,如果面试经常问,那确实应该加进去。我先看看材料。

from cs-notes.

CyC2018 avatar CyC2018 commented on May 22, 2024

@g10guang 你说的端口复用,是指 Socket 编程中 SO_REUSEADDR 选项,它利用了
TIME_WAIT 状态下需要等待的那段时间。SO_REUSEADDR 允许在 Socket1 处于该状态的情况下,Socket2 可以使用 Socket1 的端口号。

from cs-notes.

g10guang avatar g10guang commented on May 22, 2024

比如我配置服务器的防火墙信息的时候,我只开放了 80 端口,那为什么还能够同时允许这么多客户进行连接。假如客户端连接服务器后使用不同的端口,应该会被防火墙给屏蔽掉?
@CyC2018 我写了一个程序测试了使用多个客户端同时连接同一个服务端,在客户端得到的远程端口都是 8080。
我还有一个比较迷的问题:
前提:多个客户能够同时连接服务器同一个端口没什么问题。
如果服务端分配另一个端口给客户端,而不是客户端一开始就使用的 80 端口(以HTTP为例),那么岂不是要先四次挥手,然后又三次握手?我认为这是一个比较耗时、耗资源的方案。

from cs-notes.

CyC2018 avatar CyC2018 commented on May 22, 2024
  • 第一个问题,因为你发送的请求还是通过 80 端口发送的,你在浏览器输入的网址在发送时默认会加上 80 端口。服务器端有监听端口和连接端口两种,监听端口用来监听连接,连接之后会为每个客户端分配一个连接端口。即使服务器为客户端分配了不同的端口,客户端发送的 Socket 中的目的端口还是监听端口,因此即使只开放了 80 端口依然可以连接。服务器端通过维护一个映射表来确定每个客户端的连接口段,客户端感受不到服务器端为它分配了不同的端口号。我对 Socket 编程不熟悉,如果描述的有错误,请指正!

  • 第二个问题:有些服务器默认就是监听 8080 端口号的。

  • 第三个问题:是很耗时,因此现在 HTTP 都是用长连接。

from cs-notes.

g10guang avatar g10guang commented on May 22, 2024

@CyC2018
如果客户端感知不到服务端分配的端口,那端口又不能通过防火墙与客户端进行通信。那么该端口存在的意义是什么呢?

from cs-notes.

CyC2018 avatar CyC2018 commented on May 22, 2024

@g10guang 监听端口只是用来监听客户端连接,如果一个客户端连接之后,服务器端还是使用监听端口和客户端进行通信,那么就无法监听新的连接了。并且服务器都需要支持高并发,因此也就需要非常多的连接进程,每个连接进程都需要分配一个端口号。

from cs-notes.

g10guang avatar g10guang commented on May 22, 2024

@CyC2018 https://stackoverflow.com/a/11344212/7159205 我更加赞同他的观点,希望你阅读以下。

from cs-notes.

CyC2018 avatar CyC2018 commented on May 22, 2024

@g10guang 学习了,是我理解的不够,抱歉!没有重新分配端口,都是使用 80 端口号。

from cs-notes.

CyC2018 avatar CyC2018 commented on May 22, 2024

@g10guang Web 页面请求过程已修改。

Web 页面请求过程

from cs-notes.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.