troubleshooting Question

About BufferedReader

Avatar of william007
william007 asked on
9 Comments1 Solution489 ViewsLast Modified:
I am trying to build a mini webserver. The program below suppose to displayed the request of the client in the console, and wait for another connection...but it seems to block at
sContent = sockInput.readLine();
it seems to me that I am not using a correct approach to print the entire request from the client to the console, reason maybe the bufferedreader will not return null if there is nothing to read and simply will block there. Am I right?

What is the correction approach to display the request of the client to the console(by using bufferedreader) and not blocking at the middle if there is nothing to read?

public class Test {
      public static void main(String args[]) throws Exception {

            ServerSocket serverSocket = new ServerSocket(10000);
            while (true) {
                  Socket sock = serverSocket.accept();//Wait for connection
                  BufferedReader sockInput = new BufferedReader(
                              new InputStreamReader(sock.getInputStream()));
                  String sContent = sockInput.readLine();
                  while (sContent != null) {
                        sContent = sockInput.readLine();//<--Block here
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 9 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 9 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros