Skip to content

Instantly share code, notes, and snippets.

@tehmoon
Last active March 1, 2018 02:16
Show Gist options
  • Save tehmoon/9f60e21e1442d390d52c2bde25e97365 to your computer and use it in GitHub Desktop.
Save tehmoon/9f60e21e1442d390d52c2bde25e97365 to your computer and use it in GitHub Desktop.
Tests with socketpair in go
package main
import (
"io"
"syscall"
"os"
"errors"
)
func main() {
fd, err := syscall.Socketpair(syscall.AF_LOCAL, syscall.SOCK_STREAM, 0)
if err != nil {
panic(err)
}
server := os.NewFile(uintptr(fd[0]), "server")
client := os.NewFile(uintptr(fd[1]), "client")
if server == nil || client == nil {
panic(errors.New("Fail to create new file from fd"))
}
go func() {
io.Copy(server, os.Stdin)
}()
go func() {
io.Copy(os.Stdout, client)
}()
select{}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment