Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to discard data from a pipe or socket in linux?

I've got a situation where I might want to "skip" data in a pipe or socket for processing. I could of course read() the data into a buffer and just discard it, but is there a more efficient way? Perhaps something like sendfile that would work with a non-mmappable source?

like image 865
gct Avatar asked Sep 19 '25 11:09

gct


1 Answers

You need to read the data. What you do with it is up to you, so you can skip over parts, or whatever.

Without reading it the data stays stuck in the read buffer. The recommended way to get it out of the buffer is to read it.

That being said, if you read something that indicates that all subsequent data will be of no use, you could just close the connection and be done with it.

I'm not sure what you mean about "efficient" because reading from a buffer is probably not the performance bottleneck in your code.

like image 155
tadman Avatar answered Sep 22 '25 03:09

tadman