gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 22:23:08 24 4
gpt4 key购买 nike

在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/

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