Created
September 15, 2013 12:46
-
-
Save dagoof/6570486 to your computer and use it in GitHub Desktop.
echo server that tee's output
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 ( | |
"flag" | |
"io" | |
"log" | |
"net" | |
"os" | |
) | |
var ( | |
sock = flag.String("socket", "echo.sock", "socket filename") | |
) | |
func init() { | |
flag.Parse() | |
} | |
func main() { | |
c, err := net.Dial("unix", *sock) | |
defer c.Close() | |
if err != nil { | |
log.Fatal(err) | |
} | |
_, err = io.Copy(c, os.Stdin) | |
if err != nil { | |
log.Fatal(err) | |
} | |
io.Copy(os.Stdout, c) | |
} |
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 ( | |
"bytes" | |
"flag" | |
"io" | |
"log" | |
"net" | |
"os" | |
"os/signal" | |
"time" | |
) | |
var ( | |
sock = flag.String("socket", "echo.sock", "socket filename") | |
) | |
func init() { | |
flag.Parse() | |
} | |
func Echo(c net.Conn) { | |
var buf bytes.Buffer | |
defer c.Close() | |
c.SetReadDeadline(time.Now().Add(time.Second / 2)) | |
io.Copy(&buf, c) | |
io.Copy(io.MultiWriter(c, os.Stdout), &buf) | |
} | |
func Serve() { | |
l, err := net.Listen("unix", *sock) | |
if err != nil { | |
log.Fatal(err) | |
} | |
for { | |
if c, err := l.Accept(); err == nil { | |
go Echo(c) | |
} | |
} | |
} | |
func main() { | |
go Serve() | |
defer os.Remove(*sock) | |
c := make(chan os.Signal, 1) | |
signal.Notify(c, os.Interrupt, os.Kill) | |
<-c | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment