go - 多个串行请求导致缓冲区为空

标签 go tcp binary byte

在osx上的localhost上运行的第一个TCP连接始终会正确解析发送给它的二进制文件。后续请求丢失二进制数据,仅看到第一个字节[8]。我如何无法设置阅读器?

package main

import (
        "fmt"
        "log"
        "net"
        "os"
        "app/src/internal/handler"

        "github.com/golang-collections/collections/stack"
)

func main() {
        port := os.Getenv("SERVER_PORT")

        s := stack.New()

        ln, err := net.Listen("tcp", ":8080")
        if err != nil {
                log.Fatalf("net.Listen: %v", err)
        }

        fmt.Println("Serving on " + port)

        for {
                conn, err := ln.Accept()
                // defer conn.Close()
                if err != nil {
                        log.Fatal("ln.Accept")
                }
                go handler.Handle(conn, s)

        }
}
package handler

import (
        "fmt"
        "io"
        "log"
        "net"

        "github.com/golang-collections/collections/stack"
)

func Handle(c net.Conn, s *stack.Stack) {
        fmt.Printf("Serving %s\n", c.RemoteAddr().String())

        buf := make([]byte, 0, 256)
        tmp := make([]byte, 128)

        n, err := c.Read(tmp)
        if err != nil {
                if err != io.EOF {
                        log.Fatalf("connection Read() %v", err)
                }
                return
        }

        buf = append(buf, tmp[:n]...)
}

日志:
Serving [::1]:51699
 ------------- value ---------------:QCXhoy5t
Buffer Length: 9. First Value: 8
Serving [::1]:51700
------------- value ---------------:
Buffer Length: 1. First Value: 8
Serving [::1]:51701

测试寄出:
push random string:
QCXhoy5t

push random string:
GPh0EnbS
push random string:
4kJ0wN0R

最佳答案

Reader的文档说:

Read reads up to len(p) bytes into p. It returns the number of bytes read (0 <= n 
<= len(p)) and any error encountered. Even if Read returns n < len(p), it may use 
all of p as scratch space during the call. If some data is available but not 
len(p) bytes, Read conventionally returns what is available instead of waiting 
for more.

因此,最有可能造成您问题的原因是Read返回了可用数据(在这种情况下为单个字符)。您可以通过使用ioutil.ReadAll或在循环中执行读取(将数据添加到缓冲区的事实使它看起来像原来的意图)来解决此问题,例如:
for {
    n, err := c.Read(tmp)
    if err != nil {
        if err != io.EOF {
            // Note that data might have also been received - you should process that
            // if appropriate.
            log.Fatalf("connection Read() %v", err)
            return
        }
        break // All data received so process it
    }
    buf = append(buf, tmp[:n]...)
}

注意:不能保证会收到任何数据。您应先尝试检查长度,然后再尝试访问它(即buf[0]可能会崩溃)

关于go - 多个串行请求导致缓冲区为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61718676/

相关文章:

java - 确定数组是否会生成与提供的相同的 BST - 使用递归

pointers - Go中的BigInt指针

regex - 使用正则表达式检查URL是否在句子中

string - 如何在 GO 中操作字符串来反转它们?

java - 使用Java的TCP或UDP

c# - 为什么这个端口扫描器代码有时会错过打开的端口?

go - 如何使用 bigtable Go 客户端支持分页?

tcp - 如何修改 Iperf TCP 连接超时?

c++ - 定义掩码的最佳方式

c++ - 将一个数字拆分成几个数字,每个数字只有一个有效位