LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-19-2012, 11:17 PM   #1
texasone
Member
 
Registered: Jun 2008
Location: /home/lorax
Distribution: Debian Testing
Posts: 141

Rep: Reputation: Disabled
Java bufferedreader -- how to read carriage return/new line sent over network from C


Basically, I have a server in C that is sending a string of text over the using write. Then I have a client written in Java that is receiving the string and updating the text in a JTextArea. The problem I am having is that when I send the string, all carriage returns or new lines are lost and its just sending it as one long line. Does anyone know how to fix this or a little workaround hack for it?
C server code:
Code:
struct meeting newMeet; 
newMeet = agenda(); 
char * s = NULL; 
ticks = time(NULL); 
snprintf(buffer, BUFFER_SIZE, "%.24s \r\n%s, %s at %d in %s. %s is on the agenda.\r\n",
ctime(&ticks), newMeet.event, newMeet.day, newMeet.time, newMeet.where, newMeet.agenda);
buffer_length = strlen(buffer); 
if (write(connection_handle, buffer, buffer_length) != buffer_length) { 
    perror("Write failed"); 
}
Java Client Code:
Code:
InetAddress serverAddress = InetAddress.getByName(host);
Socket clientSocket = new Socket(serverAddress, port);
InputStream input= clientSocket.getInputStream();

BufferedReader r = new BufferedReader( new InputStreamReader(input)); 
String s ; 
String text = ""; 
while ( (s = r.readLine()) != null) {
     text = text + s; 
} 
infoArea.setText(text + "\r\n" + TilMeet);
Let me know if you need the full code. Thanks in advance, RzITex
 
Old 01-20-2012, 03:05 AM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Since Java readLine() does not include the newline in the results, you'll need to add it back yourself:
Code:
text = text + s + "\n";
EDIT: As Proud noted below, JTextArea lines should be separated by \n and not by \r\n as I initially had. Thanks, Proud, for the heads-up.

Last edited by Nominal Animal; 01-20-2012 at 01:59 PM.
 
Old 01-20-2012, 08:15 AM   #3
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
I would suggest the system-specific newline char sequence, but then...
Quote:
Code:
public static String newline = System.getProperty("line.separator");
When NOT to use the system independent newline characters

JTextArea
lines should be separated by a single '\n' character, not the sequence that is used for file line separators in the operating system.

Console output (eg, System.out.println()), works fine with '\n', even on Windows.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Line feed or carriage return-linefeed? Aka \n or \r? stf92 General 10 01-20-2012 12:55 AM
Line wrap on embedded system issues carriage return but no linefeed crunchytheory Linux - Embedded & Single-board computer 4 09-18-2011 12:20 AM
read carriage return code elainelaw Linux - Newbie 4 03-10-2010 12:04 AM
Remove New Line or Carriage return from Text File DIMonS Programming 13 10-01-2007 10:31 AM
java.io.BufferedReader, start from line x? student04 Programming 1 12-21-2004 09:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration