无法在Go编程中通过TCP发送gob数据

无法在Go编程中通过TCP发送gob数据,tcp,go,gob,Tcp,Go,Gob,我有一个客户端-服务器应用程序,使用TCP连接 客户: type Q struct { sum int64 } type P struct { M, N int64 } func main() { ... //read M and N ... tcpAddr, err := net.ResolveTCPAddr("tcp4", service) ... var p P p.M = M p.N = N er

我有一个客户端-服务器应用程序,使用TCP连接

客户:

type Q struct {
    sum int64
}

type P struct {
    M, N int64
}

func main() {
    ...
    //read M and N
    ...
    tcpAddr, err := net.ResolveTCPAddr("tcp4", service)
    ...
    var p P
    p.M = M
    p.N = N
    err = enc.Encode(p)
}
服务器:

type Q struct {
    sum int64
}

type P struct {
    M, N int64
}

func main() {
    ...
    tcpAddr, err := net.ResolveTCPAddr("ip4", service)
    listener, err := net.ListenTCP("tcp", tcpAddr)
    ...
    var connB bytes.Buffer
    dec := gob.NewDecoder(&connB)
    var p P
    err = dec.Decode(p)
    fmt.Printf("{%d, %d}\n", p.M, p.N)
}
serve上的结果是{0,0},因为我不知道如何从
net.Conn
获取
bytes.Buffer
变量

有没有办法通过TCP发送gob变量

如果是真的,怎么做?或者通过TCP发送数字有其他选择吗


任何帮助或示例代码都将不胜感激。

这里是一个完整的示例

服务器:

package main

import (
    "fmt"
    "net"
    "encoding/gob"
)

type P struct {
    M, N int64
}
func handleConnection(conn net.Conn) {
    dec := gob.NewDecoder(conn)
    p := &P{}
    dec.Decode(p)
    fmt.Printf("Received : %+v", p);
    conn.Close()
}

func main() {
    fmt.Println("start");
   ln, err := net.Listen("tcp", ":8080")
    if err != nil {
        // handle error
    }
    for {
        conn, err := ln.Accept() // this blocks until connection or error
        if err != nil {
            // handle error
            continue
        }
        go handleConnection(conn) // a goroutine handles conn so that the loop can accept other connections
    }
}
package main

import (
    "fmt"
    "log"
    "net"
    "encoding/gob"
)

type P struct {
    M, N int64
}

func main() {
    fmt.Println("start client");
    conn, err := net.Dial("tcp", "localhost:8080")
    if err != nil {
        log.Fatal("Connection error", err)
    }
    encoder := gob.NewEncoder(conn)
    p := &P{1, 2}
    encoder.Encode(p)
    conn.Close()
    fmt.Println("done");
}
客户端:

package main

import (
    "fmt"
    "net"
    "encoding/gob"
)

type P struct {
    M, N int64
}
func handleConnection(conn net.Conn) {
    dec := gob.NewDecoder(conn)
    p := &P{}
    dec.Decode(p)
    fmt.Printf("Received : %+v", p);
    conn.Close()
}

func main() {
    fmt.Println("start");
   ln, err := net.Listen("tcp", ":8080")
    if err != nil {
        // handle error
    }
    for {
        conn, err := ln.Accept() // this blocks until connection or error
        if err != nil {
            // handle error
            continue
        }
        go handleConnection(conn) // a goroutine handles conn so that the loop can accept other connections
    }
}
package main

import (
    "fmt"
    "log"
    "net"
    "encoding/gob"
)

type P struct {
    M, N int64
}

func main() {
    fmt.Println("start client");
    conn, err := net.Dial("tcp", "localhost:8080")
    if err != nil {
        log.Fatal("Connection error", err)
    }
    encoder := gob.NewEncoder(conn)
    p := &P{1, 2}
    encoder.Encode(p)
    conn.Close()
    fmt.Println("done");
}
启动服务器,然后启动客户机,您会看到服务器显示收到的p值

有几点意见可以澄清:

  • 当您监听套接字时,应该将打开的套接字传递给将处理它的goroutine
  • Conn
    实现了
    读卡器
    写卡器
    接口,使用方便:您可以将其交给
    解码器
    编码器
  • 在实际应用程序中,两个程序导入的包中可能都有
    P
    struct定义

您的示例非常有效。谢谢我唯一的问题是如何将结果从服务器发送回客户端?套接字是双向的。只需在
handleConnection
函数中编写,就像在客户端中编写一样。这非常有效。谢谢你的例子。我想向服务器发送一个{map[string]string}。但它并没有在服务器端被解码。有什么建议吗?