Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 7 additions & 26 deletions client/user/clientWR.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package user

//在代码里增或修改一些bug

import (
"bufio"
"encoding/binary"
"fmt"
"io"
"net"
)

Expand All @@ -17,31 +17,12 @@ func writeMessage(conn net.Conn, msg string) error {
}

// 写入消息体
_, err := conn.Write(msgBytes)
n, err := conn.Write(msgBytes)
if n == 0 {
return fmt.Errorf("消息内容为空")
}
if err != nil {
return fmt.Errorf("写入消息内容失败: %w", err)
}
return nil
}
func readMessage(conn net.Conn) (string, error) {
reader := bufio.NewReader(conn)

// 读4字节长度
lengthBytes := make([]byte, 4)
if _, err := io.ReadFull(reader, lengthBytes); err != nil {
return "", fmt.Errorf("读取消息长度失败: %w", err)
}

msgLength := int(binary.BigEndian.Uint32(lengthBytes))
if msgLength <= 0 {
return "", fmt.Errorf("非法消息长度: %d", msgLength)
}

// 读取消息体
msgBytes := make([]byte, msgLength)
if _, err := io.ReadFull(reader, msgBytes); err != nil {
return "", fmt.Errorf("读取消息内容失败: %w", err)
}

return string(msgBytes), nil
return fmt.Errorf("无错误")
}