Results 1 to 2 of 2
  1. #1
    aikanaro is offline Member
    Join Date
    Dec 2007
    Location
    Portugal
    Posts
    2
    Rep Power
    0

    Default Problem either with BufferedReader or sending a newline character

    Hi everyone,

    I've developed a small application to test a connection to an asterisk server (the same connection can be tested using telnet so I know it's working ok on the server side) in my private lan but it's failing and I've run out of ideas. I'd gladly appreciate any help on this. As it's quite short I'm posting my code below.

    Short note: I have to send a package that ends with 2 CRLF hence the empty string as the last line added to the arraylist.

    Again thanks for any help you might give me.

    Java Code:
    public static void main(String[] args) {
            try {           
                Socket socket = new Socket("192.168.1.5", 5038);
                PrintWriter out = new PrintWriter(socket.getOutputStream(), false);
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
                String dataIn = "";
                String dataOut = "";
                ArrayList data = new ArrayList();
    
                data.add("action: login");
                data.add("username: admin");
                data.add("secret: password");
                data.add("");
                
                while(true) {
    
                    if(input.readLine().equalsIgnoreCase("q"))
                        System.exit(0);
    
                    System.out.println("ready() value: " + in.ready());
    
                    while(in.ready()) {
                        dataIn = in.readLine();
                        System.out.println("From server: " + dataIn +
                                " and ready() value: " + in.ready());
                    }
                    
                    if(dataOut.equalsIgnoreCase("")) {
                        out.println(data.get(0));
                        System.out.println("To server: " + data.get(0));
                        out.println(data.get(1));
                        System.out.println("To server: " + data.get(1));
                        out.println(data.get(2));
                        System.out.println("To server: " + data.get(2));
                        out.println(data.get(3));
                        System.out.println("To server: " + data.get(3));
                        dataOut = "Sent!";
                    }
                }
            } catch (UnknownHostException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    P.S: if you need any more info just say so. Thanks again. :)

  2. #2
    undertow's Avatar
    undertow is offline Member
    Join Date
    Jan 2008
    Location
    Colorado USA
    Posts
    12
    Rep Power
    0

    Default

    looks to me that you are not sending the 2 CRLF characters. in the last add of your data arraylist i would suggest using "\r\n" and transmit with 'println' or, if you really want to be sure use "\r\n\r\n" and transmit that string with 'print' rather than 'println'. I hope that helps.

    and dont forget to call flush() on the out object when you are done transmitting just to be sure the entire buffer is sent out.
    Last edited by undertow; 01-15-2008 at 09:57 PM.

Similar Threads

  1. How to use BufferedReader?
    By ProgramBeginner in forum New To Java
    Replies: 4
    Last Post: 10-11-2011, 03:56 PM
  2. reading text character by character
    By bugger in forum New To Java
    Replies: 2
    Last Post: 11-09-2007, 09:54 PM
  3. Problem with display the character
    By romina in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 08:43 PM
  4. Problem with '/' character in HTML and JSP
    By Marcus in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 07-04-2007, 06:32 AM
  5. Problem when sending application server with RAD 6
    By Marcus in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 07:44 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
  •