草庐IT

go - ListenSocket ClientSocket Go

coder 2024-07-12 原文

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() socket function 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 divides into two processes (or threads). The first process handles the connection with the client and the second process issues the next accept() socket function. Figure 1 shows an example of a TCP connection:


注意:

  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/

有关go - ListenSocket ClientSocket Go的更多相关文章

  1. ruby-on-rails - Textmate 'Go to symbol' 相当于 Vim - 2

    在Railcasts上,我注意到一个非常有趣的功能“转到符号”窗口。它像Command-T一样工作,但显示当前文件中可用的类和方法。如何在vim中获取它? 最佳答案 尝试:helptags有各种程序和脚本可以生成标记文件。此外,标记文件格式非常简单,因此很容易将sed(1)或类似的脚本组合在一起,无论您使用何种语言,它们都可以生成标记文件。轻松获取标记文件(除了下载生成器之外)的关键在于格式化样式而不是实际解析语法。 关于ruby-on-rails-Textmate'Gotosymbol

  2. ruby-on-rails - 用于 Ruby 的 vim 中的全局 "Go to definition"? - 2

    自97年以来我一直在使用vi/vim进行各种快速编辑和管理任务,但最近才考虑使用它来替换Netbeans作为我选择的ruby​​编辑器。我发现一件事在Netbeans和Eclipse中非常有用的是Ctrl+Click“转到定义”功能,您可以在其中按住Ctrl键并单击一个类或方法,然后它将带您了解定义。现在,我玩过丰富的ctags和rails.vim,而且很接近,但没有雪茄。这就是我想要的:默认情况下在Netbeans和Eclipse中,您可以在本地rails中按住ctrl并单击本地方法或类项目,但你也可以ctrl+click定义在gems或用Ruby编写的系统库。以Netbeans为例

  3. ruby-on-rails - Rails 上的 ruby : How to have multiple submit buttons going to different methods (maybe with with_action? ) - 2

    这个问题在这里已经有了答案:HowdoIcreatemultiplesubmitbuttonsforthesameforminRails?(7个答案)关闭9年前。所以..'save'%>'library'%>然后在我的Controller中:with_actiondo|a|a.savedoenda.librarydoendend问题是只有一个操作被调用...两个submit_tags调用相同的操作...知道为什么吗?或者我如何获得两个按钮以将表单提交给两种不同的方法?

  4. DiFi: A Go-as-You-Pay Wi-Fi Access System 精读笔记(三) - 2

    IV.SYSTEMIMPLEMENTATIONWeadoptmodulardesignfollowingtheintegrationofblockchain.Itbringsmoreflexibilitybyseparatingtheimplementationofdifferentfunctionalities,sowecouldleveragetheadvantagesoftheblockchain-basedsmartcontractwhilereducingoverhead.Figure3illustrateshowdifferentmodulesareinvolvedintheint

  5. go-templates - 如何根据表达式有条件地在 Go 模板中设置变量,如果不使用 if 语句包装可能会导致错误 - 2

    问题我该如何做这样的事情:{{$use_ssl:=(ne$.Env.CERT_NAME"")}}其中$.Env.CERT_NAME可能为零/未定义。如果它是零,它给出这个错误:at:errorcallingne:invalidtypeforcomparison注意:我无法控制传递给Go模板的对象,因此必须完全在模板本身内解决这个问题。我尝试过的我试图通过首先检查它是否为非空来变通:{{$use_ssl:=(($.Env.CERT_NAME)&&(ne$.Env.CERT_NAME""))}}但它给出了这个错误:unexpected"&"inoperand所以我切换到这个,这在语法上是允

  6. Go time与string的相爱相杀 - 2

    time包与string包可以说是在Go语言的开发中常用的两个包实际开发过程中(例如web开发)经常会遇到time类型与string类型的交互,计算比较等场景首先来了解GO语言里非常浪漫的一个点,即2006-01-0215:04:05,GO语言诞生的时间,通常用来做时间的格式化time转stringt:=time.Now()//当前时间timeLayoutStr:="2006-01-0215:04:05"t.Format(timeLayoutStr)//返回值为string,可以用一个值来接收它上述例子中,将time类型t转换为string类型,并格式化为年-月-日时-分-秒,这里的格式化是可

  7. javascript - 使用哈希 url 卡在 Angular ui-router State.go 上? - 2

    我正在研究ui-router。我有一个状态:.state('new-personal-orders',{url:'/orders/new-personal-orders/:catId?',template:''})在我的Controller中,我可以使用$state.go('new-personal-orders',null,{reload:true})在Html文件中我有一个anchor标记:Link如果标签被点击,状态就会改变,'new-personal-orders'变成当前状态,在url中有尾随散列。然后url看起来像:http://localhost:3000/orders/

  8. 十分钟 Javascript : What is going on in this example code illustrating lazy scoping? - 2

    我一直在重读SpencerTipping的优秀作品JavascriptinTenMinutes在这个使用惰性作用域创建语法宏的示例中,我终究无法弄清楚发生了什么:varf=function(){return$0+$1};varg=eval(f.toString().replace(/\$(\d+)/g,function(_,digits){return'arguments['+digits+']'}));g(5,6);//=>11(exceptonIE)特别是,$0和$1正在被一个函数定义取代——那个函数是如何被计算的?(大概是通过eval(),但我没有看到)。函数中单个下划线参数的用

  9. javascript - 如何用参数做 $state.go() ? - 2

    我已经完美地初始化了$stateProvider并且我正在将所有这些状态与ui-sref一起使用。效果很好。用户按下按钮并通过$stateProvider进入编辑页面。在这个页面上,我有一个执行$http请求的表单:this.pushData=function(data){$http.post('/data/'+$stateParams.dataId+'/otherdata',JSON.stringify({id:otherdata.id,name:otherdata.name}),configAuth).then(functionsuccess(response){varaddedD

  10. Go第 15 章 :单元测试 - 2

    Go第15章:单元测试15.1先看一个需求在我们工作中,我们会遇到这样的情况,就是去确认一个函数,或者一个模块的结果是否正确,如:15.2传统的方法15.2.1传统的方式来进行测试在main函数中,调用addUpper函数,看看实际输出的结果是否和预期的结果一致,如果一致,则说明函数正确,否则函数有错误,然后修改错误代码实现:15.2.2传统方法的缺点分析不方便,我们需要在main函数中去调用,这样就需要去修改main函数,如果现在项目正在运行,就可能去停止项目。不利于管理,因为当我们测试多个函数或者多个模块时,都需要写在main函数,不利于我们管理和清晰我们思路引出单元测试。->testin

随机推荐