5

I want to send a file through TCP in golang. here's my server code:

    c is connected *net.TCPConn

    file, _ := os.Open(fn)
    defer file.Close()
    io.Copy(c, file)
    // c.CloseWrite()

and client:

    as above, c is connected *net.TCPConn

    file, _ := os.Create("file.txt")
    defer file.Close()
    io.Copy(file, c)

my question is: in this way, the client can not receive the EOF of the file

so, io.Copy blocked. I have to call c.CloseWrite to notify client that the file is over.

If I want to send files, this will not work, How can I solve this?

0

3 Answers 3

6

If you are using a TCP connection, then os.EOF error means that the connection closed by the other end.

I think the only way to reliably send a file would be to implement a multi-state protocol.

Eg. At the first state of the transfer, tell the client how much bytes to read and go to state 2. In state 2, if all bytes are read, then we know that it read the whole file. If os.EOF is detected before all bytes are read, discard and start again.

4
  • 2
    so I must use c.Read, io.Copy is useless here ?
    – dilfish
    Nov 1, 2012 at 11:05
  • @dilfish, what Allent wanted to ask you is: do you mean you want to send a file to the client but not terminate the TCP connection after that? If yes, then this is not gonna work as TCP just transfers opaque stream of bytes, and interpreting it is the task for the client. So you'll have to devise an "application-level protocol" on top of TCP. As Allen suggested, the server might first indicate how many bytes it will send, and the client then reads that many bytes, writes them to a file and waits for other "transfer indicator" or whatever.
    – kostix
    Nov 1, 2012 at 11:12
  • thanks, my question is, is io.Copy not suitable for this scenario ? It seems to be no, it's not.
    – dilfish
    Nov 1, 2012 at 11:27
  • correct, you will need to use the functions from the net package when working with a TCP connection. I think you would still need some kind of application level protocol even if you close the connection after, just in case the connection is closed prematurely. Nov 1, 2012 at 11:28
2

In normal C, one would shutdown(fd, SHUT_WR) the TCP connection to indicate EOF to the other side. You can do this in go as well:

func shutdownWrite(conn net.Conn) {
     // anonymous interface. Could explicitly use TCP instead.
     if  v, ok := conn.(interface{ CloseWrite() error }); ok {
         v.CloseWrite()
     }
 }

See https://golang.org/src/net/tcpsock_posix.go?s=2073:2109#L75

0

Everything works, if you close the connection from the sender's side.
I made the same thing now - file transfer through TCP. Everything works fine if you add

defer conn.Close()

after opening the connection.
For example:

conn, err := net.Dial("tcp", client)
defer conn.Close()
1
  • The question is transfer many files. The question code above is ok only for one file, but the second file can not work, cause client io.copy() is blocked.
    – kkkkkk
    Aug 6, 2022 at 5:28

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.