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

Deadlock situation while reading from sockets

P: n/a
Hi,

I am implementing a server that reads for socket connections at a
port, and processes the socket on a separate thread. We do not control
the client implementation. Here is the scenario.

0. Client connects to the socket.
1. Client writes to the socket.
2. Server reads from the socket.
3. Client waits for the response on the socket
4. Server writes to the socket.
5. Socket is closed.

Incoming request on the socket is not guaranteed to be terminated with
any particular sequence of characters. How does the server know how
many bytes to read, and when to stop reading?

Here are couple of things I tried.

1. readln on a buffered reader on the server side would block till it
receives the "eol" character.
2. I have tried reading x number of characters at a time, and thought
if number of characters read are less than the attempted number of
characters to read (or the read method returns -1) then read all the
data sent by the client. This works if the attempted characters to
read initially, are greater than the number of characters sent by the
client, but not if it is the other way.

I have used BufferedReader, and the PrintWriter classes.

I appreciate your help in resolving this issue.

Thanks.
Jul 17 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
When the client connects to the socket it only writes the one time then
waits, so you can use the BufferedReader to read all of the data on the
socket. Each communication from the client has a new socket, so you
don't have to maintain a connection and wonder how much data is being
sent. When the socket is flushed to the server, it will append the eof
to the stream. Hopes this helps at all.

-C

javastudent wrote:
Hi,

I am implementing a server that reads for socket connections at a
port, and processes the socket on a separate thread. We do not control
the client implementation. Here is the scenario.

0. Client connects to the socket.
1. Client writes to the socket.
2. Server reads from the socket.
3. Client waits for the response on the socket
4. Server writes to the socket.
5. Socket is closed.

Incoming request on the socket is not guaranteed to be terminated with
any particular sequence of characters. How does the server know how
many bytes to read, and when to stop reading?

Here are couple of things I tried.

1. readln on a buffered reader on the server side would block till it
receives the "eol" character.
2. I have tried reading x number of characters at a time, and thought
if number of characters read are less than the attempted number of
characters to read (or the read method returns -1) then read all the
data sent by the client. This works if the attempted characters to
read initially, are greater than the number of characters sent by the
client, but not if it is the other way.

I have used BufferedReader, and the PrintWriter classes.

I appreciate your help in resolving this issue.

Thanks.


Jul 17 '05 #2

P: n/a
Chris Rohr <cj****@verizon.net> wrote in message news:<OA*****************@nwrddc03.gnilink.net>...
When the client connects to the socket it only writes the one time then
waits, so you can use the BufferedReader to read all of the data on the
socket. Each communication from the client has a new socket, so you
don't have to maintain a connection and wonder how much data is being
sent. When the socket is flushed to the server, it will append the eof
to the stream. Hopes this helps at all.
Thanks for your reply. I really appreciate it.

What method in the BufferedReader class can I use to read all the data
on the socket? Here are the methods I looked at.

1. readLine(): Looks like this method waits till it receives an end of
line, or the socket connection is closed.
2. read(char[] cbuf, int off, int len): Reads only specified number of
bytes, so it works as long as we specify at least as many bytes as
the client sent.

Thank you.


-C

javastudent wrote:
Hi,

I am implementing a server that reads for socket connections at a
port, and processes the socket on a separate thread. We do not control
the client implementation. Here is the scenario.

0. Client connects to the socket.
1. Client writes to the socket.
2. Server reads from the socket.
3. Client waits for the response on the socket
4. Server writes to the socket.
5. Socket is closed.

Incoming request on the socket is not guaranteed to be terminated with
any particular sequence of characters. How does the server know how
many bytes to read, and when to stop reading?

Here are couple of things I tried.

1. readln on a buffered reader on the server side would block till it
receives the "eol" character.
2. I have tried reading x number of characters at a time, and thought
if number of characters read are less than the attempted number of
characters to read (or the read method returns -1) then read all the
data sent by the client. This works if the attempted characters to
read initially, are greater than the number of characters sent by the
client, but not if it is the other way.

I have used BufferedReader, and the PrintWriter classes.

I appreciate your help in resolving this issue.

Thanks.

Jul 17 '05 #3

P: n/a
ne******@comcast.net (javastudent) wrote in
news:d4**************************@posting.google.c om:
Chris Rohr <cj****@verizon.net> wrote in message
news:<OA*****************@nwrddc03.gnilink.net>...
When the client connects to the socket it only writes the one time
then waits, so you can use the BufferedReader to read all of the data
on the socket. Each communication from the client has a new socket,
so you don't have to maintain a connection and wonder how much data
is being sent. When the socket is flushed to the server, it will
append the eof to the stream. Hopes this helps at all.


Thanks for your reply. I really appreciate it.

What method in the BufferedReader class can I use to read all the data
on the socket? Here are the methods I looked at.

