Results 1 to 16 of 16
  1. #1
    space.puffin is offline Member
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default readLine() not working.

    Hi All.

    I'm building a simple Chat client / server. The Client connects to the server and both write to each other, but no output is displayed by each after the readLine() statement. I'm using Threads.

    ChatServer.java:
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package chatserver;
    
    import java.io.BufferedReader;
    import java.io.DataInputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    /**
     *
     * @author Rob
     */
    public class ChatServer implements Runnable {
        int x[] = new int[4];
        int y[] = new int[4];
        BufferedReader reader;
        PrintWriter writer;
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            ChatServer server = new ChatServer();
            server.run();
            
            
         
        }
    
        
        public void run() {
            System.out.println("hello");
            connect();
            Thread readerThread = new Thread(new IncomingReader());
            Thread writerThread = new Thread(new OutgoingWriter());
            readerThread.start();
            writerThread.start();
        }
        
        public  void connect() {
              try {
                ServerSocket serverSock = new ServerSocket(4242);
                
                while (true) {
                    Socket sock = serverSock.accept();
                    writer = new PrintWriter(sock.getOutputStream());
                    reader = new BufferedReader(new InputStreamReader(sock.getInputStream()));
                    System.out.println("NEtowrking established");
                 }
            } catch (IOException ex) {
               ex.printStackTrace();
            }
            
        }
    
            public class IncomingReader implements Runnable {
                public void run() {
                try {
                    String result = reader.readLine();
                    reader.close();
                    System.out.println("read " + result);
                    writer.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                }
            }
    
            public class OutgoingWriter implements Runnable {
                public void run() {
                    
                    writer.println("hello");
                    System.out.println("Written");
                    
                    
                    }
                                  
                }
    }
    ChatClient.java
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package chatclient;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Socket;
    
    /**
     *
     * @author Rob
     */
    public class ChatClient {
    
        BufferedReader reader;
        PrintWriter writer;
        Socket sock;
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            ChatClient client = new ChatClient();
            client.SetUpNetworking();
        }
    
        private void SetUpNetworking() {
            try {
                sock = new Socket("127.0.0.1", 4242);
                InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
                reader = new BufferedReader(streamReader);
                writer = new PrintWriter(sock.getOutputStream());
                System.out.println("Netowrking Established");
                Thread readerThread = new Thread(new IncomingReader(reader));
                Thread writerThread = new Thread(new OutgoingWriter(writer));
                readerThread.start();
                writerThread.start();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        class IncomingReader implements Runnable{
            private BufferedReader reader;
            
            public IncomingReader(BufferedReader reader) {
            this.reader = reader;
        }
            
                public void run() {   
                try {   
                    String result = reader.readLine();   
                    reader.close();   
                    System.out.println("read " + result);   
                       
                } catch (IOException ex) {   
                    ex.printStackTrace();   
                }   
            
        }
        }
                public class OutgoingWriter implements Runnable {   
                private PrintWriter writer;
    
                public OutgoingWriter(PrintWriter writer) {
                    this.writer = writer;
                }
    
                public void run() {   
                    writer.println("hello");   
                    System.out.println("Written");   
                    }   
                                     
                }   
        }

  2. #2
    space.puffin is offline Member
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default Re: readLine() not working.

    Just realised the thread IncomingReader won't execute, if the OutgoingWriter thread is run before the incomingReader thread it won't execute. No idea as to why.

    Thanks in advance.
    Last edited by space.puffin; 09-20-2011 at 04:09 PM.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default Re: readLine() not working.

    You need to add lots more printlns so you can see where the execution flow goes and where it is getting hung.

  4. #4
    space.puffin is offline Member
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default Re: readLine() not working.

    Odd.

    After the try / catch block of connect() System.out.println won't show.
    Last edited by space.puffin; 09-20-2011 at 06:04 PM.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default Re: readLine() not working.

    Try flushing the writer after writing.

  6. #6
    space.puffin is offline Member
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default Re: readLine() not working.

    writer.flush() makes no difference. IncomingReader won't execute.

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default Re: readLine() not working.

    Remove all the close statements.

  8. #8
    space.puffin is offline Member
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default Re: readLine() not working.

    ^ tried that already.

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default Re: readLine() not working.

    Here is what is printed when I execute the code:
    Java Code:
    CS - Starting
    CC- Netowrking Established
    CS - Waiting on connnect
    CC - IR waiting
    CC - writing
    CC Written
    CS - NetWorking established
    CS back from connect
    CS - IR waiting
    CS read hello from CC<<<<<<<<<<<<<<<<
    CS - sending message
    CC - read hello from CS>>>>>>>>>>>>>>>
    CS Written
    
    0 error(s)

  10. #10
    space.puffin is offline Member
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default Re: readLine() not working.

    Must be an error in my environment. Have tried compiling and running both classes from the commandline. I have the latest version of the jdk installed.
    Last edited by space.puffin; 09-20-2011 at 07:02 PM.

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default Re: readLine() not working.

    Did you notice these lines in my print out?

    CS back from connect
    CS - IR waiting


    Did you add print statements at those locations in the code?

  12. #12
    space.puffin is offline Member
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default Re: readLine() not working.

    Thanks Norm.

    I have println statements after connect in ChatServer.main after connect() .and at the start of IR. neither is printed.

  13. #13
    space.puffin is offline Member
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default Re: readLine() not working.

    ChatServer.java.
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    import java.io.BufferedReader;
    import java.io.DataInputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    /**
     *
     * @author Rob
     */
    public class ChatServer implements Runnable {
        int x[] = new int[4];
        int y[] = new int[4];
        BufferedReader reader;
        PrintWriter writer;
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            ChatServer server = new ChatServer();
            server.run();
            
            
         
        }
    
        
        public void run() {
            System.out.println("hello");
            connect();
            System.out.println("going to start thread");
            Thread readerThread = new Thread(new IncomingReader());
            System.out.println("reader started");
            Thread writerThread = new Thread(new OutgoingWriter());
            
            writerThread.start();
            readerThread.start();
            
        }
        
        public  void connect() {
              try {
                ServerSocket serverSock = new ServerSocket(4242);
                
                while (true) {
                    Socket sock = serverSock.accept();
                    writer = new PrintWriter(sock.getOutputStream());
                    reader = new BufferedReader(new InputStreamReader(sock.getInputStream()));
                    System.out.println("NEtowrking established");
                    
                 }
            } catch (IOException ex) {
               ex.printStackTrace();
            }
              System.out.println("connect finished");
            
        }
    
            public class IncomingReader implements Runnable {
                public void run() {
                    System.out.println("this far");
                try {
                    
                    String result = reader.readLine();
                    
                    reader.close();
                    
                    System.out.println("read " + result);
                    writer.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                }
            }
    
            public class OutgoingWriter implements Runnable {
                public void run() {
                    try {
                    writer.println("hello");
                    System.out.println("Written");
                    Thread.sleep(2000);
                            
                    
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                                  
                }
            }
    }
    ChatClient.java
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Socket;
    
    /**
     *
     * @author Rob
     */
    public class ChatClient {
    
        BufferedReader reader;
        PrintWriter writer;
        Socket sock;
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            ChatClient client = new ChatClient();
            client.SetUpNetworking();
        }
    
        private void SetUpNetworking() {
            try {
                sock = new Socket("127.0.0.1", 4242);
                InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
                reader = new BufferedReader(streamReader);
                writer = new PrintWriter(sock.getOutputStream());
                System.out.println("Netowrking Established");
                Thread readerThread = new Thread(new IncomingReader(reader));
                Thread writerThread = new Thread(new OutgoingWriter(writer));
                readerThread.start();
                writerThread.start();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        class IncomingReader implements Runnable{
            private BufferedReader reader;
            
            public IncomingReader(BufferedReader reader) {
            this.reader = reader;
        }
            
                public void run() {   
                try {   
                    String result = reader.readLine();   
                    reader.close();   
                    System.out.println("read " + result);   
                       
                } catch (IOException ex) {   
                    ex.printStackTrace();   
                }   
            
        }
        }
                public class OutgoingWriter implements Runnable {   
                private PrintWriter writer;
    
                public OutgoingWriter(PrintWriter writer) {
                    this.writer = writer;
                }
    
                public void run() {   
                    writer.println("hello");   
                    writer.flush();
                    System.out.println("Written");   
                    }   
                                     
                }   
        }
    Output:
    ChatServer:
    hello
    NEtowrking established
    ChatClient:

    Netowrking Established
    Written

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default Re: readLine() not working.

    neither is printed.
    That means those statements were never executed.
    I think they do need to be executed, so you need to see why they weren't

  15. #15
    space.puffin is offline Member
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default Re: readLine() not working.

    Thanks again Norm.

    If it runs fine on your machine is it not an environment problem. I've tried running the files on windows and linux to no avail. As for the threads not running i have no idea why they don't.

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default Re: readLine() not working.

    Have you looked at your code to see why this print statement is NOT being executed?

    System.out.println("going to start thread");

Similar Threads

  1. Readline from an input stream and nothing more.
    By couling in forum Advanced Java
    Replies: 19
    Last Post: 05-27-2011, 09:20 PM
  2. readLine() for loop
    By Billywizz in forum New To Java
    Replies: 15
    Last Post: 04-06-2011, 11:37 AM
  3. Replies: 10
    Last Post: 01-17-2009, 08:30 PM
  4. problem with console.readLine()
    By thatguy in forum New To Java
    Replies: 1
    Last Post: 12-14-2008, 07:40 PM
  5. DataInputStream readLine()
    By ravian in forum New To Java
    Replies: 2
    Last Post: 11-26-2007, 10:44 PM

Posting Permissions

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