dtnmuyoiw680512744 2017-04-05 11:17
浏览 57

如何使用编码器/解码器发送/接收Go Unix Socket JSON字符串[重复]

This question already has an answer here:

We are building a trivial cache process which runs on every node. Each has multiple services up and running and tries to connect to this local cache process which receives the JSON string as an input, and sends the required details in JSON format.

As of now, we have tried the below approach and it seems the local socket communication with encoder, decoder is giving empty results.

Server Code

package main

import (
    "encoding/json"
    "fmt"
    "net"
    "os"
)

var sockLocArg = "/tmp/.testsock"

type sockOut struct {
    keyCheckSum   uint32 `json:"keyCheckSum"`
    valueCheckSum uint32 `json:"valueCheckSum"`
    emsg          string `json:"emsg"`
}

type sockIn struct {
    action   string `json:"action"`
    compName string `json:"compname"`
}

func processSockRequest(c net.Conn) {

    defer c.Close()
    decode := json.NewDecoder(c)
    encode := json.NewEncoder(c)

    var inputJSON sockIn
    err := decode.Decode(&inputJSON)
    if err != nil {
        fmt.Printf("Error is %v", err)
    }
    fmt.Printf("Action: %s, CompName: %s
", inputJSON.action, inputJSON.compName)

    outputJSON := sockOut{
        keyCheckSum:   10,
        valueCheckSum: 10,
        emsg:          "",
    }
    // Send response back to the socket request
    err = encode.Encode(outputJSON)
    if err != nil {
        fmt.Printf("Error is %v", err)
    }
}

func initSocket() {
    // Creating the unix domain TCP socket
    //
    localSocket, err := net.Listen("unix", sockLocArg)
    if err != nil {
        fmt.Printf("Unable to create unix domain socket. Error: %v", err)
        os.Exit(1)
    }
    // Set the permissions 700 on this
    //
    if err = os.Chmod(sockLocArg, 0700); err != nil {
        fmt.Printf("Unable to change the permissions for the socket. Error: %v", err)
        os.Exit(1)
    }
    // Initiate and listen to the socket
    //
    for {
        SockFileDescriptor, err := localSocket.Accept()
        if err != nil {
            fmt.Printf("Unable to accept incoming messages over the socket. Error: %v", err)
            os.Exit(1)
        }
        processSockRequest(SockFileDescriptor)
    }
}

func main() {
    initSocket()
}

Client Code

package main

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

type sockOut struct {
    keyCheckSum   uint32 `json:"keyCheckSum"`
    valueCheckSum uint32 `json:"valueCheckSum"`
    emsg          string `json:"emsg"`
}

type sockIn struct {
    action   string `json:"action"`
    compName string `json:"compname"`
}

func main() {
    c, err := net.Dial("unix", "/tmp/.testsock")
    if err != nil {
        panic(err)
    }
    defer c.Close()

    //go reader(c)

    decode := json.NewDecoder(c)
    encode := json.NewEncoder(c)

    inputJSON := sockIn{
        action:   "GET",
        compName: "TEST",
    }
    err = encode.Encode(inputJSON)

    if err != nil {
        fmt.Printf("Error is: %v", err)
    }
    var outputJSON sockOut
    err = decode.Decode(&outputJSON)

    if err != nil {
        fmt.Printf("Error is %v", err)
    }
    fmt.Printf("KeyCheckSum: %d, ValueCheckSum: %d Error: %s
", outputJSON.keyCheckSum, outputJSON.valueCheckSum, outputJSON.emsg)
}

Output

Starting Server

bin/serv &
[1] 6195

Running Client

$ bin/client 
Action: , CompName: 
KeyCheckSum: 0, ValueCheckSum: 0 Error: 

Would someone provide some guidance on why we are getting this empty string between server and client communication. We are using Go version go1.8 darwin/amd64.

</div>
  • 写回答

1条回答 默认 最新

  • douqian2524 2017-04-05 11:35
    关注

    JSON marshalling / unmarshalling only takes into account the public fields in a struct; i.e., their name must start with an uppercase letter.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?