简体   繁体   中英

Java Socket Bug: Reading Strings from Socket's InputStream

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.

Are you maybe using readLine to read data?

Here is excerpt from javadoc which might be interesting to you:

Since this method continues to search through the input looking for a line separator, it may buffer all of the input searching for the line to skip if no line separators are present.

Can you attach client and server code to see what is wrong? It would be good that you use very simple byte-per-byte reader to check is the stream from server with pauses in transport, maybe the problem is with the way how you write data into server's socket... without code is hard to guess:)

Do you flush the stream every time you write to it from the server side? It could be buffering the output and not writing it to the stream until it reaches a certain threshold.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM