Skip to content

Instantly share code, notes, and snippets.

@s-shin
Created June 7, 2017 04:51
Show Gist options
  • Save s-shin/c53c021adb303a24614c07226d9d99f6 to your computer and use it in GitHub Desktop.
Save s-shin/c53c021adb303a24614c07226d9d99f6 to your computer and use it in GitHub Desktop.
An example of tcp and gzip in golang.
package main
import (
"compress/gzip"
"io"
"log"
"net"
)
func main() {
l, err := net.Listen("tcp", ":5000")
if err != nil {
log.Fatal(err)
}
for {
conn, err := l.Accept()
if err != nil {
log.Fatal(err)
}
go func(c net.Conn) {
defer c.Close()
r, w := io.Pipe()
go func() {
defer w.Close()
gzr, err := gzip.NewReader(c)
if err != nil {
log.Fatal(err)
}
io.Copy(w, gzr)
}()
io.Copy(c, r)
}(conn)
}
}
#!/bin/bash
set -eu
rm -f hello.txt hello.txt.gz
# start server
go run tcp-gzip.go &
# send gzip data
echo "Hello, tcp!" > hello.txt
gzip hello.txt
nc localhost 5000 < hello.txt.gz
kill %%
@s-shin
Copy link
Author

s-shin commented Mar 19, 2020

Though I don't remember what I wanted to do, maybe it was intended to manipulate the stream more after gzip, I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment