views:

221

answers:

5

Hi all,

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

+2  A: 

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

Zed
Because I need to send it data, of course.
jbu
Why is this being voted up? It's not even an answer.
jbu
+1  A: 

I'm not sure of the implications of never reading from a buffer in Java -- I'd guess that eventually the OS would stop accepting data on that socket, but I'm not sure there.

Why don't you just call the skip method of your InputStream occasionally with a large number, to ensure that you discard the data?

Mark Rushakoff
#skip() is not supported by nearly all possible InputStreams because its not enforced, therefore it's a mechanism which can't be relied on.
Esko
A: 

Hi,

I think you get the InputStream once you accept the request, so if you don't acknowledge that request the underlying framework (i.e. tomcat) will drop that request (after some lapsed time).

Regards.

ATorras
+4  A: 

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.

Dave Ray
+1 for the suggestion to create a thread to read/discard the unwanted data. This is the simplest robust solution; i.e. it avoids the real client thread blocking while waiting for the unwanted data to appear.
Stephen C
A: 
InputStream in = ....
byte[] buffer = new byte[4096] // or whatever
while(true)
  in.read(buffer);

if you accept the connection, you should read the data. to tell you the truth i have never seen (or could forsee) a situation where this (a server that ignores all data) could be useful.

aepurniet