This question already has an answer here:
- Unable to unmarshal JSON into struct 1 answer
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>