0

I am currently working on a Client/Server project. Up until now both have been in C++ and now I am making my client Java based. In order to receive from the Server I have been using a

BufferedReader in = new BufferedReader(new InputStreamReader(Socket.getInputStream()));

and Have been using

in.readLine()

The problem is when I need to receive multiple lines from the Server, the client stops after the first line because of the '\n' character.

How do i avoid this happening and receive all of the info?

I thought of using a char[] like this:

char[] buffer = new char[1024];

but the problem is that when the client receives the next message there is still left overs in the buffer.

Any help would be great!

Thanks

user2955610
  • 815
  • 2
  • 9
  • 15
  • Does the server close the socket? If not, how would you expect to know when it's finished writing? – Jon Skeet Jan 07 '16 at 16:21
  • You need to inform the server how much data it should expect from the client by sending it the data size at the very beginning. Another approach can be parsing the received data in search of a _magic_ value, so that the server knows this is the end of the data. –  Jan 07 '16 at 16:27
  • 1
    You just read until your read all the data. The question on how to communicate the size of the data to the receiver remains open. In most extreme case, server just closes connection after all the data was sent, so you keep reading untill you can't read anymore. – SergeyA Jan 07 '16 at 16:28
  • Thee server does not close the socket, it stays open for multiple send/receive actions. The server knows how much to read, its the client thats the problem – user2955610 Jan 07 '16 at 16:52
  • Do you call `readLine()` only once?. If you do, you should call `readLine()` in a loop as long as something other than `null` is read. – Whateverest Jan 07 '16 at 18:11
  • 1
    Just keep calling `readLine` until you have all the information you want. – David Schwartz Jan 07 '16 at 19:08

0 Answers0