使用Go语言实现UDP的客户端/服务器

huangapple go评论83阅读模式
英文:

Client/Server using UDP in Go

问题

我正在使用gob将消息从客户端发送到服务器,这部分是正常工作的,但是当服务器响应客户端时,它不会从连接中读取。

func servidor(porta int){
    var site string
    
    addr := net.UDPAddr{
        Port: porta,
        IP: net.ParseIP("localhost"),
    }
    
    conn, erro := net.ListenUDP("udp", &addr)
    verificaErro(erro)
    
    enc := gob.NewEncoder(conn)
    dec := gob.NewDecoder(conn)
    
    dec.Decode(&site)
    
    enc.Encode(site)
}

func cliente(site string){
    porta := "1200"
    conn := ConnectToSocket("localhost:"+porta)
    
    enc := gob.NewEncoder(conn)
    dec := gob.NewDecoder(conn)
    
    enc.Encode("Test")
    dec.Decode(&site)
    
    fmt.Println(site)
}

你想知道如何解决这个问题?

英文:

I'm using gob to send messages from client to serve and this is working, however when the server response to client this don't read from connection.

func servidor(porta int){
var site string

addr := net.UDPAddr{
	Port: porta,
	IP: net.ParseIP("localhost"),
}

conn, erro := net.ListenUDP("udp", &addr)
verificaErro(erro)

enc := gob.NewEncoder(conn)
dec := gob.NewDecoder(conn)

dec.Decode(&site)

enc.Encode(site)

}

func cliente(site string){
porta := "1200"
conn := ConnectToSocket("localhost:"+porta)

enc := gob.NewEncoder(conn)
dec := gob.NewDecoder(conn)

enc.Encode("Test")
dec.Decode(&site)

fmt.Println(site)

}

How can I solve this problem?

答案1

得分: 5

有两个问题:

  • UDP是基于数据包的协议,但gob期望的是一个流。编码器在编码一个值时可以多次调用连接的Write方法。每次写入都会发送一个数据包。你可能希望每个编码值只有一个数据包。
  • Gob流具有状态。编码器发送有关任何类型的信息一次,并期望解码器记住这些信息。即使gob编码器对每个编码值只调用一次Write,后续写入发送的数据包也不会包含类型信息。

修复方法是将数据编码到缓冲区并发送该缓冲区:

var buf bytes.Buffer
if err := gob.NewEncoder(&buf).Encode(value); err != nil {
   // 处理错误
}
_, err := c.WriteTo(buf.Bytes(), addr)

接收到缓冲区并从该缓冲区解码:

buf := make([]byte, 1024)
n, addr, err := c.ReadFrom(buf)
if err != nil {
   // 处理错误
}
if err := gob.NewDecoder(bytes.NewReader(buf[:n])).Decode(&v); err != nil {
   // 处理错误
}
英文:

There are two issues:

  • UDP is a packet based protocol, but gob is expecting a stream. The encoder can call the connection Write method multiple times when encoding a value. Each write sends a packet. You probably want one packet per encoded value.
  • Gob streams have state. The encoder sends information about any type once and expects the decoder to remember the information. Even if the gob encoder calls Write exactly once per encoded value, packets sent by subsequent writes will not include the type information.

The fix is to encode to a buffer and send that buffer:

var buf Bytes.Buffer
if err := gob.NewEncoder(&buf).Encode(value); err != nil {
   // handle error
}
_, err := c.WriteTo(buf.Bytes(), addr)

Receive to a buffer and decode from that buffer:

buf := make([]byte, 1024)
n, addr, err := c.ReadFrom(buf)
if err != nil {
   // handle error
}
if err := gob.NewDecoder(bytes.NewReader(buf[:n])).Decode(&v); err != nil {
   // handle error
}

huangapple
  • 本文由 发表于 2014年10月2日 05:02:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/26150610.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定