What is the best way to attach a socket to Stdin/Stdout. I know we can redirect the stdin/Stdout to any any file descriptor but how can we do the same with sockets. (like how socat works) ?
-
4Where is the code written by you? Have you tried anything yet.shivams– shivams2016-05-07 07:54:42 +00:00Commented May 7, 2016 at 7:54
-
I have added something to open a pipe and set the fd of the pipe to terminal.MakeRaw() but looks like we cant do something like that with sockets.Harish– Harish2016-05-12 20:34:27 +00:00Commented May 12, 2016 at 20:34
Add a comment
|
1 Answer
Well the socket types in Go implement the io.Writer interface, and os.Stdin implements the io.Reader, so my first guess would be to try out bufio.Writer. It would probably look something like:
package main
import (
"bufio"
"os"
)
func main() {
socket := getSocket() // left as an exercise for you to implement
writer := bufio.NewWriter(socket)
writer.ReadFrom(os.Stdin)
// do something to determine when to stop
}
2 Comments
Harish
By creating a socket do you mean net.Dial() ? I need to attach the current terminal to the unix socket. - The other end of the socket is running agetty. Should I have to use the termios structure accomplish this?
Endophage
@Harish
net.Dial can open unix sockets: net.Dial("unix", "/path/to/unix.sock")