I'm using a Socket to communicate with a ServerSocket. Strings are being sent from the server to my Socket. Each distinct line is a message that, when parsed, contains information. To read these lines of text, a Scanner is used.
The problem is that data is coming in "spurts." Although the Server is sending data continuously and evenly, the data read by the Scanner on the client side seems to pause, read in a bunch of messages (30-40) at once, and then pause again. It repeats this cycle indefinitely.
If I increase the rate at which data is sent, the duration of the pauses decreases; if I slow down the data (to 1 message per second), the bug persists and the pauses become very long. It's almost as if the Socket waits for its buffer to overflow before it sends any data to the Scanner; then flushes everything and waits for the overflow again. However, if I decrease the size of the Socket's buffer, nothing changes at all.
It should be noted that I've used Scanner and Socket in this method before - from the Server side - and everything worked as desired. Additionally, I a) tried this with a BufferedReader like the Java Tutorials (no change in the bug) and b) printed a list of Server transmissions into a file, and read from the file in the same way, and the program worked as expected (constant rate of message reception, etc) so the problem seems to be in the Socket itself.
So: How do I fix this behavior? I'm out of ideas atm and I really don't know what's going on.
CODE (As requested):
// In try block
// Makes the connection
Socket connection = new Socket(TARGET_MACHINE, PORT_NUMBER);
Scanner reader = new Scanner(connection.getInputStream());
// In new Thread
// In run()
while(!finished) // Boolean exit strategy
{
if(reader.hasNextLine())
Sring message = reader.nextLine();
}
That's how I connect and retrieve the Strings.
Also, the Strings that I am receiving are usually about 20-40 characters long.