472,353 Members | 1,654 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

Sockets, PrintWriter and BufferedReader question

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
4 15444
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
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
"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: JatP | last post by:
hi Everyone I am trying to create a server and client to send files from one side to the other. I can send files from one side to the other using...
4
by: crashnburn | last post by:
Hi there, I was wondering if C++ has some kind of default new socket wrapper, that avoids many lines of code. Maybe there is a good C++ library?...
6
by: AA | last post by:
I am writing a socket class that implements the System.Net and System.Net.Sockets namespaces. My Question is: If I need to send a large amount...
0
by: Naveen Mukkelli | last post by:
Hi, I'm using multicast using C#. When receiver disconnects from the server and reconnects, following exception is occuring ...
4
by: Michael Evanchik | last post by:
im having a small bug now where if i give a http header GET / with a connection: keep-alive i cant tell when the server is done sending. If i say...
2
by: ZorpiedoMan | last post by:
I'm new to the world of sockets, and this question is not VB specific: If multiple clients access the same server on the same port, and the server...
2
by: ZorpiedoMan | last post by:
I'm new to the world of sockets, and this question is not VB specific: If multiple clients access the same server on the same port, and the server...
1
by: =?Utf-8?B?UmFqbmk=?= | last post by:
Dear Sir/Mam, I have written a server code using the Windows Socket API's. Wherein I have created the socket and bound it to a particular IP...
0
by: =?Utf-8?B?UmFqbmk=?= | last post by:
Dear William Stacey, I have written a server code using the Windows Socket API's. Wherein I have created the socket and bound it to a particular...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.