By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,962 Members | 1,998 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,962 IT Pros & Developers. It's quick & easy.

Sockets, PrintWriter and BufferedReader question

P: n/a
i have a client-server application. client and server should communicate via
tcp sockets. ok, so i use Sockets, PrintWriter and BufferedReader. the
problem is that: both client and server will send each other multiple lines
(using PrintWriter.println()) at a time, and i don't know how many lines
each of them will send. this wouldn't be a problem if a sender could end his
turn with a pre-defined character or string (e.g. "\n", "bye" or something)
in a last line it sends. but i'm not allowed to do that. i can't not put any
additional characters in a message.

so i thought this could be done by closing the PrintWriter (but not the
Socket) on the sender's side when i want to signal end of transmission. on
the receiveing side this causes the recipient to exit from a "while( (line =
bufferedReader.readLine()) != null )" loop. i figured that i coud then
initialize new BufferedReader and PrintWriter from the same socket, and
continue communication. this actually kind of works, but only once. :) the
problem is that this way i also close the socket, and therefore can't
initialize new BufferedReader and PrintWriter. does anybody have any idea
how to solve this?

again, this is what i tried to do. i start with:

server side:

socket = serverSocket.accept();
BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
while( (line = in.readLine()) != null ) {
message = message + line;
}
in.close();

client side:

PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println(message);
// this message actually contains a number of lines divided with "\n"
out.close();

this far it works perfectly. but if i now want a server to reply, trying
something like:

server side:

PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println(message);
out.close();

client side:

BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
while( (line = in.readLine()) != null ) {
message = message + line;
}
in.close();

just doesn't work. the socket closed when i did out.close() and in.close()
earlier, so now i can't create new BufferedReader and PrintWriter. of
course, i COULD create a new TCP connection and break it up a new each time
i have to send something, but i really don't see the point in that... any
ideas?

thanks.
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi!
To be honest I didn't quite understand the problem..
I am kind of a newbee in sockets myself... but all I have managed to
implement, I attribute to the following sun tutorial:
http://java.sun.com/docs/books/tutor...ets/index.html
Especially helpful is the title "reading from and writing to a socket".
Check it out you might find it useful.

Also what you are really concerned with is a) how to know that it is the end
of one transmission from ONE side or b) how to end transmissions from both
sides and close connection ??

This is my first suggestion but I promise to look closer at your code and
see if I can suggest more.

Maria

"Dr.Kadzija" <dr********@mrbmrb.comx> wrote in message
news:bv**********@bagan.srce.hr...
i have a client-server application. client and server should communicate via tcp sockets. ok, so i use Sockets, PrintWriter and BufferedReader. the
problem is that: both client and server will send each other multiple lines (using PrintWriter.println()) at a time, and i don't know how many lines
each of them will send. this wouldn't be a problem if a sender could end his turn with a pre-defined character or string (e.g. "\n", "bye" or something) in a last line it sends. but i'm not allowed to do that. i can't not put any additional characters in a message.

so i thought this could be done by closing the PrintWriter (but not the
Socket) on the sender's side when i want to signal end of transmission. on
the receiveing side this causes the recipient to exit from a "while( (line = bufferedReader.readLine()) != null )" loop. i figured that i coud then
initialize new BufferedReader and PrintWriter from the same socket, and
continue communication. this actually kind of works, but only once. :) the
problem is that this way i also close the socket, and therefore can't
initialize new BufferedReader and PrintWriter. does anybody have any idea
how to solve this?

again, this is what i tried to do. i start with:

server side:

socket = serverSocket.accept();
BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
while( (line = in.readLine()) != null ) {
message = message + line;
}
in.close();

client side:

PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println(message);
// this message actually contains a number of lines divided with "\n"
out.close();

this far it works perfectly. but if i now want a server to reply, trying
something like:

server side:

PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println(message);
out.close();

client side:

BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
while( (line = in.readLine()) != null ) {
message = message + line;
}
in.close();

just doesn't work. the socket closed when i did out.close() and in.close()
earlier, so now i can't create new BufferedReader and PrintWriter. of
course, i COULD create a new TCP connection and break it up a new each time i have to send something, but i really don't see the point in that... any
ideas?

thanks.

Jul 17 '05 #2

P: n/a
Why do you need to know when the sender has finished? Why would it be wrong
to just sit in an endless while (true) { getline(); processline(); } loop?

--
Chris Gray ch***@kiffer.eunet.be
/k/ Embedded Java Solutions

Jul 17 '05 #3

P: n/a
"chris" <ch***@kiffer.eunet.be> wrote in message
news:c0**********@reader11.wxs.nl...
Why do you need to know when the sender has finished? Why would it be wrong to just sit in an endless while (true) { getline(); processline(); } loop?


because server needs to receive more than one line before it processes what
it received. but there is no way for server to know when the client is done
sending just from the content of what server received. and server doesn't
know in advance how many lines the client is about to send. is this making
any sense? :)

--
Dr.Kadzija
Jul 17 '05 #4

P: n/a
"Maria Gaitani" <M.*******@warwick.ac.uk> wrote in message
news:40***********************@lovejoy.zen.co.uk.. .
Hi!
To be honest I didn't quite understand the problem..
I am kind of a newbee in sockets myself... but all I have managed to
implement, I attribute to the following sun tutorial:
http://java.sun.com/docs/books/tutor...ets/index.html
Especially helpful is the title "reading from and writing to a socket".
Check it out you might find it useful.

Also what you are really concerned with is a) how to know that it is the end of one transmission from ONE side or b) how to end transmissions from both
sides and close connection ??

This is my first suggestion but I promise to look closer at your code and
see if I can suggest more.


thanks for your effort. i tried to clarify my problem a bit more in my reply
to chris.

--
Dr.Kadzija
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.