Read from socket

后端 未结 5 1064
遥遥无期
遥遥无期 2020-12-31 10:08

I need to read from an AF_UNIX socket to a buffer using the function read from C, but I don\'t know the buffer size.

I think the best way is to read

5条回答
  •  无人及你
    2020-12-31 10:28

    read N bytes until the read returns 0

    Yes!

    One added detail. If the sender doesn't close the connection, the socket will just block, instead of returning. A nonblocking socket will return -1 (with errno == EAGAIN) when there's nothing to read; that's another case.

    Opening the file in binary mode and getting the size would help me in knowing the correct size to give to the buffer?

    Nope. Sockets don't have a size. Suppose you sent two messages over the same connection: How long is the file?

提交回复
热议问题