Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: ignoring an input stream - will buffers overflow and bad things happen?

I have a client connecting to my server. The client sends some messages to the server which I do not care about and do not want to waste time parsing its messages if I'm not going to be using them. All the i/o I'm using is simple java i/o, not nio.

If I create the input stream and just never read from it, can that buffer fill up and cause problems? If so, is there something I can do or a property I can set to have it just throw away data that it sees?

Now what if the server doesn't create the input stream at all? Will that cause any problems on the client/sending side?

Please let me know.

Thanks, jbu

like image 654
jbu Avatar asked Jan 24 '23 07:01

jbu


2 Answers

When you accept a connection from a client, you get an InputStream. If you don't read from that stream, the client's data will buffer up. Eventually, the buffer will fill up and the client will block when it tries to write more data. If the client writes all of its data before reading a response from the server, you will end up with a pretty classic deadlock situation. If you really don't care about the data from the client, just read (or call skip) until EOF and drop the data. Alternatively, if it's not a standard request/response (like HTTP) protocol, fire up a new thread that continually reads the stream to keep it from getting backed up.

like image 183
Dave Ray Avatar answered Feb 06 '23 04:02

Dave Ray


If you get no useful data from the client, what's the point of allowing it to connect?

like image 36
Zed Avatar answered Feb 06 '23 05:02

Zed