Problem With Bufferedinputstream Read Blocking Issue In Java - Programming - Nairaland
Nairaland Forum › Science/Technology › Programming › Problem With Bufferedinputstream Read Blocking Issue In Java (4806 Views)
| Problem With Bufferedinputstream Read Blocking Issue In Java by nollyj(op): 10:49pm On Oct 15, 2013 |
I wrote a client server application that list all the files in the server and can download any file in the server with a command. Everything seems to be working well but I am having a problem with downloading files to the client due to read blocking. Problem case When the client request to download a file in the server, the server send the content of the file in bytes. The client will start to read the received byte into a buffer and the size of the buffer is 8192 which I got from int bSize = clientSocket.getReceiveBufferSize(); Since the buffer size from the server is less than that of client, the client blocks after reading the bytes from the server since it is still waiting for more and the server has less. Server Code private static void DownloadFileFromServer(BufferedInputStream bis, String filePath){Server Code
I have used the socket setTimeout which works but it always throws Java socket exception. I have also tried using separate thread for each server connection but it did not know for me. I need suggestion or point on how best to solve this. Thanks |
| Re: Problem With Bufferedinputstream Read Blocking Issue In Java by BetaQuick: 2:19am On Oct 16, 2013 |
You are trying to read greater than whats in the buffered input stream. Why dont you try a bytearrayoutputstream. See below // output bucket to read data ByteArrayOutputStream os = new ByteArrayOutputStream(); // read the response data InputStream in = new BufferedInputStream(conn.getInputStream()); int read = 0; byte[] data = new byte[1024]; while((read = in.read(data)) > 0) { os.write(data, 0, read); } // Your data is now in the ByteArrayOutputStream for reading |
| Re: Problem With Bufferedinputstream Read Blocking Issue In Java by nollyj(op): 9:01am On Oct 16, 2013 |
BetaQuick: You are trying to read greater than whats in the buffered input stream.Thanks, but it still gives the same problem since the blocking occurs here in.read(data) |
| Re: Problem With Bufferedinputstream Read Blocking Issue In Java by BetaQuick: 10:34am On Oct 16, 2013 |
Ok, the reason why your read is blocking is because I suspect the socket is still open. Keep in mind that you cannot always just read from the Socket's InputStream until it returns -1, as you can when reading a file. The reason is that -1 is only returned when the server closes the connection. But a server may not always close the connection. Perhaps you want to send multiple requests over the same connection. In that case it would be pretty stupid to close the connection. Instead you must know how many bytes to read from the Socket's InputStream. This can be done by either the server telling how many bytes it is sending, or by looking for a special end-of-data character. |
| Re: Problem With Bufferedinputstream Read Blocking Issue In Java by nollyj(op): 11:16am On Oct 16, 2013 |
BetaQuick: Instead you must know how many bytes to read from the Socket's InputStream. This can be done by either the server telling how many bytes it is sending, or by looking for a special end-of-data character.Thanks once again. If you look at my first post, using this method call gets the amount of bytes the server is excepting from the client. int bSize = clientSocket.getReceiveBufferSize(); This returns 8192 when you print out bSize; Since the client is send less than 8192 bytes, the BufferedInputStream read action keep waiting (Blocking) for the client to complete this amount. 1. I observed that when I close the connection it automatically flushes out the stream but I am using the connection for multiple read so it will be open until I choose to close it. 2. That is why I mentioned that I used timeout socket method since I know that the client have finish reading the bytes from server irrespective of the fact that it still waits for more bytes that the client does not have. I think I have to try JAVA NIO in Java 7 API since it has non-blocking read functionality. Thanks for your help |
| Re: Problem With Bufferedinputstream Read Blocking Issue In Java by BetaQuick: 7:32pm On Oct 16, 2013 |
yea but in your code you are looking for the -1 while ((nRead = bis.read(data, 0, data.length)) != -1) That wont happen since your socket remains open. You have to stop when you have the bytes expected OR put a special character to stop reading. Since your client is sending less than 8192, I would go for the special character to signify end of data. Good Luck! |
| Re: Problem With Bufferedinputstream Read Blocking Issue In Java by ToyinDipo(m): 8:05pm On Oct 16, 2013*. Modified: 10:20pm On Oct 16, 2013 |
I don't know whether this that I'm posting might help. I wrote the sh1t 5 years ago while still in school, I don't even understand half of what it means again. But since you are still active in java programming you should be able to decode it. It designed it to transfer files between two PC's. Server side (which must be run first) /* Client side /* |
. • I Want To Develop A Software In Java To Calculate CGPA • How Do Io Create Ribbon Ui In Java • 2 • 3 • 4
Mr Seun Please SSL Enable Nairaland • Capture Image From Webcam And Store In Mysql Database From C Sharp Winform • Python
;