ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How do I get a TCP socket to read into a bit bucket (like /dev/null)?
Background/My Constraints:
In one program, I have a socket that uses 3 pre-allocated buffers to read into. I use another program to process the contents of a full buffer -- but that may take awhile, so its possible for the first program to fill up all 3 buffers. When that happens, I still need to perform a read (as I am being fed a continuous flow of data), but I just want to read into a bit bucket since all my buffers are full.
Is this possible? Or do I have to use some kind of stack buffer and keep reading into that until I can get a free buffer. It just seems wierd that there doesnt seem to be a way to tell a socket to perform a read, but in cases where there is no buffer (lets say the pointer is NULL) just read into /dev/null.
So, allocate a 4th buffer into which your socket can read, and simply don't use the data that goes there. I think you're over-thinking the concept a bit.
So, allocate a 4th buffer into which your socket can read, and simply don't use the data that goes there. I think you're over-thinking the concept a bit.
--- rod.
LOL. I *knew* I was going to get a response like this. Thanks, but that wont work. My buffers are gigabytes in size and they are created from shared memory (both constraints I have to work under), so the number I can create is limited (as my system has only 4gig of memory).
Again, I am suprised that there (apparently) is no Linux feature to read from a socket, into a bit bucket -- this just seems like something so common, you'd expect it to be a feature of the sockets read call. Anyhow, as I said in my original post, I know how to accomplish what I want with a smaller stack buffer -- it just grates me because its an inelegant solution to a very simple problem.
C'mon socket coders, I *know* you must have some secret to doing this besides a "4th buffer"!
Why not just disconnect (ie. close) the socket, and then re-connect at a later time, if needed.
Btw, what in pray tell are you using several gigabyte buffers for? Had you provided more background about your project and why you necessitate such large buffers things might make better sense, but for now, it seems to me that you have "issues" with your app's design.
And, why does the bit-bucket buffer have to be so spacious and special? A few KBytes from the heap can be re-used ad nauseum. After all, you're throwing away all of the data.
Why not just disconnect (ie. close) the socket, and then re-connect at a later time, if needed.
Btw, what in pray tell are you using several gigabyte buffers for? Had you provided more background about your project and why you necessitate such large buffers things might make better sense, but for now, it seems to me that you have "issues" with your app's design.
I cant just disconnect and reconnect because the data source is a (almost) constant stream. There is some downtime between the discrete messages, but we are talking microseconds at best. Constantly closing and opening a socket would be inefficient.
If I could explain in detail why I am using those buffers I would, but I cant, sorry. My constraints are as I listed, and that's the best background I can give.
And, why does the bit-bucket buffer have to be so spacious and special? A few KBytes from the heap can be re-used ad nauseum. After all, you're throwing away all of the data.
--- rod.
Hey Rod,
Sorry, but you arent getting it. A bit bucket is neither spacious nor special -- its a concept that is represented (in one form) as /dev/null in linux. Unfortunately, I dont know how to get a char pointer (not a file pointer!) to /dev/null. So, I dont think that it can be used for what I am looking for.
Yes, I could use a local buffer and read a few KBytes, but that is grossly inefficient for the massive amount of data I am reading in. All those calls cost time/cpu resources! I should be able to make a socket function call to "read" in X bytes of data off of the sockets internal buffer into some internal bit bucket. I quoted "read" because it could be just an increment on the internal socket buffers pointer -- but I've no clue how to do that.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.