dph23577 2017-10-11 12:55
浏览 50
已采纳

ListenSocket ClientSocket转到

In this article about Go Web Server, there're Listen Socket and Client Socket in Go,

I can't understand why GoLang need two sockets Listen Socket, Client Socket but not just one socket, can anyone explain its concept or give a metaphor?

EDIT : I update my answer.

  • 写回答

1条回答 默认 最新

  • douhui9192 2017-10-11 13:20
    关注

    Maybe I misunderstanding the graph or the graph isn't draw very well, possibly Listen Socket, Client Socket are same socket, if the socket hasn't accept connection from client, it's called Listen Socket, and after it accept the connection, it's renamed to Client Socket, there's only one socket with different stage and name.


    UPDATE 1:

    I find a better article and graph about Socket Working Here.

    In the grpah of article, it's clear when there's new connection, TCP Server will create a new socket to handle the connection, and the Listen Socket continuing listening for other connections.

    Here's a paragraph in the article:

    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.


    UPDATE 2

    Since first update is working on Micrium, I find another seems more general TCP working instuction 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.

    2. 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.

    3. 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.

    2. 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:


    Note:

    1. I find another Socker Programming Tutorial mention about working detail in TCP.

    2. And In .NET Framework MSDN, the explanation about Socket.Accept Method() says 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. I have skimmed RFC about TCP before Update 1, but I didn't see it mention the detail that Listen use one socket, and when Accept it'll create another new Socket.

    4. Maybe the thorough way is to research the Source Code about Create Socket and Connection in Go, but I'm not consiedring to do it now.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c