I got a thread that reads and writes to a network socket.
public void run() {
try {
Socket socket = new Socket("10.0.1.11", 19456);
out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String message = "13" + seperator + "0" + seperator + "userdata";
out.write(message);
out.flush();
while (listen) {
String input = "";
for (int letter = in.read(); letter != -1; letter = in.read()) {
System.out.println((char)letter);
input += (char)letter;
}
System.out.println(input);
displayText(input);
}
socket.close();
} catch (UnknownHostException e) {
displayText("Could not connect to server!");
} catch (IOException e) {
displayText("Could not open IO to server!");
}
}
However, everything after the for-loop is not executed. The System.out.println inside the for-loop prints out all the letters it’s supposed to, but the System.out.println and displayText function after the for-loop are never executed (checked with breakpoints)… What am I missing here? :S
According to http://www.docjar.com/docs/api/java/io/Reader.html#read
Their code example does show they -1 is returned on EOF.
So, your for loop is blocking because in.read() is not recognizing the EOF condition.
And it can’t do that until the socket server shuts down its send direction, which closes the receive half at the client.