problem with chatting

my code is like this...

Socket s=new Socket(ipaddress,portno)

PrintWriter to=new PrintWriter(s.getOutputStream,true);

to.println(textArea.getText());

this is the code in sending.....

and this is at receiving side....

ServerSocket ss=new ServerSocket(portno);

while(true)

{

Socket c=ss.accept();

BufferdReader from=new BUfferdReader(new inputStreamreader(c.getInputStream());

String h= from.readLine();

and then i try to print h ...

it doesnt contain enire data...

its only single line....

plz help..

[597 byte] By [deepak_cuceka] at [2007-11-27 2:05:26]
# 1
That's what readLine() does. It reads a line. If you want more lines, read them.
ejpa at 2007-7-12 1:50:48 > top of Java-index,Core,Core APIs...
# 2
ya then i modify the code as belowString h="";while ((h=from.readLine())!=null){//print}but the problem is that i cant exit from the while loopplz help
deepak_cuceka at 2007-7-12 1:50:48 > top of Java-index,Core,Core APIs...
# 3

You won't exit from the while loop until the other end closes the file.

If you don't want this behaviour and you don't know how many lines there are to be read at a time, you'll have to have the sender teill you in advance, e.g. by writing the line count before the next bunch of lines.

ejpa at 2007-7-12 1:50:48 > top of Java-index,Core,Core APIs...
# 4
can you specify the code....plz
deepak_cuceka at 2007-7-12 1:50:48 > top of Java-index,Core,Core APIs...
# 5
This may come as a shock but this isn't a code-writing service actually. Why don't you try to work it out for yourself?
ejpa at 2007-7-12 1:50:48 > top of Java-index,Core,Core APIs...
# 6
sure i will try.....but i need guidance from you all...bcoz am new to java
deepak_cuceka at 2007-7-12 1:50:49 > top of Java-index,Core,Core APIs...
# 7
You've had all the guidance in Java you need. It's now an application-protocol problem.
ejpa at 2007-7-12 1:50:49 > top of Java-index,Core,Core APIs...
# 8
finding the number of rows in textarea and send throgh socket..is ithis a proper method...is there any other solution ...?
deepak_cuceka at 2007-7-12 1:50:49 > top of Java-index,Core,Core APIs...
# 9
What do you thiink? Does that constitute 'writing the line count before the next bunch of lines'? And if it does why would you need another solution?
ejpa at 2007-7-12 1:50:49 > top of Java-index,Core,Core APIs...
# 10
am sending data through socket by this method..to.println(textArea.getText());if i have one more data ie number of lines how can i send
deepak_cuceka at 2007-7-12 1:50:49 > top of Java-index,Core,Core APIs...
# 11

Hmm... A very primitive, unsecure, yet simple to implement, and on-par with the level of programming you're applying, way to do this is to add, right before this:

to.println(Integer.toString(textArea.getText().split("\n").length));

What this does is takes the text, counts its lines by splitting the text into an array of lines(using the line-ending character as a seperator for the split) and then taking the length of the array, and finally it converts it to a string and prints it as the first line.

On the receiver end, you should read the first line seperately from the loop, and convert it to an integer using "Integer.valueOf".

And for the love of security, be sure to account for the cases where a first line is never sent, or that the first line is not an integer, treat it as a "fatal error", and promptly close the receiving socket. Do not attempt to continue reading the socket given a malformed first line.

Once you have the number of lines as an integer, the next part is a for loop that reads all of the lines. Again, for the love of security, be ready for the case that the socket closes before all the lines get through, or some other error occurs, and simply discard everything received and close the socket in that event. Do not attempt to continue running after such an error.

SlugFillera at 2007-7-12 1:50:49 > top of Java-index,Core,Core APIs...
# 12
higot your idea...on the receiving side ...i wrote like this..h=from.readLine();but this contains only the number of lines...i didnt get any data....how i can get the data along with this number of lines...plz help...
deepak_cuceka at 2007-7-12 1:50:49 > top of Java-index,Core,Core APIs...
# 13
any one plz help on above problem
deepak_cuceka at 2007-7-12 1:50:49 > top of Java-index,Core,Core APIs...
# 14
Now that you've got the number of lines, read that number of lines?
ejpa at 2007-7-12 1:50:49 > top of Java-index,Core,Core APIs...
# 15

i changed the code as follows

on the sending side i attach the number of lines with string...

on the recieving side

i extract first charcter ie the number of lines..

then

for(i=0;i<=number oflines;i++)

{

h=from.readLine();

String h1=h1.concat(h);

}

but still i cant exit from the loop

deepak_cuceka at 2007-7-21 20:18:53 > top of Java-index,Core,Core APIs...
# 16
That's because you're executing it one too many times. i = 0; i <= N; i++ executes the loop N+1 times. Start from one or change the condition to i < N.
ejpa at 2007-7-21 20:18:53 > top of Java-index,Core,Core APIs...