[java] Scanner vs BufferedReader

Started by
0 comments, last by CaptainJester 13 years, 11 months ago
I'm finishing up my final, which is a simple top-down 2D tank shooter with LAN networking. I used to use a Scanner and Formatter pair to read or write to the streams on the socket, but since the Scanner's nextLine() method blocks until it receives data, I had to put it in its own thread to the server could still send player data around without having to wait until it receives data. This makes it so up to four threads (two server-side, two client-side) are running for every player. I tried to switch the Scanner with a BufferedReader (which has the ready() method which checks for available data without blocking) and a DataInputStream (which has a similar method), but they both cause the data transfer over the server to lag horribly, and the amount of lag would increase the longer the program was running. My question is this: is there a class that is fast and reliable like the Scanner that provides a method that checks for input without blocking? Or is there a way to use the Scanner or BufferedReader in a way that would fix their respective issues?
Advertisement
If you have time to rewrite the networking code I would recommend using NIO. If you need some help to get started with NIO I posted a bare bones FTP server to JavaGaming not too long ago. http://www.javagaming.org/index.php/topic,22117.0.html. I did end up using threads in the example, but it can be done without threads as well. NIO can handle at least in the hundreds of connections, which is plenty for your school project.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]

This topic is closed to new replies.

Advertisement