2

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) ?

2
  • 4
    Where is the code written by you? Have you tried anything yet. Commented 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. Commented May 12, 2016 at 20:34

1 Answer 1

2

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
}
Sign up to request clarification or add additional context in comments.

2 Comments

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?
@Harish net.Dial can open unix sockets: net.Dial("unix", "/path/to/unix.sock")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.