how to implement google's protocol buffers via http [closed]












-5















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: %sn", err.Error())
return
}
sentBytes, err := conn.Write(data)
if err != nil {
fmt.Fprintf(os.Stderr, "Error sending bytes to serve: %sn", err.Error())
return
}
fmt.Printf("Sent %d bytesn", 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?










share|improve this question















closed as unclear what you're asking by Volker, Flimzy, Vorsprung, JimB, gnat Nov 22 '18 at 9:57


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • 1





    Your code establishes a tcp connection (see the literal "tcp" in net.Dial). So what is the question?

    – Volker
    Nov 21 '18 at 8:17






  • 1





    "Protocol Buffers" is essentially the protocol--it even has protocol in the name :)

    – Flimzy
    Nov 21 '18 at 8:19











  • Can we use protocol buffer over http

    – Prakash Pandey
    Nov 21 '18 at 8:24











  • Yes you can. But why?

    – Volker
    Nov 21 '18 at 8:32






  • 1





    I suggest you google "how does protobuf work?" and start from there. There are worked examples here, github.com/golang/protobuf for instance

    – Vorsprung
    Nov 21 '18 at 9:29
















-5















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: %sn", err.Error())
return
}
sentBytes, err := conn.Write(data)
if err != nil {
fmt.Fprintf(os.Stderr, "Error sending bytes to serve: %sn", err.Error())
return
}
fmt.Printf("Sent %d bytesn", 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?










share|improve this question















closed as unclear what you're asking by Volker, Flimzy, Vorsprung, JimB, gnat Nov 22 '18 at 9:57


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • 1





    Your code establishes a tcp connection (see the literal "tcp" in net.Dial). So what is the question?

    – Volker
    Nov 21 '18 at 8:17






  • 1





    "Protocol Buffers" is essentially the protocol--it even has protocol in the name :)

    – Flimzy
    Nov 21 '18 at 8:19











  • Can we use protocol buffer over http

    – Prakash Pandey
    Nov 21 '18 at 8:24











  • Yes you can. But why?

    – Volker
    Nov 21 '18 at 8:32






  • 1





    I suggest you google "how does protobuf work?" and start from there. There are worked examples here, github.com/golang/protobuf for instance

    – Vorsprung
    Nov 21 '18 at 9:29














-5












-5








-5








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: %sn", err.Error())
return
}
sentBytes, err := conn.Write(data)
if err != nil {
fmt.Fprintf(os.Stderr, "Error sending bytes to serve: %sn", err.Error())
return
}
fmt.Printf("Sent %d bytesn", 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?










share|improve this question
















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: %sn", err.Error())
return
}
sentBytes, err := conn.Write(data)
if err != nil {
fmt.Fprintf(os.Stderr, "Error sending bytes to serve: %sn", err.Error())
return
}
fmt.Printf("Sent %d bytesn", 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?







go network-programming protocols protocol-buffers network-protocols






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 5:54







Prakash Pandey

















asked Nov 21 '18 at 8:10









Prakash PandeyPrakash Pandey

7221230




7221230




closed as unclear what you're asking by Volker, Flimzy, Vorsprung, JimB, gnat Nov 22 '18 at 9:57


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.









closed as unclear what you're asking by Volker, Flimzy, Vorsprung, JimB, gnat Nov 22 '18 at 9:57


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 1





    Your code establishes a tcp connection (see the literal "tcp" in net.Dial). So what is the question?

    – Volker
    Nov 21 '18 at 8:17






  • 1





    "Protocol Buffers" is essentially the protocol--it even has protocol in the name :)

    – Flimzy
    Nov 21 '18 at 8:19











  • Can we use protocol buffer over http

    – Prakash Pandey
    Nov 21 '18 at 8:24











  • Yes you can. But why?

    – Volker
    Nov 21 '18 at 8:32






  • 1





    I suggest you google "how does protobuf work?" and start from there. There are worked examples here, github.com/golang/protobuf for instance

    – Vorsprung
    Nov 21 '18 at 9:29














  • 1





    Your code establishes a tcp connection (see the literal "tcp" in net.Dial). So what is the question?

    – Volker
    Nov 21 '18 at 8:17






  • 1





    "Protocol Buffers" is essentially the protocol--it even has protocol in the name :)

    – Flimzy
    Nov 21 '18 at 8:19











  • Can we use protocol buffer over http

    – Prakash Pandey
    Nov 21 '18 at 8:24











  • Yes you can. But why?

    – Volker
    Nov 21 '18 at 8:32






  • 1





    I suggest you google "how does protobuf work?" and start from there. There are worked examples here, github.com/golang/protobuf for instance

    – Vorsprung
    Nov 21 '18 at 9:29








1




1





Your code establishes a tcp connection (see the literal "tcp" in net.Dial). So what is the question?

– Volker
Nov 21 '18 at 8:17





Your code establishes a tcp connection (see the literal "tcp" in net.Dial). So what is the question?

– Volker
Nov 21 '18 at 8:17




1




1





"Protocol Buffers" is essentially the protocol--it even has protocol in the name :)

– Flimzy
Nov 21 '18 at 8:19





"Protocol Buffers" is essentially the protocol--it even has protocol in the name :)

– Flimzy
Nov 21 '18 at 8:19













Can we use protocol buffer over http

– Prakash Pandey
Nov 21 '18 at 8:24





Can we use protocol buffer over http

– Prakash Pandey
Nov 21 '18 at 8:24













Yes you can. But why?

– Volker
Nov 21 '18 at 8:32





Yes you can. But why?

– Volker
Nov 21 '18 at 8:32




1




1





I suggest you google "how does protobuf work?" and start from there. There are worked examples here, github.com/golang/protobuf for instance

– Vorsprung
Nov 21 '18 at 9:29





I suggest you google "how does protobuf work?" and start from there. There are worked examples here, github.com/golang/protobuf for instance

– Vorsprung
Nov 21 '18 at 9:29












1 Answer
1






active

oldest

votes


















1














I successfully implemented protobuff vai http.



Credits: https://jacobmartins.com/2016/05/24/practical-golang-using-protobuffs/



Sample_Client:



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 sendMessage(transaction *proto.Transaction) {
message, err := protoc.Marshal(transaction)
if err != nil {
fmt.Fprintf(os.Stderr, "Error while marshaling message: %s", err.Error())
os.Exit(1)
}

_, err = http.Post("http://localhost:8080", "", bytes.NewBuffer(message))
if err != nil {
fmt.Fprintf(os.Stderr, "Error while post request to server: %s", err.Error())
os.Exit(1)
}
fmt.Printf("Sent %d bytes to servern", len(message))
}

func main() {
fmt.Println("Starting client..")
transaction := getFakeTransaction()
for {
sendMessage(transaction)
// time.Sleep(1 * time.Second)
}
}


Sample Server:



func printMessage(t *proto.Transaction) {
clientId := t.GetClientId()
clientName := t.GetClientName()
items := t.GetItems()
fmt.Printf("ClientId: %s, ClientName: %s, Items: %sn", clientId, clientName, items)
}

func main() {
fmt.Println("Staring server..")
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
message, err := ioutil.ReadAll(r.Body)
if err != nil {
fmt.Fprintf(os.Stderr, "Error while reading data from client: ", err.Error())
return
}
transaction := new(proto.Transaction)
// protoc.Unmarshal(message, &transaction)
if err = transaction.XXX_Unmarshal(message); err != nil {
fmt.Fprintf(os.Stderr, "Error while unmarshaling client message: %s", err.Error())
return
}
printMessage(transaction)
})
http.ListenAndServe(":8080", nil)

}


Sample Protofile:



syntax="proto3";

package proto;

enum Status {
SUCCESS = 0;
INPROGRESS = 1;
FAILED = 2;
}

message Items {
int32 itemId = 1;
string itemName = 2;
int32 itemValue = 3;
Status status = 4;
}

message Transaction {
string clientId = 1;
string clientName = 2;
Items items = 3;
}





