0

I'm trying to determine the best way to transfer data through a socket between a client and server. Currently I have a BufferedReader that reads one character at a time (or however many characters have arrived since the last iteration). Through each iteration, it pulls the data received so far and puts it into an array. When the '|' character is read, it knows that the current instruction is done.

I know what I have so far is grossly inefficient and burns the CPU, but I'm a little unclear as to the differences between all the ways to read from a socket input stream. What would I use to not have to read each character at a time, but rather to wait until the input stream is finished receiving the current instruction (which would be terminated by "\n")?

3
  • 7
    If your commands are \n-terminated, why read char by char at all instead of calling BufferedReader.readLine()? Commented Nov 10, 2013 at 23:58
  • As I understand it, and perhaps I may be wrong, doesn't readline read what's currently in the buffer? So Let's say I'm sending the word "hello", and only "he" has arrived, doesn't it only read that? Commented Nov 11, 2013 at 0:07
  • 1
    Readline blocks until the full line is read. You get partial data only for the char array based read() methods. Commented Nov 11, 2013 at 0:17

1 Answer 1

1

I find the best way is to create a new thread for each socket/client that listens for input. readLine() always works for me fine. Perhaps this might help you out a bit.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.