Unable to send gob data over TCP in Go Programming

前端 未结 1 853
鱼传尺愫
鱼传尺愫 2021-01-31 19:50

I have a client server application, using TCP connection

Client:

type Q struct {
    sum int64
}

type P struct {
    M, N int64
}

func main() {
    ...         


        
相关标签:
1条回答
  • 2021-01-31 20:09

    Here's a complete example.

    Server:

    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
        }
    }
    

    Client :

    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");
    }
    

    Launch the server, then the client, and you see the server displaying the received P value.

    A few observations to make it clear :

    • When you listen on a socket, you should pass the open socket to a goroutine that will handle it.
    • Conn implements the Reader and Writer interfaces, which makes it easy to use : you can give it to a Decoder or Encoder
    • In a real application you would probably have the P struct definition in a package imported by both programs
    0 讨论(0)
提交回复
热议问题