Results 1 to 4 of 4
  1. #1
    Onra is offline Member
    Join Date
    Nov 2009
    Posts
    41
    Rep Power
    0

    Default BufferedReader makes me rage

    Hello,

    here is an echo server code:

    Java Code:
    public class EchoServer {
    	
    	public static void main(String[] args) {
    		try {
    			serverSocket = new ServerSocket(1234);
    		} catch (IOException e) {
    			System.out.println("Could not listen on port: 1234");
    			System.exit(-1);
    		}
    		Socket clientSocket = null;
    	 	try {
    			clientSocket = serverSocket.accept();
    		} catch (IOException e) {
    			System.out.println("Accept failed: 1234");
    			System.exit(-1);
    		}
    		PrintWriter out = new PrintWriter(
    			clientSocket.getOutputStream(), true);
    		BufferedReader in = new BufferedReader(
    			new InputStreamReader(
    				clientSocket.getInputStream()));
    		String inputLine, outputLine;
    
    		while ((inputLine = in.readLine()) != null) {	
    			outputLine = "Server: " + inputline;
    			out.println(outputLine);
    			if outputLine.equals("bye"))
    			break;
    		}
    
    		out.close();
    		n.close();
    		clientSocket.close();
    		serverSocket.close();
    	}
    }
    The client works accordingly. Reads with a BufferedReader from the stream and writes console input into the stream with a printwriter.

    The problem here is that the freaking BufferedReader returns a null on the second read attempt and the loop breaks. Meaning I can only read one line from the client and not more.
    What's the problem?

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Does your client drop the connection after having written a single line?

    kind regards,

    Jos

  3. #3
    Onra is offline Member
    Join Date
    Nov 2009
    Posts
    41
    Rep Power
    0

    Default

    No, it continues with its loop.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Onra View Post
    No, it continues with its loop.
    And does the client receive the line sent by the server? Maybe you can show us (the relevant part(s) of) your client code.

    kind regards,

    Jos

Similar Threads

  1. Replies: 8
    Last Post: 04-10-2010, 01:17 AM
  2. JAVA-SIMPL makes its first release
    By bobicanprogram in forum Java Software
    Replies: 0
    Last Post: 03-31-2009, 07:10 PM
  3. Replies: 3
    Last Post: 03-04-2009, 02:56 PM
  4. What makes a good programmer?
    By cruxblack in forum Forum Lobby
    Replies: 35
    Last Post: 11-16-2008, 04:22 AM
  5. Never makes it to the method!
    By minihazard10 in forum New To Java
    Replies: 9
    Last Post: 11-10-2008, 07:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •