如何在linux中清除套接字缓冲区

时间:2016-08-25 23:17:00

标签: c linux sockets buffer

我对套接字编程有疑问。我想清除套接字缓冲区。 我尝试了以下代码:

int ret_read = read(return_events[index].data.fd, recv_buffer, sizeof(recv_buffer));
if(-1 == ret_read)
{
    if(EAGAIN != errno)
    {
        printf("read data from %d error occured, errno=%d, %s.\n", return_events[index].data.fd, errno, strerror(errno));
        /**
        Tag-position, I know buffer is not empty. I wonder clear buffer in code position.The buffer is socket recv buffer, is not recv_buffer.
        */
    }
    continue;
}

我不想在Tag-position中再次使用read(),因为我想将缓冲区设置为空。虽然在Tag-position中使用read(),但我认为它可能会失败。

是否有人可以告诉我除标记位置read()以外的其他方式?

1 个答案:

答案 0 :(得分:0)

它与任何其他缓冲区没有区别

Array
(
    [0] => Array
        (
            [0] => A0
            [1] => B0
        )

    [1] => Array
        (
            [0] => A1
            [1] => B1
        )

    [2] => Array
        (
            [0] => A2
            [1] => B2
        )

    [3] => Array
        (
            [0] => A3
            [1] => B3
            [2] => C3
        )

)