Hi all,
I have a weird error: io.copy works fine in piping into a tcp connection, but it cannot send to a file the same bytes.
This is the relevant code:
connection, requests, err := newChannel.Accept()
if err != nil {
log.Printf("Could not accept channel (%s)", err)
return
}
remoteconnection, e := net.Dial("tcp", "127.0.0.1:7777")
if e != nil {
log.Fatal("dialing: ", e.Error())
}
log.Printf("Connection: %#v", connection)
flowlog, err := os.Create("flowlog")
if err != nil {
log.Fatal("error: ", e.Error())
}
//pipe session to bash and visa-versa
var once sync.Once
// Prepare teardown function
close := func() {
log.Printf("closing connection..")
remoteconnection.Close()
flowlog.Close()
}
go func() {
inl,err := io.Copy(connection,flowlog)
if err != nil {
log.Fatal("error: ", err.Error())
}
log.Printf("bytes written to log: %d", inl)
in,_ := io.Copy(connection, remoteconnection)
log.Printf("bytes in written: %d", in)
io.Copy(connection,os.Stdout)
flowlog.Sync()
once.Do(close)
}()
This is the output during my tests:
2017/10/04 17:14:07 bytes written to log: 0
2017/10/04 17:14:18 bytes in written: 12
I suspect the error is related to different type of destination…but I don’t how to solve it…
Please help me to understand and fix it.
Thanks in advance.