Created
June 7, 2017 04:51
-
-
Save s-shin/c53c021adb303a24614c07226d9d99f6 to your computer and use it in GitHub Desktop.
An example of tcp and gzip in golang.
This file contains 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 ( | |
"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) | |
} | |
} |
This file contains 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
#!/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 %% |
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
Why not just
?