share|improve this answer
































    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    I successfully implemented protobuff vai http.



    Credits: https://jacobmartins.com/2016/05/24/practical-golang-using-protobuffs/



    Sample_Client:



    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 sendMessage(transaction *proto.Transaction) {
    message, err := protoc.Marshal(transaction)
    if err != nil {
    fmt.Fprintf(os.Stderr, "Error while marshaling message: %s", err.Error())
    os.Exit(1)
    }

    _, err = http.Post("http://localhost:8080", "", bytes.NewBuffer(message))
    if err != nil {
    fmt.Fprintf(os.Stderr, "Error while post request to server: %s", err.Error())
    os.Exit(1)
    }
    fmt.Printf("Sent %d bytes to servern", len(message))
    }

    func main() {
    fmt.Println("Starting client..")
    transaction := getFakeTransaction()
    for {
    sendMessage(transaction)
    // time.Sleep(1 * time.Second)
    }
    }


    Sample Server:



    func printMessage(t *proto.Transaction) {
    clientId := t.GetClientId()
    clientName := t.GetClientName()
    items := t.GetItems()
    fmt.Printf("ClientId: %s, ClientName: %s, Items: %sn", clientId, clientName, items)
    }

    func main() {
    fmt.Println("Staring server..")
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    message, err := ioutil.ReadAll(r.Body)
    if err != nil {
    fmt.Fprintf(os.Stderr, "Error while reading data from client: ", err.Error())
    return
    }
    transaction := new(proto.Transaction)
    // protoc.Unmarshal(message, &transaction)
    if err = transaction.XXX_Unmarshal(message); err != nil {
    fmt.Fprintf(os.Stderr, "Error while unmarshaling client message: %s", err.Error())
    return
    }
    printMessage(transaction)
    })
    http.ListenAndServe(":8080", nil)

    }


    Sample Protofile:



    syntax="proto3";

    package proto;

    enum Status {
    SUCCESS = 0;
    INPROGRESS = 1;
    FAILED = 2;
    }

    message Items {
    int32 itemId = 1;
    string itemName = 2;
    int32 itemValue = 3;
    Status status = 4;
    }

    message Transaction {
    string clientId = 1;
    string clientName = 2;
    Items items = 3;
    }





    share|improve this answer






























      1














      I successfully implemented protobuff vai http.



      Credits: https://jacobmartins.com/2016/05/24/practical-golang-using-protobuffs/



      Sample_Client:



      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 sendMessage(transaction *proto.Transaction) {
      message, err := protoc.Marshal(transaction)
      if err != nil {
      fmt.Fprintf(os.Stderr, "Error while marshaling message: %s", err.Error())
      os.Exit(1)
      }

      _, err = http.Post("http://localhost:8080", "", bytes.NewBuffer(message))
      if err != nil {
      fmt.Fprintf(os.Stderr, "Error while post request to server: %s", err.Error())
      os.Exit(1)
      }
      fmt.Printf("Sent %d bytes to servern", len(message))
      }

      func main() {
      fmt.Println("Starting client..")
      transaction := getFakeTransaction()
      for {
      sendMessage(transaction)
      // time.Sleep(1 * time.Second)
      }
      }


      Sample Server:



      func printMessage(t *proto.Transaction) {
      clientId := t.GetClientId()
      clientName := t.GetClientName()
      items := t.GetItems()
      fmt.Printf("ClientId: %s, ClientName: %s, Items: %sn", clientId, clientName, items)
      }

      func main() {
      fmt.Println("Staring server..")
      http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
      message, err := ioutil.ReadAll(r.Body)
      if err != nil {
      fmt.Fprintf(os.Stderr, "Error while reading data from client: ", err.Error())
      return
      }
      transaction := new(proto.Transaction)
      // protoc.Unmarshal(message, &transaction)
      if err = transaction.XXX_Unmarshal(message); err != nil {
      fmt.Fprintf(os.Stderr, "Error while unmarshaling client message: %s", err.Error())
      return
      }
      printMessage(transaction)
      })
      http.ListenAndServe(":8080", nil)

      }


      Sample Protofile:



      syntax="proto3";

      package proto;

      enum Status {
      SUCCESS = 0;
      INPROGRESS = 1;
      FAILED = 2;
      }

      message Items {
      int32 itemId = 1;
      string itemName = 2;
      int32 itemValue = 3;
      Status status = 4;
      }

      message Transaction {
      string clientId = 1;
      string clientName = 2;
      Items items = 3;
      }





      share|improve this answer




























        1












        1








        1







        I successfully implemented protobuff vai http.



        Credits: https://jacobmartins.com/2016/05/24/practical-golang-using-protobuffs/



        Sample_Client:



        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 sendMessage(transaction *proto.Transaction) {
        message, err := protoc.Marshal(transaction)
        if err != nil {
        fmt.Fprintf(os.Stderr, "Error while marshaling message: %s", err.Error())
        os.Exit(1)
        }

        _, err = http.Post("http://localhost:8080", "", bytes.NewBuffer(message))
        if err != nil {
        fmt.Fprintf(os.Stderr, "Error while post request to server: %s", err.Error())
        os.Exit(1)
        }
        fmt.Printf("Sent %d bytes to servern", len(message))
        }

        func main() {
        fmt.Println("Starting client..")
        transaction := getFakeTransaction()
        for {
        sendMessage(transaction)
        // time.Sleep(1 * time.Second)
        }
        }


        Sample Server:



        func printMessage(t *proto.Transaction) {
        clientId := t.GetClientId()
        clientName := t.GetClientName()
        items := t.GetItems()
        fmt.Printf("ClientId: %s, ClientName: %s, Items: %sn", clientId, clientName, items)
        }

        func main() {
        fmt.Println("Staring server..")
        http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        message, err := ioutil.ReadAll(r.Body)
        if err != nil {
        fmt.Fprintf(os.Stderr, "Error while reading data from client: ", err.Error())
        return
        }
        transaction := new(proto.Transaction)
        // protoc.Unmarshal(message, &transaction)
        if err = transaction.XXX_Unmarshal(message); err != nil {
        fmt.Fprintf(os.Stderr, "Error while unmarshaling client message: %s", err.Error())
        return
        }
        printMessage(transaction)
        })
        http.ListenAndServe(":8080", nil)

        }


        Sample Protofile:



        syntax="proto3";

        package proto;

        enum Status {
        SUCCESS = 0;
        INPROGRESS = 1;
        FAILED = 2;
        }

        message Items {
        int32 itemId = 1;
        string itemName = 2;
        int32 itemValue = 3;
        Status status = 4;
        }

        message Transaction {
        string clientId = 1;
        string clientName = 2;
        Items items = 3;
        }





        share|improve this answer















        I successfully implemented protobuff vai http.



        Credits: https://jacobmartins.com/2016/05/24/practical-golang-using-protobuffs/



        Sample_Client:



        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 sendMessage(transaction *proto.Transaction) {
        message, err := protoc.Marshal(transaction)
        if err != nil {
        fmt.Fprintf(os.Stderr, "Error while marshaling message: %s", err.Error())
        os.Exit(1)
        }

        _, err = http.Post("http://localhost:8080", "", bytes.NewBuffer(message))
        if err != nil {
        fmt.Fprintf(os.Stderr, "Error while post request to server: %s", err.Error())
        os.Exit(1)
        }
        fmt.Printf("Sent %d bytes to servern", len(message))
        }

        func main() {
        fmt.Println("Starting client..")
        transaction := getFakeTransaction()
        for {
        sendMessage(transaction)
        // time.Sleep(1 * time.Second)
        }
        }


        Sample Server:



        func printMessage(t *proto.Transaction) {
        clientId := t.GetClientId()
        clientName := t.GetClientName()
        items := t.GetItems()
        fmt.Printf("ClientId: %s, ClientName: %s, Items: %sn", clientId, clientName, items)
        }

        func main() {
        fmt.Println("Staring server..")
        http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        message, err := ioutil.ReadAll(r.Body)
        if err != nil {
        fmt.Fprintf(os.Stderr, "Error while reading data from client: ", err.Error())
        return
        }
        transaction := new(proto.Transaction)
        // protoc.Unmarshal(message, &transaction)
        if err = transaction.XXX_Unmarshal(message); err != nil {
        fmt.Fprintf(os.Stderr, "Error while unmarshaling client message: %s", err.Error())
        return
        }
        printMessage(transaction)
        })
        http.ListenAndServe(":8080", nil)

        }


        Sample Protofile:



        syntax="proto3";

        package proto;

        enum Status {
        SUCCESS = 0;
        INPROGRESS = 1;
        FAILED = 2;
        }

        message Items {
        int32 itemId = 1;
        string itemName = 2;
        int32 itemValue = 3;
        Status status = 4;
        }

        message Transaction {
        string clientId = 1;
        string clientName = 2;
        Items items = 3;
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 22 '18 at 5:55

























        answered Nov 21 '18 at 12:16









        Prakash PandeyPrakash Pandey

        7221230




        7221230















            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            Npm cannot find a required file even through it is in the searched directory

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith