gpt4 book ai didi

go - ListenSocket ClientSocket Go

转载 作者:数据小太阳 更新时间:2023-10-29 03:37:58 27 4
gpt4 key购买 nike

this article 关于Go Web Server , 有 Listen SocketClient SocketGo ,

我不明白为什么 GoLang 需要两个套接字 Listen Socket, Client Socket但不只是一个 socket ,谁能解释一下它的概念或打个比方?

编辑:我更新我的答案。

最佳答案

也许我误解了图表或者图表画得不是很好,可能是Listen Socket , Client Socket是相同的套接字,如果套接字没有接受来自客户端的连接,它被称为Listen Socket , 接受连接后,重命名为 Client Socket ,只有一个套接字具有不同的阶段和名称。


更新 1:

我找到了一篇关于套接字工作的更好的文章和图表 Here .

在文章的grpah中,很明显当有新的连接时,TCP Server会创建一个新套接字来处理连接,Listen Socket继续监听其他连接。

这是文章中的一段:

The first socket created by a TCP server, via NetSock_Open(), is typically designated a listen socket , and, after the call to NetSock_Listen(), remains open indefinitely, to allow the server to respond to various connection requests. Rather than using this socket to exchange data with requesting clients, the server will create a new socket for each request.


更新 2

由于第一次更新是在 Micrium 上运行,我发现另一个似乎更通用的 TCP 工作指令 Here :

TCP connection flow The following sequence shows the flow of a TCP

connection:

  1. The server creates the listener socket that is waiting for remote clients to connect.
  1. The client issues the connect() socket function to start the TCP handshake (SYN, SYN/ACK, ACK). The server issues the accept() socketfunction to accept the connection request.
  1. The client and server issue the read() and write() socket functions to exchange data over the socket.

Note: There are several SSL APIs that you can use to send and receive data other than the read() and write() socket functions.

  1. Either the server or the client decides to close the socket. This causes the TCP closure sequence (FINs and ACKs) to occur.
  1. The server either closes the listener socket or repeats beginning with step 2 to accept another connection from a remote client.

Note: Normally after the accept() socket function ends, the server dividesinto two processes (or threads). The first process handles theconnection with the client and the second process issues the nextaccept() socket function. Figure 1 shows an example of a TCPconnection:


注意:

  1. 我找到另一个 Socker Programming Tutorial 提及 TCP 中的工作细节。

  2. .NET Framework MSDN ,关于Socket.Accept Method()的解释说Accept synchronously extracts the first pending connection request from the connection request queue of the listening socket, and then creates and returns a new Socket.

  3. 我浏览过 RFC about TCP 在 Update 1 之前,但我没有看到它提到 Listen 使用一个套接字的细节,当 Accept 时它会创建另一个新的套接字。

  4. 也许彻底的方法是研究Go中有关创建套接字和连接的源代码。 ,但我现在不打算这样做。

关于go - ListenSocket ClientSocket Go,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46688738/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com