Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-19-2007, 03:07 PM
Member
 
Join Date: Dec 2007
Location: Portugal
Posts: 2
aikanaro is on a distinguished road
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.

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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-15-2008, 09:55 PM
undertow's Avatar
Member
 
Join Date: Jan 2008
Location: Colorado USA
Posts: 12
undertow is on a distinguished road
Send a message via AIM to undertow Send a message via Skype™ to undertow
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
reading text character by character bugger New To Java 2 11-09-2007 09:54 PM
How to use BufferedReader? ProgramBeginner New To Java 2 11-04-2007 10:08 AM
Problem with display the character romina New To Java 1 07-25-2007 08:43 PM
Problem with '/' character in HTML and JSP Marcus JavaServer Pages (JSP) and JSTL 2 07-04-2007 06:32 AM
Problem when sending application server with RAD 6 Marcus Advanced Java 2 07-02-2007 07:44 AM


All times are GMT +3. The time now is 02:30 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org