1. readLine(): Looks like this method waits till it receives an end of
line, or the socket connection is closed.
2. read(char[] cbuf, int off, int len): Reads only specified number of
bytes, so it works as long as we specify at least as many bytes as
the client sent.

Thank you.

This is what I did to request HTML data back:

Socket socket = new Socket(URL, HTTP_PORT);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket
.getOutputStream()));

BufferedReader in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
out.write(outTargetrequest +" HTTP/1.0\n\n");
out.flush();

String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
out.close();
in.close();

--
Thanks in Advance...
IchBin
_____________________________________________

'Black holes are where God divided by zero.'
-Steven Wright, comedian (1955- )
Jul 17 '05 #4

P: n/a
IchBin wrote:
while ((line = in.readLine()) != null) {
System.out.println(line);

call me dumb, but isnt this Syste.out different from
your buffered output stream? e.g. should be just
out.println(line);

}
out.close();
in.close();

i am still trying to cope with all the magic hat tricks
java is unloading on newbie coders...
- nate

Jul 17 '05 #5

P: n/a
Yes, you are right.

This is not my final code. I had never done this before and wanted to test
the see the input right to the console. I now have other components using
this data, in my application. You have to start somewhere. Look up the
System.out.println() as opposed to my instantiated BufferedWriter out. I am
witting out to a different place that what I Buffered in.on... LOL...

Sorry it's late for me.

--
Thanks in Advance...
IchBin
_____________________________________________

'Black holes are where God divided by zero.'
-Steven Wright, comedian (1955- )
"Nate Smith" <gr*******@NET1Plus.com> wrote in message
news:u4********************@net1plus.com...
IchBin wrote:
while ((line = in.readLine()) != null) {
System.out.println(line);

call me dumb, but isnt this Syste.out different from
your buffered output stream? e.g. should be just
> out.println(line);

}
out.close();
in.close();

i am still trying to cope with all the magic hat tricks
java is unloading on newbie coders...
- nate

Jul 17 '05 #6

P: n/a
IchBin <we******@ptd.net> wrote in message news:<Xn********************@204.186.200.105>...
ne******@comcast.net (javastudent) wrote in
news:d4**************************@posting.google.c om:
Chris Rohr <cj****@verizon.net> wrote in message
news:<OA*****************@nwrddc03.gnilink.net>...
When the client connects to the socket it only writes the one time
then waits, so you can use the BufferedReader to read all of the data
on the socket. Each communication from the client has a new socket,
so you don't have to maintain a connection and wonder how much data
is being sent. When the socket is flushed to the server, it will
append the eof to the stream. Hopes this helps at all.


Thanks for your reply. I really appreciate it.

What method in the BufferedReader class can I use to read all the data
on the socket? Here are the methods I looked at.

1. readLine(): Looks like this method waits till it receives an end of
line, or the socket connection is closed.
2. read(char[] cbuf, int off, int len): Reads only specified number of
bytes, so it works as long as we specify at least as many bytes as
the client sent.

Thank you.

This is what I did to request HTML data back:

Socket socket = new Socket(URL, HTTP_PORT);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket
.getOutputStream()));

BufferedReader in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
out.write(outTargetrequest +" HTTP/1.0\n\n");
out.flush();

String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
out.close();
in.close();

You are relying on your clients terminating the string with "\n",
hence your readLine works. My clients wouldn't be ending the string
with "\n". However, I have just found out that we have a gate way that
terminates the string with '\004', so how do I use BufferedReader to
read until I find a termination character?

Thanks.
Jul 17 '05 #7

P: n/a
ne******@comcast.net (javastudent) wrote in
news:d4**************************@posting.google.c om:
IchBin <we******@ptd.net> wrote in message
news:<Xn********************@204.186.200.105>...
ne******@comcast.net (javastudent) wrote in
news:d4**************************@posting.google.c om:
> Chris Rohr <cj****@verizon.net> wrote in message
> news:<OA*****************@nwrddc03.gnilink.net>...
You are relying on your clients terminating the string with "\n",
hence your readLine works. My clients wouldn't be ending the string
with "\n". However, I have just found out that we have a gate way that
terminates the string with '\004', so how do I use BufferedReader to
read until I find a termination character?

Thanks.


Right off the top, not sure. Look at the InputStreamReader,
OutputStreamWriter for adding, changing or reconizing a termiation or
custom termination char, via (Java 2 Platform, Standard Edition, v 1.5.0
Beta 2 API Specification), Online or download it. Maybe even socket?

