Name: bsT130419 Date: 10/09/2001
C:\WINDOWS>java -version
java version "1.4.0-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta-b65)
Java HotSpot(TM) Client VM (build 1.4.0-beta-b65, mixed mode)
Subsequent reads to BufferedReader.readLine() returns the same text when
reading from a socket, until the client sends new text. For example, if the
client sends "hello" to a socket, BufferedReader.readLine() will return "hello"
indefinitely until the client sends new text to the socket. When the client
sends different input, BufferedReader.readLine() sees the new text fine, but
the return value from readLine() will not change until the client sends new
text through the socket.
Looks like the BufferedReader's buffer is not being cleared.
Here's the settings of the socket:
getTcpNoDelay: false
getSoLinger: 10
getSoTimeout: 75
getSendBufferSize: 8192
getReceiveBufferSize: 8192
You can find the source files and captured output at the following locations:
SOURCE CODE:
http://www.nationalinformatics.com/download/SocketServerTest.java
http://www.nationalinformatics.com/download/SocketServerThread.java
http://www.nationalinformatics.com/download/SocketServerClientTest.java
CAPTURED OUTPUT:
http://www.nationalinformatics.com/download/SocketServerTest_output.txt
http://www.nationalinformatics.com/download/SocketServerClientTest_output.txt
Thread class that reads the input follows:
import java.net.*;
import java.io.*;
import java.util.*;
/** SocketServerThread is an independent thread that is used to test
socket connections.
<p>Copyright 2001 (c) Richard Schilling. All rights reserved.
*/
class SocketServerThread extends Thread {
private boolean running;
private Socket socket = null;
public SocketServerThread(Socket s, boolean r) {
socket = s;
running = r;
}
public void run() {
try {
// Create a reader to receive messages from the client
BufferedReader in = new BufferedReader(
new InputStreamReader(socket.getInputStream())
);
String inputLine = null;
while ( running) {
System.out.println("SocketServerThread:
readline loop.");
boolean lineread = false;
while (!lineread){
lineread = true;
try {
inputLine = in.readLine();
} catch (InterruptedIOException e){
lineread = false;
System.out.println
("SocketServerThread: Interrupted exception while reading from 'in'");
} catch (SocketException e){
lineread = false;
System.out.println
("SocketServerThread: could not read input stream 'in'");
}
}
if (inputLine != null){
if (inputLine.length() > 0){
String TrimmedInputLine =
inputLine.trim();
System.out.println
("SocketServerThread: Input line is '" + TrimmedInputLine + "' (length: " +
TrimmedInputLine.length() + ")");
/* Process the string that has
been received from the client.
*/
System.out.println
("SocketServerThread: input line was read");
if
(TrimmedInputLine.equalsIgnoreCase("bye")){
running = false;
}
}
}
else{
// set the status of this
thread so that it closes the connection and exits.
running = false;
}
inputLine = null;
yield();
}
in.close();
System.out.println("SocketServerThread: CONNECTION WITH
CLIENT CLOSED");
} catch (IOException e) {
e.printStackTrace();
}
}
}
(Review ID: 133410)
======================================================================
- duplicates
-
JDK-4521942 (cs) InputStreamReader repeats characters after socket timeout (regression)
-
- Resolved
-