gpt4 book ai didi

Golang Bufio 编写器不写入 TCP 连接

转载 作者:IT王子 更新时间:2023-10-29 02:10:06 25 4
gpt4 key购买 nike

我正在开发一个用 Golang 编写的小型服务器。我正在查看以下示例:

https://gist.github.com/kenshinx/5796276

当尝试从 Bufio 包中实现 Reader 和 Writer 时,只能从连接中读取。看起来它没有写任何东西(好吧,我没有收到客户端的任何东西),而且它也没有给出错误。但是,使用实际连接而不是 bufio.Writer 进行写入工作正常。

这是代码。

package main

import (
"fmt"
"net"
"os"
"bufio"
"strings"
)

const (
CONN_HOST = "localhost"
CONN_TYPE = "tcp"
CONN_PORT = "3333"
)

type Client struct{
name string
reader *bufio.Reader
writer *bufio.Writer
connection net.Conn
}

type Clients [] Client

var lobby Clients

func main() {
c, err := net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT)

if err != nil {
fmt.Println("Error listening:", err.Error())
os.Exit(1)
}

defer c.Close()
fmt.Println("Server listening on port " + CONN_PORT)

go matchmaking()

for {
conn, err := c.Accept()
if err != nil {
fmt.Println("Error accepting: ", err.Error())
os.Exit(1)
}

if conn != nil {
go handleRequest(conn)
}
}
}

func handleRequest(conn net.Conn) {
client := &Client{
reader: bufio.NewReader(conn),
writer: bufio.NewWriter(conn),
connection:conn,
}

client.name, _ = client.reader.ReadString('\n') // Works fine
client.name = strings.TrimSpace(client.name)

lobby = append(lobby, *client)
fmt.Println("Client connected: " + client.name)
}

func matchmaking(){
fmt.Println("Matchmaker started!")
for {
for i := range lobby {
if len(lobby) >= 2 {
go startMatch(lobby[i], lobby[i+1] )
lobby = append(lobby[:i], lobby[i+1:]...) // Remove from lobby
lobby = append(lobby[:i], lobby[i+1:]...)
}
}
}
}

func startMatch(client1 Client, client2 Client){
client1.writer.WriteString("found\n") // Doens't work?
client2.writer.WriteString("found\n") // Doens't work?

//client1.writer.Write([]byte("found\n")) => Doensn't work either
//client1.connection.Write([]byte("found\n")) => this works fine..?

fmt.Println("Starting match with: " + client1.name + " and " + client2.name)
fmt.Println("Current lobby size is: ", len(lobby))
}

正如标题所说,为什么 bufio.Writer 不写入已连接的客户端?

最佳答案

缓冲的写入器已经缓冲了数据。要进一步插入它,请调用 writer.Flush .

func startMatch(client1 Client, client2 Client){
_, err := client1.writer.WriteString("found\n")
// handle error here
err = client1.writer.Flush()
// handle error here
_, err = client2.writer.WriteString("found\n")
// handle error here
err = client2.writer.Flush()
// handle error here

fmt.Println("Starting match with: " + client1.name + " and " + client2.name)
fmt.Println("Current lobby size is: ", len(lobby))
}

处理刷新操作中的错误也变得很重要,因为此时潜在的编写器错误可能会冒出来。

关于Golang Bufio 编写器不写入 TCP 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49348463/

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