Read from socket

后端 未结 5 1047
遥遥无期
遥遥无期 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:44

    On common way is to use ioctl(..) to query FIONREAD of the socket which will return how much data is available.

    int len = 0;
    ioctl(sock, FIONREAD, &len);
    if (len > 0) {
      len = read(sock, buffer, len);
    }
    

提交回复
热议问题