I am using google's protocol buffers to send data from client to server.
Client and server both are written in Golang.
I think it uses plain tcp to send data from client
to server
.
Sample client code:
func getFakeTransaction() *proto.Transaction {
transaction := new(proto.Transaction)
transaction.ClientId = "client_1"
transaction.ClientName = "Europa"
items := new(proto.Items)
items.ItemId = 1
items.ItemName = "Space suite"
items.ItemValue = 2000
transaction.Items = items
return transaction
}
func readDataFromExternalDatasource() *proto.Transaction {
return getFakeTransaction()
}
func sentDataToServer(data []byte) {
conn, err := net.Dial("tcp", "localhost:8080")
defer conn.Close()
if err != nil {
fmt.Fprintf(os.Stderr, "Error while dialing server: %s
", err.Error())
return
}
sentBytes, err := conn.Write(data)
if err != nil {
fmt.Fprintf(os.Stderr, "Error sending bytes to serve: %s
", err.Error())
return
}
fmt.Printf("Sent %d bytes
", sentBytes)
}
func main() {
fmt.Println("Starting client..")
data := readDataFromExternalDatasource()
dataInByteArr, err := protoc.Marshal(data)
if err != nil {
fmt.Fprintf(os.Stderr, "Error while Marshal data: %s", err.Error())
}
for {
sentDataToServer(dataInByteArr)
time.Sleep(1000)
}
}
How to send data from client to server via HTTP
using protocol buffers in Golang?