Reading all bytes from open UnixConn impossible in go?

Hi guys,

I’m working on exchanging data between 2 processes using unix SOCK_SEQPACKET which have the nice property to never transfers parts of more than one packet at the same time.

The packet that I’m transmitting are not of a fixed size, so I’m trying to find a way to read the entire message. All the reading method on net.Conn or net.PacketConn take a fixed size byte buffer. Which means when I read the packet its possible that my buffer will be too small and therefore I will not get the full message.

I tried using io.Copy, bufio.Reader, ioutil.ReadAll but they all only return when the connection is closed with all messages at once, which is not what I want. I’m running out of ideas (despite using a gigantic byte array which seems stupid). Anyone knows if it’s possible and how to do that?

Hey @ThomasAlxDmy, have you tried using a bytes.Buffer.

Not sure how you’d get the data from io.Reader to bytes.Buffer without io.Copy. I found that thread claiming that you cannot with io.Reader: http://stackoverflow.com/questions/21138693/can-someone-tell-me-whats-the-behavior-of-io-readfull-and-bytes-buffer-readfrom

I don’t know if it’s really the case but it seems inappropriate for SOCK_SEQPACKET

Hey @ThomasAlxDmy,

I might have misunderstood what you were asking, but before what I was suggesting was reading into a bytes.Buffer in a loop with io.CopyN and reading a specific amount of bytes at a time.

Are you trying to accomplish something like the following?

Server: https://play.golang.org/p/lMETRpL7UP
Client: https://play.golang.org/p/kHmBOvC3n3

If that socket is similar to a UDP socket, it does indeed deliver one packet per read() call. You need to provide a buffer large enough to hold the entire packet, or it will be truncated. The buffer doesn’t need to be gigantic - only as large as the largest packet you expect to receive. (For UDP this is nearly limited by the maximum size of a datagram; for your socket type I’m not sure.)

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.