英文:
How to Send/Receive Go Unix Socket JSON string using encoder/decoder
问题
我们正在构建一个在每个节点上运行的简单缓存进程。每个节点都有多个正在运行的服务,并尝试连接到本地缓存进程,该进程接收JSON字符串作为输入,并以JSON格式发送所需的详细信息。
目前,我们尝试了下面的方法,似乎本地套接字与编码器、解码器之间的通信结果为空。
服务器代码
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\n", 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()
}
客户端代码
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\n", outputJSON.keyCheckSum, outputJSON.valueCheckSum, outputJSON.emsg)
}
输出
启动服务器
bin/serv &
[1] 6195
运行客户端
$ bin/client
Action: , CompName:
KeyCheckSum: 0, ValueCheckSum: 0 Error:
请问有人能够提供一些关于为什么在服务器和客户端之间通信时会得到空字符串的指导吗?我们使用的是Go版本go1.8 darwin/amd64。
英文:
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\n", 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\n", 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.
答案1
得分: 2
JSON的编组/解组只考虑结构体中的公共字段;也就是说,它们的名称必须以大写字母开头。
英文:
JSON marshalling / unmarshalling only takes into account the public fields in a struct; i.e., their name must start with an uppercase letter.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论