Created
August 12, 2024 17:24
-
-
Save dlisboa/682f55fb039bb3f9c00ad92cea4e29a7 to your computer and use it in GitHub Desktop.
GOB encoding through TCP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "encoding/gob" | |
| "fmt" | |
| "log" | |
| "net" | |
| "time" | |
| ) | |
| type request struct { | |
| Query string | |
| Results uint | |
| } | |
| func main() { | |
| for { | |
| c, err := net.Dial("tcp", ":4000") | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| req := request{Query: "searchterm", Results: 10} | |
| err = gob.NewEncoder(c).Encode(req) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| fmt.Println("sent:", req) | |
| time.Sleep(1 * time.Second) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "encoding/gob" | |
| "fmt" | |
| "log" | |
| "net" | |
| ) | |
| type request struct { | |
| Query string | |
| Results uint | |
| } | |
| func main() { | |
| l, err := net.Listen("tcp", ":4000") | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| for { | |
| c, err := l.Accept() | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| var req request | |
| err = gob.NewDecoder(c).Decode(&req) | |
| if err != nil { | |
| log.Println("decode:", err) | |
| } | |
| fmt.Println("received:", req) | |
| c.Close() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment