简体   繁体   English

如何在go中发现客户端正在从tcp缓冲区读取

[英]how to find that the client is reading from tcp buffer in go

I'm starting to use golang for a quite amount of time for a project.我开始在一个项目中使用 golang 相当长的时间。 In my project I have to implement a tcp server which responds to tcp clients.在我的项目中,我必须实现一个响应 tcp 客户端的 tcp 服务器。 The server has to send a number of messages to a client.服务器必须向客户端发送大量消息。 The problem is that when a server writes a message to a client connection, it has to wait until the client has read that message from buffer and then send another message (the server has to wait until the client calls the reader.ReadString('\\n') method).问题在于,当服务器将消息写入客户端连接时,它必须等到客户端从缓冲区读取该消息,然后再发送另一条消息(服务器必须等到客户端调用reader.ReadString('\\n')方法)。

In my server code I wrote:在我的服务器代码中,我写道:

for {
    data := <-client.outgoing
    client.writer.WriteString(data + "\n")
    client.writer.Flush()
}

but the server sends all the messages to client without waiting for ReadString in client.但是服务器将所有消息发送给客户端,而无需等待客户端中的 ReadString。

How to make server wait until the client read a message and then send the other message?如何让服务器等待客户端读取一条消息,然后再发送另一条消息?

I think that either the assignment is ambiguous or you're misinterpreting it and solving the XY problem .我认为要么分配不明确,要么你误解了它并解决了 XY 问题

The short answer is that you can never know whether the client has read a message just by looking at the TCP conversation .简短的回答是,仅通过查看 TCP 对话您永远无法知道客户端是否已阅读消息 You have to implement this "protocol" in your application.你必须在你的应用程序中实现这个“协议”。

Here are a few problems:这里有几个问题:

  1. From your application you don't really have access to what TCP is doing.从您的应用程序中,您实际上无法访问 TCP 正在做什么。 You get a stream on which you can perform I/O.您将获得一个可以在其上执行 I/O 的流。

    • The fact that a write to your stream "succeeds" only means that TCP has agreed to try to transport your stuff and has an independent copy.写入您的流“成功”这一事实仅意味着 TCP 已同意尝试传输您的内容并拥有独立副本。 It doesn't say anything about whether the data has been received and it doesn't even mean the data has been even sent它没有说明数据是否已收到,甚至不意味着数据已发送
    • You may find certain mechanisms to peer into TCP's inner workings (such as ioctl s, SIOCINQ , SIOCOUTQ or various setsockopts): these won't help您可能会发现某些机制可以查看 TCP 的内部工作原理(例如ioctl s、 SIOCINQSIOCOUTQ或各种SIOCOUTQ ):这些都无济于事
  2. Even if you find out what your TCP is doing, this only tells you what the remote TCP is doing .即使你发现你的 TCP 在做什么,这也只能告诉你远程 TCP 在做什么 So if you have full control over your TCP and even see the acknowledgments from the peer, you still don't know what the application is doing .因此,如果您完全控制了您的 TCP,甚至看到来自对等方的确认,您仍然不知道应用程序在做什么 It's very possible the application didn't read the data yet (it might not have requested the data, the TCP might be withholding it in a buffer for some weird reason, the scheduler might not have scheduled the remote process etc.)很可能应用程序还没有读取数据(它可能没有请求数据,TCP 可能出于某种奇怪的原因将它保留在缓冲区中,调度程序可能没有调度远程进程等)

Going back to your question, a way to really know whether the remote application has received your message is to have the remote application tell you.回到您的问题,真正知道远程应用程序是否已收到您的消息的一种方法是让远程应用程序告诉您。 This means you have to restructure your protocol to:这意味着您必须将协议重组为:

  1. Send stuff from the server从服务器发送东西
  2. Wait for a message from the application telling you it received your stuff等待来自应用程序的消息告诉您它收到了您的东西
  3. Send more stuff (because you know from point 2 it's safe to do so)发送更多内容(因为您从第 2 点知道这样做是安全的)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM