Nous construisons un processus de cache trivial qui s'exécute sur chaque nœud. Chacun a plusieurs services en cours d'exécution et tente de se connecter à ce processus de cache local qui reçoit la chaîne JSON en entrée et envoie les détails requis au format JSON. Pour l'instant, nous avons essayé l'approche ci-dessous et il semble que la communication de socket locale avec codeur, décodeur donne des résultats vides.Comment envoyer/recevoir Go Unix Socket JSON chaîne utilisant encodeur/décodeur
Code Serveur
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()
}
Code client
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)
}
Sortie
serveur À partir
bin/serv &
[1] 6195
exécution client
$ bin/client
Action: , CompName:
KeyCheckSum: 0, ValueCheckSum: 0 Error:
Quelqu'un aurait-il fournir des conseils sur la raison pour laquelle nous obtenons cette chaîne vide entre le serveur et la communication client. Nous utilisons Go version go1.8 darwin/amd64.
Merci beaucoup Zoyd pour cet indice. Oui, ça fonctionne comme prévu. Je pense que c'est ce qui se passe quand on passe de plusieurs langages de programmation :-) – user3620473