out.write(outTargetrequest +" terination char? )

Another good place to post this question is:comp.lang.java.developer or
comp.lang.java.programmer and maybe comp.lang.java.help. Don't cross post
and maybe Roedy Green or Andrew Thompson could help you out.

Sorry for now...
--
Thanks in Advance...
IchBin
_____________________________________________

'Black holes are where God divided by zero.'
-Steven Wright, comedian (1955- )
Jul 17 '05 #8

P: n/a
Look at this documentation, I happen to bump into.. It's in the "Big Index"
at Sun. This page has most all common java information! Not as detailed
specifc as the java API specs. You should aways look here first.. anyway

Go here (http://java.sun.com/docs/books/tutor...ybigindex.html)

This link will take you to the instructions...
(http://java.sun.com/docs/books/tutor...ngWriting.html
)
Other good ones just in case... and should be your bible's, so to speak..
(just downlod the lastest SDK document zip.)

JavaTM 2 SDK, Standard Edition Documentation
JavaTM 2 Platform, Standard Edition, v 1.5.0 Beta 2 API Specification
Java Language Specification
--
Hope this helps.. I think it will
IchBin
_____________________________________________

'Black holes are where God divided by zero.'
-Steven Wright, comedian (1955- )
"IchBin" <we******@ptd.net> wrote in message
news:Xn********************@204.186.200.105...
ne******@comcast.net (javastudent) wrote in
news:d4**************************@posting.google.c om:
IchBin <we******@ptd.net> wrote in message
news:<Xn********************@204.186.200.105>...
ne******@comcast.net (javastudent) wrote in
news:d4**************************@posting.google.c om:

> Chris Rohr <cj****@verizon.net> wrote in message
> news:<OA*****************@nwrddc03.gnilink.net>...

You are relying on your clients terminating the string with "\n",
hence your readLine works. My clients wouldn't be ending the string
with "\n". However, I have just found out that we have a gate way that
terminates the string with '\004', so how do I use BufferedReader to
read until I find a termination character?

Thanks.


Right off the top, not sure. Look at the InputStreamReader,
OutputStreamWriter for adding, changing or reconizing a termiation or
custom termination char, via (Java 2 Platform, Standard Edition, v 1.5.0
Beta 2 API Specification), Online or download it. Maybe even socket?

out.write(outTargetrequest +" terination char? )

Another good place to post this question is:comp.lang.java.developer or
comp.lang.java.programmer and maybe comp.lang.java.help. Don't cross post
and maybe Roedy Green or Andrew Thompson could help you out.

Sorry for now...
--
Thanks in Advance...
IchBin
_____________________________________________

'Black holes are where God divided by zero.'
-Steven Wright, comedian (1955- )

Jul 17 '05 #9

P: n/a
"IchBin" <we***********@pdt.net> wrote in message news:<YG********************@ptd.net>...
Look at this documentation, I happen to bump into.. It's in the "Big Index"
at Sun. This page has most all common java information! Not as detailed
specifc as the java API specs. You should aways look here first.. anyway

Go here (http://java.sun.com/docs/books/tutor...ybigindex.html)

This link will take you to the instructions...
(http://java.sun.com/docs/books/tutor...ngWriting.html
)
Other good ones just in case... and should be your bible's, so to speak..
(just downlod the lastest SDK document zip.)

JavaTM 2 SDK, Standard Edition Documentation
JavaTM 2 Platform, Standard Edition, v 1.5.0 Beta 2 API Specification
Java Language Specification
--
Hope this helps.. I think it will
IchBin

Yes, it did. Thanks for taking time.

Regards. _____________________________________________

'Black holes are where God divided by zero.'
-Steven Wright, comedian (1955- )
"IchBin" <we******@ptd.net> wrote in message
news:Xn********************@204.186.200.105...
ne******@comcast.net (javastudent) wrote in
news:d4**************************@posting.google.c om:
IchBin <we******@ptd.net> wrote in message
news:<Xn********************@204.186.200.105>...
> ne******@comcast.net (javastudent) wrote in
> news:d4**************************@posting.google.c om:
>
> > Chris Rohr <cj****@verizon.net> wrote in message
> > news:<OA*****************@nwrddc03.gnilink.net>... You are relying on your clients terminating the string with "\n",
hence your readLine works. My clients wouldn't be ending the string
with "\n". However, I have just found out that we have a gate way that
terminates the string with '\004', so how do I use BufferedReader to
read until I find a termination character?

Thanks.


Right off the top, not sure. Look at the InputStreamReader,
OutputStreamWriter for adding, changing or reconizing a termiation or
custom termination char, via (Java 2 Platform, Standard Edition, v 1.5.0
Beta 2 API Specification), Online or download it. Maybe even socket?

out.write(outTargetrequest +" terination char? )

Another good place to post this question is:comp.lang.java.developer or
comp.lang.java.programmer and maybe comp.lang.java.help. Don't cross post
and maybe Roedy Green or Andrew Thompson could help you out.

Sorry for now...
--
Thanks in Advance...
IchBin
_____________________________________________

'Black holes are where God divided by zero.'
-Steven Wright, comedian (1955- )

Jul 17 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.