Sign in to follow this  
Abion47

[java] Scanner vs BufferedReader

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this