Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Bug with InputStream.read after BufferedReader.readLine in a Thread ?

843790Jan 5 2008 — edited Jan 5 2008
I just found something strange when reading bytes on a socket after reading lines.

I'll show you an example :

In this example, when a connexion is established, I launch a thread where a single line of String then 3 bytes are read from the socket. I use BufferedReader.readLine for the String line then just InputStream.read to read the 3 bytes.

The results are quite random. Sometimes it works, sometimes not (most times it doesn't)... it seems to always work under linux (but haven't tested it as many times than on windows). Most of the time the program is stuck on the "socket.getInputStream().read(bytes);" line with 0 bytes available. I tried to do the same thing outside of a thread, I thought it worked better beaucause I was able to run it without any problem but it doesn't work anymore when I test it again.

I can't say if I'm doing something wrong or if it's a java bug (I've got JDK 1.6.0_03-b05).

Can you please have a look a this little example and maybe test it and let me know if it works or not. I tried to code my own readLine function and it seems to work with it but I'd like to know if it's a bug or what. Thank you.

Server side :
import java.io.*;
import java.net.*;

public class testServer extends Thread {

	private Socket socket;
	
	public testServer(Socket testSocket)
	{
		socket = testSocket;
	}
	
	
	public void readData() throws Exception
	{

		BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		String str;
		
		str = br.readLine();
		System.out.println(str);
		
		System.out.println("Bytes available : " + socket.getInputStream().available());
		
                //Try to read the bytes
                byte[] bytes = new byte[3];
                
                //STOPS THERE UNDER WINDOWS, BUT WORKS WITH LINUX
		socket.getInputStream().read(bytes);
		
                //Simple "ack" to say to the client that he can close the connexion
		socket.getOutputStream().write(0);
		socket.getOutputStream().flush();
		
                //Print the bytes values
		for(byte value : bytes)
			System.out.println(value);
	}
	
	public void run()
	{
		try {
			readData();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	public static void main(String[] args) {

		try {
			ServerSocket welcomeSocket = new ServerSocket(3333);
			
			while(true)
				new testServer(welcomeSocket.accept()).start();

		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}
client side :
import java.io.*;
import java.net.*;

public class testClient {

	public static void main(String[] args) {
		
		try {
                        //Some test values
			byte[] testValues = new byte[]{1,2,3};

			Socket socket = new Socket(InetAddress.getLocalHost(), 3333);

                        //Send the line through the socket
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
			bw.write("HELLO WORLD\r\n");
			bw.flush();

                        //then the bytes
			socket.getOutputStream().write(testValues);
			socket.getOutputStream().flush();
                
                        //Just waits for the server's ack to close the connexion
			socket.getInputStream().read();
			socket.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 2 2008
Added on Jan 5 2008
4 comments
2,720 views